Skip to content

Commit

Permalink
feat: Create processAtProgramExit option (#30)
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
coreyfarrell authored Jan 26, 2020
1 parent 8dbab86 commit ad99be5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}]
]
}
Expand Down Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
37 changes: 37 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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";'
Expand Down

0 comments on commit ad99be5

Please sign in to comment.