From ad99be55f15896277d8b85c05bddf63528f8439b Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Sat, 25 Jan 2020 19:03:43 -0500 Subject: [PATCH] feat: Create processAtProgramExit option (#30) Fixes #26 --- README.md | 8 +++++++- index.js | 24 ++++++++++++++++++++---- test/test.js | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index faec428..6912972 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ Add `bare-import-rewrite` to `plugins` in your babel settings. "alwaysRootImport": [], "ignorePrefixes": ["//"], "failOnUnresolved": false, - "resolveDirectories": ["node_modules"] + "resolveDirectories": ["node_modules"], + "processAtProgramExit": false }] ] } @@ -105,6 +106,11 @@ to be ignored. A list of extensions to use in resolver. Default `['.mjs', '.js', '.json']`. +### processAtProgramExit + +This causes processing to occur during the babel `Program.exit` visitor. In general +this option is not needed. + ### `.resolve(importModule, sourceFileName, pluginOptions)` - Resolve absolute path. This function is used internally by the babel plugin, is exposed so it can be used diff --git a/index.js b/index.js index d9bd4ce..17ae1f6 100644 --- a/index.js +++ b/index.js @@ -108,9 +108,9 @@ function tryResolve(babelPath, importPath, sourceFileName, pluginOptions) { } } -module.exports = ({types: t}) => ({ - visitor: { - CallExpression(path, {file, opts}) { +function getVisitor(t, opts) { + const visitor = { + CallExpression(path, {file}) { if (path.node.callee.type !== 'Import') { return; } @@ -123,7 +123,7 @@ module.exports = ({types: t}) => ({ source.replaceWith(t.stringLiteral(tryResolve(path, source.node.value, file.opts.parserOpts.sourceFileName, opts))); }, - 'ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration'(path, {file, opts}) { + 'ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration'(path, {file}) { const source = path.get('source'); // An export without a 'from' clause @@ -133,6 +133,22 @@ module.exports = ({types: t}) => ({ source.replaceWith(t.stringLiteral(tryResolve(path, source.node.value, file.opts.parserOpts.sourceFileName, opts))); } + }; + + if (!opts.processAtProgramExit) { + return visitor; } + + return { + Program: { + exit(path, state) { + path.traverse(visitor, state); + } + } + }; +} + +module.exports = ({types: t}, opts) => ({ + visitor: getVisitor(t, opts) }); module.exports.resolve = absResolve; diff --git a/test/test.js b/test/test.js index ae0e91c..e13014e 100644 --- a/test/test.js +++ b/test/test.js @@ -37,6 +37,21 @@ async function babelTest(t, {source, result, options = {}, expectErrors = []}) { const test = (name, helper, ...args) => t.test(name, t => helper(t, ...args)); +const pluginInjectFakeModuleAtExit = ({types: t}) => ({ + visitor: { + Program: { + exit(path) { + path.node.body.unshift( + t.importDeclaration( + [t.importDefaultSpecifier(t.identifier('mod'))], + t.stringLiteral('@cfware/fake-module1') + ) + ); + } + } + } +}); + t.test('exports', async t => { t.type(plugin, 'function'); t.type(plugin.resolve, 'function'); @@ -80,6 +95,28 @@ t.test('absolute resolve', async t => { t.equal(plugin.resolve('is-windows', fakeModule2, {alwaysRootImport: ['**']}), isWindows); }); +test('import injected by another plugins Program.exit ignored by default', babelTest, { + source: '', + result: 'import mod from "@cfware/fake-module1";', + options: { + plugins: [ + plugin, + pluginInjectFakeModuleAtExit + ] + } +}); + +test('import injected by another plugins Program.exit seen with processAtProgramExit', babelTest, { + source: '', + result: 'import mod from "./node_modules/@cfware/fake-module1/index.js";', + options: { + plugins: [ + pluginInjectFakeModuleAtExit, + [plugin, {processAtProgramExit: true}] + ] + } +}); + test('static node package', babelTest, { source: 'import mod from "@cfware/fake-module1";', result: 'import mod from "./node_modules/@cfware/fake-module1/index.js";'