Skip to content

Commit

Permalink
[New] expose createDynamicImportTransform and getImportSource
Browse files Browse the repository at this point in the history
While working on babel/babel#9552, I realized I needed two functions:
- createDynamicImportTransform is basically all what this plugin
  does, but it can be invoked directly.
- getImportSource is needed to re-use the same import argument
  stringifying logic for the AMD and SystemJS transforms
  • Loading branch information
nicolo-ribaudo authored and ljharb committed Jun 18, 2019
1 parent d7d48bc commit 08ebb94
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
30 changes: 5 additions & 25 deletions src/index.js
@@ -1,5 +1,7 @@
export default function ({ template, types: t }) {
const buildImport = template('Promise.resolve().then(() => MODULE)');
import { createDynamicImportTransform } from './utils';

export default function (api) {
const transformImport = createDynamicImportTransform(api);

return {
// NOTE: Once we drop support for Babel <= v6 we should
Expand All @@ -11,29 +13,7 @@ export default function ({ template, types: t }) {

visitor: {
Import(path) {
const importArguments = path.parentPath.node.arguments;
const [importPath] = importArguments;
const isString = t.isStringLiteral(importPath) || t.isTemplateLiteral(importPath);
if (isString) {
t.removeComments(importPath);
}
const SOURCE = isString
? importArguments
: t.templateLiteral([
t.templateElement({ raw: '', cooked: '' }),
t.templateElement({ raw: '', cooked: '' }, true),
], importArguments);
const requireCall = t.callExpression(
t.identifier('require'),
[].concat(SOURCE),
);

const { noInterop = false } = this.opts;
const MODULE = noInterop === true ? requireCall : t.callExpression(this.addHelper('interopRequireWildcard'), [requireCall]);
const newImport = buildImport({
MODULE,
});
path.parentPath.replaceWith(newImport);
transformImport(this, path);
},
},
};
Expand Down
33 changes: 33 additions & 0 deletions src/utils.js
@@ -0,0 +1,33 @@
export function getImportSource(t, callNode) {
const importArguments = callNode.arguments;
const [importPath] = importArguments;

const isString = t.isStringLiteral(importPath) || t.isTemplateLiteral(importPath);
if (isString) {
t.removeComments(importPath);
return importPath;
}

return t.templateLiteral([
t.templateElement({ raw: '', cooked: '' }),
t.templateElement({ raw: '', cooked: '' }, true),
], importArguments);
}

export function createDynamicImportTransform({ template, types: t }) {
const buildImport = template('Promise.resolve().then(() => MODULE)');

return (context, path) => {
const requireCall = t.callExpression(
t.identifier('require'),
[getImportSource(t, path.parent)],
);

const { noInterop = false } = context.opts;
const MODULE = noInterop === true ? requireCall : t.callExpression(context.addHelper('interopRequireWildcard'), [requireCall]);
const newImport = buildImport({
MODULE,
});
path.parentPath.replaceWith(newImport);
};
}
6 changes: 6 additions & 0 deletions utils.js
@@ -0,0 +1,6 @@
// Re-export lib/utils, so that consumers can import
// babel-plugin-dynamic-import-node/utils instead of
// babel-plugin-dynamic-import-node/lib/utils

// eslint-disable-next-line import/no-unresolved
module.exports = require('./lib/utils');

0 comments on commit 08ebb94

Please sign in to comment.