Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the path uniqueness problem #3

Merged
merged 2 commits into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Here are a few before/after examples:
import importAll from 'import-all.macro'

document.getElementById('load-stuff').addEventListener('click', () => {
importAll('./my-files/*.js').then(all => {
importAll('./files/*.js').then(all => {
console.log(all)
})
})
Expand All @@ -76,17 +76,17 @@ document.getElementById('load-stuff').addEventListener('click', () => {

document.getElementById('load-stuff').addEventListener('click', () => {
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],
}
})
.then(all => {
Expand All @@ -100,20 +100,20 @@ document.getElementById('load-stuff').addEventListener('click', () => {
```javascript
import importAll from 'import-all.macro'

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,
}
```

Expand All @@ -122,22 +122,22 @@ const a = {
```javascript
import importAll from 'import-all.macro'

const routes = importAll.deferred('./my-files/*.js')
const routes = importAll.deferred('./files/*.js')

↓ ↓ ↓ ↓ ↓ ↓

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')
},
}
```
Expand Down
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const lintStagedConfig = require('kcd-scripts/config').lintStaged

module.exports = Object.assign(lintStagedConfig.linters, {
'**/__snapshots__/**': 'node other/snap-to-readme.js',
'**/__snapshots__/**': ['node other/snap-to-readme.js', 'git add README.md'],
})
54 changes: 27 additions & 27 deletions src/__tests__/__snapshots__/macro.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`README:1 \`importAll\` uses dynamic import 1`] = `
import importAll from 'import-all.macro'

document.getElementById('load-stuff').addEventListener('click', () => {
importAll('./my-files/*.js').then(all => {
importAll('./files/*.js').then(all => {
console.log(all)
})
})
Expand All @@ -14,17 +14,17 @@ document.getElementById('load-stuff').addEventListener('click', () => {

document.getElementById('load-stuff').addEventListener('click', () => {
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],
}
})
.then(all => {
Expand All @@ -39,20 +39,20 @@ exports[`README:2 \`importAll.sync\` uses static imports 1`] = `

import importAll from 'import-all.macro'

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,
}


Expand All @@ -62,22 +62,22 @@ exports[`README:3 \`importAll.deferred\` gives an object with dynamic imports 1`

import importAll from 'import-all.macro'

const routes = importAll.deferred('./my-files/*.js')
const routes = importAll.deferred('./files/*.js')

↓ ↓ ↓ ↓ ↓ ↓

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')
},
}

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import plugin from 'babel-macros'
import prettier from 'prettier'
import {prettier as prettierConfig} from 'kcd-scripts/config'

const projectRoot = path.join(__dirname, '../../')
const projectRoot = path.join(__dirname, '../../').replace(/\\/g, '/')

expect.addSnapshotSerializer({
print(val) {
return val
.split(projectRoot)
.join('<PROJECT_ROOT>/')
.replace(/fixtures/g, 'my-files')
.replace(/fixtures/g, 'files')
.replace(/..\/macro/, 'import-all.macro')
},
test(val) {
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)
}