Skip to content

Commit

Permalink
Use the full path to prevent uniqueness problems
Browse files Browse the repository at this point in the history
Used other/snap-to-readme.js to update the README.md
  • Loading branch information
Rafał Ruciński authored and Kent C. Dodds committed Sep 27, 2017
1 parent c39e767 commit 36ab4cd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand 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,
}
```

Expand All @@ -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')
},
}
Expand Down
32 changes: 16 additions & 16 deletions src/__tests__/__snapshots__/macro.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand 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,
}
Expand All @@ -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')
},
}
Expand Down
17 changes: 5 additions & 12 deletions src/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []},
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}

0 comments on commit 36ab4cd

Please sign in to comment.