import-all.macro
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -45,6 +45,5 @@ 'use strict'; | ||
var _importSources$reduce = importSources.reduce(function (all, source) { | ||
var sourceId = getFilename(source); | ||
var id = referencePath.scope.generateUidIdentifier(sourceId); | ||
var id = referencePath.scope.generateUidIdentifier(source); | ||
all.importNodes.push(t.importDeclaration([t.importNamespaceSpecifier(id)], t.stringLiteral(source))); | ||
all.objectProperties.push(t.objectProperty(t.stringLiteral(sourceId), id)); | ||
all.objectProperties.push(t.objectProperty(t.stringLiteral(source), id)); | ||
return all; | ||
@@ -78,6 +77,5 @@ }, { importNodes: [], objectProperties: [] }), | ||
var _importSources$reduce2 = importSources.reduce(function (all, source, index) { | ||
var sourceId = getFilename(source); | ||
all.dynamicImports.push(t.callExpression(t.import(), [t.stringLiteral(source)])); | ||
all.objectProperties.push(t.objectProperty(t.stringLiteral(sourceId), t.memberExpression(t.identifier('importVals'), t.numericLiteral(index), true))); | ||
all.objectProperties.push(t.objectProperty(t.stringLiteral(source), t.memberExpression(t.identifier('importVals'), t.numericLiteral(index), true))); | ||
return all; | ||
@@ -104,4 +102,3 @@ }, { dynamicImports: [], objectProperties: [] }), | ||
var objectProperties = importSources.map(function (source) { | ||
var sourceId = getFilename(source); | ||
return t.objectProperty(t.stringLiteral(sourceId), t.functionExpression(t.identifier(sourceId), [], t.blockStatement([t.returnStatement(t.callExpression(t.import(), [t.stringLiteral(source)]))]))); | ||
return t.objectProperty(t.stringLiteral(source), t.functionExpression(null, [], t.blockStatement([t.returnStatement(t.callExpression(t.import(), [t.stringLiteral(source)]))]))); | ||
}); | ||
@@ -127,6 +124,2 @@ | ||
return glob.sync(globValue, { cwd }); | ||
} | ||
function getFilename(string) { | ||
return path.basename(string).slice(0, -path.extname(string).length); | ||
} |
{ | ||
"name": "import-all.macro", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A babel-macro that allows you to import all files that match a glob", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -70,3 +70,3 @@ <div align="center"> | ||
document.getElementById('load-stuff').addEventListener('click', () => { | ||
importAll('./my-files/*.js').then(all => { | ||
importAll('./files/*.js').then(all => { | ||
console.log(all) | ||
@@ -80,13 +80,13 @@ }) | ||
Promise.all([ | ||
import('./my-files/a.js'), | ||
import('./my-files/b.js'), | ||
import('./my-files/c.js'), | ||
import('./my-files/d.js'), | ||
import('./files/a.js'), | ||
import('./files/b.js'), | ||
import('./files/c.js'), | ||
import('./files/d.js'), | ||
]) | ||
.then(function importAllHandler(importVals) { | ||
return { | ||
a: importVals[0], | ||
b: importVals[1], | ||
c: importVals[2], | ||
d: importVals[3], | ||
'./files/a.js': importVals[0], | ||
'./files/b.js': importVals[1], | ||
'./files/c.js': importVals[2], | ||
'./files/d.js': importVals[3], | ||
} | ||
@@ -105,16 +105,16 @@ }) | ||
const a = importAll.sync('./my-files/*.js') | ||
const a = importAll.sync('./files/*.js') | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
import * as _a from './my-files/a.js' | ||
import * as _b from './my-files/b.js' | ||
import * as _c from './my-files/c.js' | ||
import * as _d from './my-files/d.js' | ||
import * as _filesAJs from './files/a.js' | ||
import * as _filesBJs from './files/b.js' | ||
import * as _filesCJs from './files/c.js' | ||
import * as _filesDJs from './files/d.js' | ||
const a = { | ||
a: _a, | ||
b: _b, | ||
c: _c, | ||
d: _d, | ||
'./files/a.js': _filesAJs, | ||
'./files/b.js': _filesBJs, | ||
'./files/c.js': _filesCJs, | ||
'./files/d.js': _filesDJs, | ||
} | ||
@@ -128,3 +128,3 @@ ``` | ||
const routes = importAll.deferred('./my-files/*.js') | ||
const routes = importAll.deferred('./files/*.js') | ||
@@ -134,13 +134,13 @@ ↓ ↓ ↓ ↓ ↓ ↓ | ||
const routes = { | ||
a: function a() { | ||
return import('./my-files/a.js') | ||
'./files/a.js': function() { | ||
return import('./files/a.js') | ||
}, | ||
b: function b() { | ||
return import('./my-files/b.js') | ||
'./files/b.js': function() { | ||
return import('./files/b.js') | ||
}, | ||
c: function c() { | ||
return import('./my-files/c.js') | ||
'./files/c.js': function() { | ||
return import('./files/c.js') | ||
}, | ||
d: function d() { | ||
return import('./my-files/d.js') | ||
'./files/d.js': function() { | ||
return import('./files/d.js') | ||
}, | ||
@@ -147,0 +147,0 @@ } |
15302
97