import-all.macro
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -12,9 +12,13 @@ "use strict"; | ||
module.exports = createMacro(prevalMacros); | ||
module.exports = createMacro(prevalMacros, { | ||
configName: 'importAll' | ||
}); | ||
function prevalMacros({ | ||
references, | ||
state, | ||
babel | ||
...macroOptions | ||
}) { | ||
const { | ||
babel | ||
} = macroOptions; | ||
references.default.forEach(referencePath => { | ||
@@ -24,4 +28,3 @@ if (referencePath.parentPath.type === 'CallExpression') { | ||
referencePath, | ||
state, | ||
babel | ||
...macroOptions | ||
}); | ||
@@ -31,4 +34,3 @@ } else if (referencePath.parentPath.type === 'MemberExpression' && referencePath.parentPath.node.property.name === 'sync') { | ||
referencePath, | ||
state, | ||
babel | ||
...macroOptions | ||
}); | ||
@@ -38,4 +40,3 @@ } else if (referencePath.parentPath.type === 'MemberExpression' && referencePath.parentPath.node.property.name === 'deferred') { | ||
referencePath, | ||
state, | ||
babel | ||
...macroOptions | ||
}); | ||
@@ -51,3 +52,4 @@ } else { | ||
state, | ||
babel | ||
babel, | ||
config | ||
}) { | ||
@@ -64,3 +66,7 @@ const { | ||
} = state; | ||
const importSources = getImportSources(referencePath.parentPath.parentPath, path.dirname(filename)); | ||
const importSources = getImportSources({ | ||
callExpressionPath: referencePath.parentPath.parentPath, | ||
filename, | ||
config | ||
}); | ||
const { | ||
@@ -87,3 +93,4 @@ importNodes, | ||
state, | ||
babel | ||
babel, | ||
config | ||
}) { | ||
@@ -106,3 +113,7 @@ const { | ||
`); | ||
const importSources = getImportSources(referencePath.parentPath, path.dirname(filename)); | ||
const importSources = getImportSources({ | ||
callExpressionPath: referencePath.parentPath, | ||
filename, | ||
config | ||
}); | ||
const { | ||
@@ -128,3 +139,4 @@ dynamicImports, | ||
state, | ||
babel | ||
babel, | ||
config | ||
}) { | ||
@@ -141,3 +153,7 @@ const { | ||
} = state; | ||
const importSources = getImportSources(referencePath.parentPath.parentPath, path.dirname(filename)); | ||
const importSources = getImportSources({ | ||
callExpressionPath: referencePath.parentPath.parentPath, | ||
filename, | ||
config | ||
}); | ||
const objectProperties = importSources.map(source => { | ||
@@ -150,3 +166,7 @@ return t.objectProperty(t.stringLiteral(source), t.functionExpression(null, [], t.blockStatement([t.returnStatement(t.callExpression(t.import(), [t.stringLiteral(source)]))]))); | ||
function getImportSources(callExpressionPath, cwd) { | ||
function getImportSources({ | ||
callExpressionPath, | ||
filename, | ||
config | ||
}) { | ||
let globValue; | ||
@@ -164,5 +184,11 @@ | ||
return glob.sync(globValue, { | ||
cwd | ||
const filepaths = glob.sync(globValue, { | ||
cwd: path.dirname(filename) | ||
}); | ||
if (typeof config.transformModulePath === 'function') { | ||
return filepaths.map(p => config.transformModulePath(p, filename)); | ||
} | ||
return filepaths; | ||
} |
{ | ||
"name": "import-all.macro", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "A macro for babel-plugin-macros that allows you to import all files that match a glob", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -17,3 +17,3 @@ <div align="center"> | ||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors-) | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) | ||
<!-- ALL-CONTRIBUTORS-BADGE:END --> | ||
@@ -149,12 +149,12 @@ [![PRs Welcome][prs-badge]][prs] | ||
const routes = { | ||
'./files/a.js': function() { | ||
'./files/a.js': function () { | ||
return import('./files/a.js') | ||
}, | ||
'./files/b.js': function() { | ||
'./files/b.js': function () { | ||
return import('./files/b.js') | ||
}, | ||
'./files/c.js': function() { | ||
'./files/c.js': function () { | ||
return import('./files/c.js') | ||
}, | ||
'./files/d.js': function() { | ||
'./files/d.js': function () { | ||
return import('./files/d.js') | ||
@@ -167,2 +167,39 @@ }, | ||
**Configure `importAll` to transform import path before generating imports** | ||
`babel-plugin-macros.config.js`: | ||
```javascript | ||
module.exports = { | ||
importAll: { | ||
transformModulePath(modulePath, importingPath) { | ||
const projectRoot = path.join(__dirname, '../../') | ||
const modulePathWithoutExt = modulePath.replace(/\.js$/, '') | ||
const absolutePath = path.resolve( | ||
path.dirname(importingPath), | ||
modulePathWithoutExt, | ||
) | ||
const pathRelativeToRoot = path.relative(projectRoot, absolutePath) | ||
return pathRelativeToRoot | ||
}, | ||
}, | ||
} | ||
``` | ||
```javascript | ||
import importAll from 'import-all.macro' | ||
const a = importAll.sync('./files/*.js') | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
import * as _filesA from './files/a' | ||
import * as _filesB from './files/b' | ||
import * as _filesC from './files/c' | ||
import * as _filesD from './files/d' | ||
const a = { | ||
'./files/a': _filesA, | ||
'./files/b': _filesB, | ||
'./files/c': _filesC, | ||
'./files/d': _filesD, | ||
} | ||
``` | ||
## Caveats | ||
@@ -216,2 +253,3 @@ | ||
<td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4" width="100px;" alt=""/><br /><sub><b>Michaël De Boey</b></sub></a><br /><a href="https://github.com/kentcdodds/import-all.macro/commits?author=MichaelDeBoey" title="Code">💻</a></td> | ||
<td align="center"><a href="https://chengjianhua.github.io"><img src="https://avatars0.githubusercontent.com/u/10795207?v=4" width="100px;" alt=""/><br /><sub><b>Jianhua Cheng</b></sub></a><br /><a href="https://github.com/kentcdodds/import-all.macro/commits?author=chengjianhua" title="Code">💻</a> <a href="https://github.com/kentcdodds/import-all.macro/commits?author=chengjianhua" title="Tests">⚠️</a> <a href="https://github.com/kentcdodds/import-all.macro/commits?author=chengjianhua" title="Documentation">📖</a></td> | ||
</tr> | ||
@@ -218,0 +256,0 @@ </table> |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18890
168
294
0