Skip to content

Commit

Permalink
fix: Fix plugin running twice in some cases (#136)
Browse files Browse the repository at this point in the history
Fixes #96
  • Loading branch information
fatfisz authored and tleunen committed Mar 27, 2017
1 parent c4dd26f commit 097bcc8
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 210 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -38,8 +38,10 @@
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-jest": "^19.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-latest": "^6.22.0",
"common-tags": "^1.4.0",
"eslint": "^3.16.1",
"eslint-config-airbnb-base": "^11.1.0",
"eslint-plugin-import": "^2.2.0",
Expand Down
12 changes: 6 additions & 6 deletions src/getRealPath.js
Expand Up @@ -32,9 +32,9 @@ function getRealPathFromRootConfig(sourcePath, absCurrentFile, rootDirs, cwd, ex
// map the source and keep its extension if the import/require had one
const ext = realSourceFileExtension === sourceFileExtension ? realSourceFileExtension : '';
return toLocalPath(toPosixPath(replaceExtension(
mapToRelative(cwd, absCurrentFile, absFileInRoot),
ext,
)));
mapToRelative(cwd, absCurrentFile, absFileInRoot),
ext,
)));
}

return null;
Expand All @@ -53,16 +53,16 @@ function getRealPathFromAliasConfig(sourcePath, absCurrentFile, alias, cwd) {
moduleSplit.pop();
}

// no alias mapping found
// no alias mapping found
if (!aliasPath) {
return null;
}

// remove legacy "npm:" prefix for npm packages
// remove legacy "npm:" prefix for npm packages
aliasPath = aliasPath.replace(/^(npm:)/, '');
const newPath = sourcePath.replace(moduleSplit.join('/'), aliasPath);

// alias to npm module don't need relative mapping
// alias to npm module don't need relative mapping
if (aliasPath[0] !== '.') {
return newPath;
}
Expand Down
87 changes: 42 additions & 45 deletions src/index.js
Expand Up @@ -66,54 +66,51 @@ export function manipulatePluginOptions(pluginOpts, cwd = process.cwd()) {
return pluginOpts;
}

export default ({ types: t }) => ({
manipulateOptions(babelOptions) {
let findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
findPluginOptions = manipulatePluginOptions(findPluginOptions);

this.customCWD = findPluginOptions.cwd;
},

pre(file) {
let { customCWD } = this.plugin;
if (customCWD === 'babelrc') {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;

const { file: babelFile } = findBabelConfig.sync(startPath);
customCWD = babelFile
? path.dirname(babelFile)
: null;
}

this.moduleResolverCWD = customCWD || process.cwd();
},

visitor: {
CallExpression: {
exit(nodePath, state) {
if (nodePath.node.seen) {
return;
}
export default ({ types: t }) => {
const importVisitors = {
CallExpression(nodePath, state) {
transformRequireCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
transformJestCalls(t, nodePath, mapModule, state, this.moduleResolverCWD);
transformSystemImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
},
ImportDeclaration(nodePath, state) {
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
},
ExportDeclaration(nodePath, state) {
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
},
};

transformRequireCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
transformJestCalls(t, nodePath, mapModule, state, this.moduleResolverCWD);
transformSystemImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
return {
manipulateOptions(babelOptions) {
let findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
findPluginOptions = manipulatePluginOptions(findPluginOptions);

// eslint-disable-next-line no-param-reassign
nodePath.node.seen = true;
},
this.customCWD = findPluginOptions.cwd;
},
ImportDeclaration: {
exit(nodePath, state) {
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
},

pre(file) {
let { customCWD } = this.plugin;
if (customCWD === 'babelrc') {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;

const { file: babelFile } = findBabelConfig.sync(startPath);
customCWD = babelFile
? path.dirname(babelFile)
: null;
}

this.moduleResolverCWD = customCWD || process.cwd();
},
ExportDeclaration: {
exit(nodePath, state) {
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);

visitor: {
Program: {
exit(programPath, state) {
programPath.traverse(importVisitors, state);
},
},
},
},
});
};
};
6 changes: 1 addition & 5 deletions src/transformers/jest.js
Expand Up @@ -26,11 +26,7 @@ export default function transformJestCalls(t, nodePath, mapper, state, cwd) {
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
const newArgs = [...args].map(a => a.node);
newArgs[0] = t.stringLiteral(modulePath);
nodePath.replaceWith(t.callExpression(
calleePath.node, newArgs,
));
moduleArg.replaceWith(t.stringLiteral(modulePath));
}
}
}
4 changes: 1 addition & 3 deletions src/transformers/require.js
Expand Up @@ -19,9 +19,7 @@ export default function transformRequireCall(t, nodePath, mapper, state, cwd) {
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(
calleePath.node, [t.stringLiteral(modulePath)],
));
moduleArg.replaceWith(t.stringLiteral(modulePath));
}
}
}
4 changes: 1 addition & 3 deletions src/transformers/systemImport.js
Expand Up @@ -18,9 +18,7 @@ export default function transformSystemImportCall(t, nodePath, mapper, state, cw
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(
calleePath.node, [t.stringLiteral(modulePath)],
));
moduleArg.replaceWith(t.stringLiteral(modulePath));
}
}
}
4 changes: 2 additions & 2 deletions src/utils.js
Expand Up @@ -6,8 +6,8 @@ export function toPosixPath(modulePath) {

export function toLocalPath(p) {
return p
.replace(/\/index$/, '') // remove trailing /index
.replace(/^(?!\.)/, './'); // insert `./` to make it a local path
.replace(/\/index$/, '') // remove trailing /index
.replace(/^(?!\.)/, './'); // insert `./` to make it a local path
}

export function replaceExtension(p, ext) {
Expand Down

0 comments on commit 097bcc8

Please sign in to comment.