From 36ab4cdf3c84276a913861923ee804958df8c527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ruci=C5=84ski?= Date: Wed, 27 Sep 2017 18:10:54 +0200 Subject: [PATCH] Use the full path to prevent uniqueness problems Used other/snap-to-readme.js to update the README.md --- README.md | 32 +++++++++++------------ src/__tests__/__snapshots__/macro.js.snap | 32 +++++++++++------------ src/macro.js | 17 ++++-------- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index e94d9cc..feaaf9e 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,10 @@ document.getElementById('load-stuff').addEventListener('click', () => { ]) .then(function importAllHandler(importVals) { return { - a: importVals[0], - b: importVals[1], - c: importVals[2], - d: importVals[3], + './my-files/a.js': importVals[0], + './my-files/b.js': importVals[1], + './my-files/c.js': importVals[2], + './my-files/d.js': importVals[3], } }) .then(all => { @@ -104,16 +104,16 @@ const a = importAll.sync('./my-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 _my-filesAJs from './my-files/a.js' +import * as _my-filesBJs from './my-files/b.js' +import * as _my-filesCJs from './my-files/c.js' +import * as _my-filesDJs from './my-files/d.js' const a = { - a: _a, - b: _b, - c: _c, - d: _d, + './my-files/a.js': _my-filesAJs, + './my-files/b.js': _my-filesBJs, + './my-files/c.js': _my-filesCJs, + './my-files/d.js': _my-filesDJs, } ``` @@ -127,16 +127,16 @@ const routes = importAll.deferred('./my-files/*.js') ↓ ↓ ↓ ↓ ↓ ↓ const routes = { - a: function a() { + './my-files/a.js': function() { return import('./my-files/a.js') }, - b: function b() { + './my-files/b.js': function() { return import('./my-files/b.js') }, - c: function c() { + './my-files/c.js': function() { return import('./my-files/c.js') }, - d: function d() { + './my-files/d.js': function() { return import('./my-files/d.js') }, } diff --git a/src/__tests__/__snapshots__/macro.js.snap b/src/__tests__/__snapshots__/macro.js.snap index c789dbb..e5cab38 100644 --- a/src/__tests__/__snapshots__/macro.js.snap +++ b/src/__tests__/__snapshots__/macro.js.snap @@ -21,10 +21,10 @@ document.getElementById('load-stuff').addEventListener('click', () => { ]) .then(function importAllHandler(importVals) { return { - a: importVals[0], - b: importVals[1], - c: importVals[2], - d: importVals[3], + './my-files/a.js': importVals[0], + './my-files/b.js': importVals[1], + './my-files/c.js': importVals[2], + './my-files/d.js': importVals[3], } }) .then(all => { @@ -43,16 +43,16 @@ const a = importAll.sync('./my-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 _my-filesAJs from './my-files/a.js' +import * as _my-filesBJs from './my-files/b.js' +import * as _my-filesCJs from './my-files/c.js' +import * as _my-filesDJs from './my-files/d.js' const a = { - a: _a, - b: _b, - c: _c, - d: _d, + './my-files/a.js': _my-filesAJs, + './my-files/b.js': _my-filesBJs, + './my-files/c.js': _my-filesCJs, + './my-files/d.js': _my-filesDJs, } @@ -67,16 +67,16 @@ const routes = importAll.deferred('./my-files/*.js') ↓ ↓ ↓ ↓ ↓ ↓ const routes = { - a: function a() { + './my-files/a.js': function() { return import('./my-files/a.js') }, - b: function b() { + './my-files/b.js': function() { return import('./my-files/b.js') }, - c: function c() { + './my-files/c.js': function() { return import('./my-files/c.js') }, - d: function d() { + './my-files/d.js': function() { return import('./my-files/d.js') }, } diff --git a/src/macro.js b/src/macro.js index a42a815..850005a 100644 --- a/src/macro.js +++ b/src/macro.js @@ -39,15 +39,14 @@ function syncVersion({referencePath, state, babel}) { const {importNodes, objectProperties} = importSources.reduce( (all, source) => { - const sourceId = getFilename(source) - const id = referencePath.scope.generateUidIdentifier(sourceId) + const 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 }, {importNodes: [], objectProperties: []}, @@ -75,14 +74,13 @@ function asyncVersion({referencePath, state, babel}) { const {dynamicImports, objectProperties} = importSources.reduce( (all, source, index) => { - const sourceId = getFilename(source) all.dynamicImports.push( t.callExpression(t.import(), [t.stringLiteral(source)]), ) const computed = true all.objectProperties.push( t.objectProperty( - t.stringLiteral(sourceId), + t.stringLiteral(source), t.memberExpression( t.identifier('importVals'), t.numericLiteral(index), @@ -112,11 +110,10 @@ function deferredVersion({referencePath, state, babel}) { ) const objectProperties = importSources.map(source => { - const sourceId = getFilename(source) return t.objectProperty( - t.stringLiteral(sourceId), + t.stringLiteral(source), t.functionExpression( - t.identifier(sourceId), + null, [], t.blockStatement([ t.returnStatement( @@ -149,7 +146,3 @@ function getImportSources(callExpressionPath, cwd) { return glob.sync(globValue, {cwd}) } - -function getFilename(string) { - return path.basename(string).slice(0, -path.extname(string).length) -}