From 06ba4dd5768b7e7df12d0a2bd385560ec1b1a2ca Mon Sep 17 00:00:00 2001 From: Tommy Date: Wed, 30 Nov 2016 12:09:04 +0100 Subject: [PATCH] feat: Add support for System.import (#101) Closes #99 --- src/index.js | 28 ++++++++++++++++++++++++++++ test/system.test.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 test/system.test.js diff --git a/src/index.js b/src/index.js index c52dbf2..d08a872 100644 --- a/src/index.js +++ b/src/index.js @@ -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]; @@ -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; diff --git a/test/system.test.js b/test/system.test.js new file mode 100644 index 0000000..fba208f --- /dev/null +++ b/test/system.test.js @@ -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(() => {});'); + }); +});