-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
1 parent
d7d48bc
commit 08ebb94
Showing
3 changed files
with
44 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |