Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for System.import #101

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(() => {});');
});
});