Skip to content

Commit 06ba4dd

Browse files
authored
feat: Add support for System.import (#101)
Closes #99
1 parent 8101cff commit 06ba4dd

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,33 @@ export default ({ types: t }) => {
148148
}
149149
}
150150

151+
function transformSystemImportCall(nodePath, state, cwd) {
152+
const calleePath = nodePath.get('callee');
153+
154+
if (!(
155+
t.isMemberExpression(calleePath.node) &&
156+
t.isIdentifier(calleePath.node.object, { name: 'System' }) &&
157+
t.isIdentifier(calleePath.node.property, { name: 'import' })
158+
)) {
159+
return;
160+
}
161+
162+
const args = nodePath.get('arguments');
163+
if (!args.length) {
164+
return;
165+
}
166+
167+
const moduleArg = args[0];
168+
if (moduleArg.node.type === 'StringLiteral') {
169+
const modulePath = mapModule(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
170+
if (modulePath) {
171+
nodePath.replaceWith(t.callExpression(
172+
calleePath.node, [t.stringLiteral(modulePath)],
173+
));
174+
}
175+
}
176+
}
177+
151178
return {
152179
manipulateOptions(babelOptions) {
153180
const findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
@@ -188,6 +215,7 @@ export default ({ types: t }) => {
188215

189216
transformRequireCall(nodePath, state, this.moduleResolverCWD);
190217
transformJestCalls(nodePath, state, this.moduleResolverCWD);
218+
transformSystemImportCall(nodePath, state, this.moduleResolverCWD);
191219

192220
// eslint-disable-next-line no-param-reassign
193221
nodePath.node.seen = true;

test/system.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-env jest */
2+
import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies
3+
import plugin from '../src';
4+
5+
describe('System.import', () => {
6+
const transformerOpts = {
7+
babelrc: false,
8+
plugins: [
9+
[plugin, {
10+
root: [
11+
'./test/examples/components',
12+
'./test/examples/foo',
13+
],
14+
alias: {
15+
utils: './src/mylib/subfolder/utils',
16+
},
17+
}],
18+
],
19+
};
20+
21+
it('should resolve the path based on the root config', () => {
22+
const code = 'System.import("c1").then(() => {}).catch(() => {});';
23+
const result = transform(code, transformerOpts);
24+
25+
expect(result.code).toBe('System.import("./test/examples/components/c1").then(() => {}).catch(() => {});');
26+
});
27+
28+
it('should alias the path', () => {
29+
const code = 'System.import("utils").then(() => {}).catch(() => {});';
30+
const result = transform(code, transformerOpts);
31+
32+
expect(result.code).toBe('System.import("./src/mylib/subfolder/utils").then(() => {}).catch(() => {});');
33+
});
34+
35+
it('should not change the path', () => {
36+
const code = 'System.import("./utils").then(() => {}).catch(() => {});';
37+
const result = transform(code, transformerOpts);
38+
39+
expect(result.code).toBe('System.import("./utils").then(() => {}).catch(() => {});');
40+
});
41+
});

0 commit comments

Comments
 (0)