Skip to content

Commit 097bcc8

Browse files
fatfisztleunen
authored andcommitted
fix: Fix plugin running twice in some cases (#136)
Fixes #96
1 parent c4dd26f commit 097bcc8

8 files changed

Lines changed: 281 additions & 210 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
"babel-cli": "^6.23.0",
3939
"babel-core": "^6.23.1",
4040
"babel-jest": "^19.0.0",
41+
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.0",
4142
"babel-plugin-transform-object-rest-spread": "^6.23.0",
4243
"babel-preset-latest": "^6.22.0",
44+
"common-tags": "^1.4.0",
4345
"eslint": "^3.16.1",
4446
"eslint-config-airbnb-base": "^11.1.0",
4547
"eslint-plugin-import": "^2.2.0",

src/getRealPath.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ function getRealPathFromRootConfig(sourcePath, absCurrentFile, rootDirs, cwd, ex
3232
// map the source and keep its extension if the import/require had one
3333
const ext = realSourceFileExtension === sourceFileExtension ? realSourceFileExtension : '';
3434
return toLocalPath(toPosixPath(replaceExtension(
35-
mapToRelative(cwd, absCurrentFile, absFileInRoot),
36-
ext,
37-
)));
35+
mapToRelative(cwd, absCurrentFile, absFileInRoot),
36+
ext,
37+
)));
3838
}
3939

4040
return null;
@@ -53,16 +53,16 @@ function getRealPathFromAliasConfig(sourcePath, absCurrentFile, alias, cwd) {
5353
moduleSplit.pop();
5454
}
5555

56-
// no alias mapping found
56+
// no alias mapping found
5757
if (!aliasPath) {
5858
return null;
5959
}
6060

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

65-
// alias to npm module don't need relative mapping
65+
// alias to npm module don't need relative mapping
6666
if (aliasPath[0] !== '.') {
6767
return newPath;
6868
}

src/index.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -66,54 +66,51 @@ export function manipulatePluginOptions(pluginOpts, cwd = process.cwd()) {
6666
return pluginOpts;
6767
}
6868

69-
export default ({ types: t }) => ({
70-
manipulateOptions(babelOptions) {
71-
let findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
72-
findPluginOptions = manipulatePluginOptions(findPluginOptions);
73-
74-
this.customCWD = findPluginOptions.cwd;
75-
},
76-
77-
pre(file) {
78-
let { customCWD } = this.plugin;
79-
if (customCWD === 'babelrc') {
80-
const startPath = (file.opts.filename === 'unknown')
81-
? './'
82-
: file.opts.filename;
83-
84-
const { file: babelFile } = findBabelConfig.sync(startPath);
85-
customCWD = babelFile
86-
? path.dirname(babelFile)
87-
: null;
88-
}
89-
90-
this.moduleResolverCWD = customCWD || process.cwd();
91-
},
92-
93-
visitor: {
94-
CallExpression: {
95-
exit(nodePath, state) {
96-
if (nodePath.node.seen) {
97-
return;
98-
}
69+
export default ({ types: t }) => {
70+
const importVisitors = {
71+
CallExpression(nodePath, state) {
72+
transformRequireCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
73+
transformJestCalls(t, nodePath, mapModule, state, this.moduleResolverCWD);
74+
transformSystemImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
75+
},
76+
ImportDeclaration(nodePath, state) {
77+
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
78+
},
79+
ExportDeclaration(nodePath, state) {
80+
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
81+
},
82+
};
9983

100-
transformRequireCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
101-
transformJestCalls(t, nodePath, mapModule, state, this.moduleResolverCWD);
102-
transformSystemImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
84+
return {
85+
manipulateOptions(babelOptions) {
86+
let findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
87+
findPluginOptions = manipulatePluginOptions(findPluginOptions);
10388

104-
// eslint-disable-next-line no-param-reassign
105-
nodePath.node.seen = true;
106-
},
89+
this.customCWD = findPluginOptions.cwd;
10790
},
108-
ImportDeclaration: {
109-
exit(nodePath, state) {
110-
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
111-
},
91+
92+
pre(file) {
93+
let { customCWD } = this.plugin;
94+
if (customCWD === 'babelrc') {
95+
const startPath = (file.opts.filename === 'unknown')
96+
? './'
97+
: file.opts.filename;
98+
99+
const { file: babelFile } = findBabelConfig.sync(startPath);
100+
customCWD = babelFile
101+
? path.dirname(babelFile)
102+
: null;
103+
}
104+
105+
this.moduleResolverCWD = customCWD || process.cwd();
112106
},
113-
ExportDeclaration: {
114-
exit(nodePath, state) {
115-
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
107+
108+
visitor: {
109+
Program: {
110+
exit(programPath, state) {
111+
programPath.traverse(importVisitors, state);
112+
},
116113
},
117114
},
118-
},
119-
});
115+
};
116+
};

src/transformers/jest.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ export default function transformJestCalls(t, nodePath, mapper, state, cwd) {
2626
if (moduleArg.node.type === 'StringLiteral') {
2727
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
2828
if (modulePath) {
29-
const newArgs = [...args].map(a => a.node);
30-
newArgs[0] = t.stringLiteral(modulePath);
31-
nodePath.replaceWith(t.callExpression(
32-
calleePath.node, newArgs,
33-
));
29+
moduleArg.replaceWith(t.stringLiteral(modulePath));
3430
}
3531
}
3632
}

src/transformers/require.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ export default function transformRequireCall(t, nodePath, mapper, state, cwd) {
1919
if (moduleArg.node.type === 'StringLiteral') {
2020
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
2121
if (modulePath) {
22-
nodePath.replaceWith(t.callExpression(
23-
calleePath.node, [t.stringLiteral(modulePath)],
24-
));
22+
moduleArg.replaceWith(t.stringLiteral(modulePath));
2523
}
2624
}
2725
}

src/transformers/systemImport.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ export default function transformSystemImportCall(t, nodePath, mapper, state, cw
1818
if (moduleArg.node.type === 'StringLiteral') {
1919
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
2020
if (modulePath) {
21-
nodePath.replaceWith(t.callExpression(
22-
calleePath.node, [t.stringLiteral(modulePath)],
23-
));
21+
moduleArg.replaceWith(t.stringLiteral(modulePath));
2422
}
2523
}
2624
}

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export function toPosixPath(modulePath) {
66

77
export function toLocalPath(p) {
88
return p
9-
.replace(/\/index$/, '') // remove trailing /index
10-
.replace(/^(?!\.)/, './'); // insert `./` to make it a local path
9+
.replace(/\/index$/, '') // remove trailing /index
10+
.replace(/^(?!\.)/, './'); // insert `./` to make it a local path
1111
}
1212

1313
export function replaceExtension(p, ext) {

0 commit comments

Comments
 (0)