Skip to content

Commit 12a2d07

Browse files
daneztleunen
authored andcommitted
feat: run plugin also on Program exit to handle dynamically added imports from other transforms (#269)
1 parent 3a0e1dd commit 12a2d07

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const visitor = {
1818
enter(programPath, state) {
1919
programPath.traverse(importVisitors, state);
2020
},
21+
exit(programPath, state) {
22+
programPath.traverse(importVisitors, state);
23+
},
2124
},
2225
};
2326

@@ -29,7 +32,14 @@ export default ({ types }) => ({
2932

3033
const currentFile = file.opts.filename;
3134
this.normalizedOpts = normalizeOptions(currentFile, this.opts);
35+
// We need to keep track of all handled nodes so we do not try to transform them twice,
36+
// because we run before (enter) and after (exit) all nodes are handled
37+
this.moduleResolverVisited = new Set();
3238
},
3339

3440
visitor,
41+
42+
post() {
43+
this.moduleResolverVisited.clear();
44+
},
3545
});

src/transformers/call.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ import {
66

77

88
export default function transformCall(nodePath, state) {
9+
if (state.moduleResolverVisited.has(nodePath)) {
10+
return;
11+
}
12+
913
const calleePath = nodePath.get('callee');
1014
const isNormalCall = state.normalizedOpts.transformFunctions.some(
1115
pattern => matchesPattern(state.types, calleePath, pattern),
1216
);
1317

1418
if (isNormalCall || isImportCall(state.types, nodePath)) {
19+
state.moduleResolverVisited.add(nodePath);
1520
mapPathString(nodePath.get('arguments.0'), state);
1621
}
1722
}

src/transformers/import.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@ import { mapPathString } from '../utils';
22

33

44
export default function transformImport(nodePath, state) {
5+
if (state.moduleResolverVisited.has(nodePath)) {
6+
return;
7+
}
8+
state.moduleResolverVisited.add(nodePath);
9+
510
mapPathString(nodePath.get('source'), state);
611
}

test/dynamicImport.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,26 @@ describe('import()', () => {
5656

5757
expect(result.code).toBe('import("").then(() => {}).catch(() => {});');
5858
});
59+
60+
it('should handle imports added by other transforms', () => {
61+
const options = {
62+
...transformerOpts,
63+
plugins: [
64+
function fakePlugin({ types }) {
65+
return {
66+
visitor: {
67+
Identifier(path) {
68+
path.replaceWith(types.Import());
69+
},
70+
},
71+
};
72+
},
73+
...transformerOpts.plugins,
74+
],
75+
};
76+
const code = 'boo("components/Header/SubHeader");';
77+
const result = transform(code, options);
78+
79+
expect(result.code).toBe('import("./test/testproject/src/components/Header/SubHeader");');
80+
});
5981
});

0 commit comments

Comments
 (0)