From 08ebb940ed3304ae68ec94f7d9b85b6acdf4b946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 18 Jun 2019 23:27:52 +0200 Subject: [PATCH] [New] expose createDynamicImportTransform and getImportSource 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 --- src/index.js | 30 +++++------------------------- src/utils.js | 33 +++++++++++++++++++++++++++++++++ utils.js | 6 ++++++ 3 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 src/utils.js create mode 100644 utils.js diff --git a/src/index.js b/src/index.js index 37770a6..d154cc9 100644 --- a/src/index.js +++ b/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 @@ -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); }, }, }; diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..7b82e42 --- /dev/null +++ b/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); + }; +} diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..1dc2e6c --- /dev/null +++ b/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');