Skip to content

Commit

Permalink
feat: Add support for System.import (#101)
Browse files Browse the repository at this point in the history
Closes #99
  • Loading branch information
tleunen committed Nov 30, 2016
1 parent 8101cff commit 06ba4dd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,33 @@ export default ({ types: t }) => {
}
}

function transformSystemImportCall(nodePath, state, cwd) {
const calleePath = nodePath.get('callee');

if (!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, { name: 'System' }) &&
t.isIdentifier(calleePath.node.property, { name: 'import' })
)) {
return;
}

const args = nodePath.get('arguments');
if (!args.length) {
return;
}

const moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapModule(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(
calleePath.node, [t.stringLiteral(modulePath)],
));
}
}
}

return {
manipulateOptions(babelOptions) {
const findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
Expand Down Expand Up @@ -188,6 +215,7 @@ export default ({ types: t }) => {

transformRequireCall(nodePath, state, this.moduleResolverCWD);
transformJestCalls(nodePath, state, this.moduleResolverCWD);
transformSystemImportCall(nodePath, state, this.moduleResolverCWD);

// eslint-disable-next-line no-param-reassign
nodePath.node.seen = true;
Expand Down
41 changes: 41 additions & 0 deletions test/system.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-env jest */
import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies
import plugin from '../src';

describe('System.import', () => {
const transformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: [
'./test/examples/components',
'./test/examples/foo',
],
alias: {
utils: './src/mylib/subfolder/utils',
},
}],
],
};

it('should resolve the path based on the root config', () => {
const code = 'System.import("c1").then(() => {}).catch(() => {});';
const result = transform(code, transformerOpts);

expect(result.code).toBe('System.import("./test/examples/components/c1").then(() => {}).catch(() => {});');
});

it('should alias the path', () => {
const code = 'System.import("utils").then(() => {}).catch(() => {});';
const result = transform(code, transformerOpts);

expect(result.code).toBe('System.import("./src/mylib/subfolder/utils").then(() => {}).catch(() => {});');
});

it('should not change the path', () => {
const code = 'System.import("./utils").then(() => {}).catch(() => {});';
const result = transform(code, transformerOpts);

expect(result.code).toBe('System.import("./utils").then(() => {}).catch(() => {});');
});
});

0 comments on commit 06ba4dd

Please sign in to comment.