Skip to content

Commit

Permalink
Inline dynamic import transform
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 7, 2022
1 parent 367d4ed commit 8d317e1
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 19 deletions.
15 changes: 15 additions & 0 deletions packages/babel-helper-module-transforms/src/dynamic-import.ts
@@ -0,0 +1,15 @@
// Heavily inspired by
// https://github.com/airbnb/babel-plugin-dynamic-import-node/blob/master/src/utils.js

import * as t from "@babel/types";
import template from "@babel/template";

export function getDynamicImportSource(
node: t.CallExpression,
): t.StringLiteral | t.TemplateLiteral {
const [source] = node.arguments;

return t.isStringLiteral(source) || t.isTemplateLiteral(source)
? source
: (template.expression.ast`\`${source}\`` as t.TemplateLiteral);
}
2 changes: 2 additions & 0 deletions packages/babel-helper-module-transforms/src/index.ts
Expand Up @@ -35,6 +35,8 @@ import type {
} from "./normalize-and-load-metadata";
import type { NodePath } from "@babel/traverse";

export { getDynamicImportSource } from "./dynamic-import";

export { default as getModuleName } from "./get-module-name";
export type { PluginOptions } from "./get-module-name";

Expand Down
3 changes: 1 addition & 2 deletions packages/babel-plugin-transform-modules-amd/package.json
Expand Up @@ -15,8 +15,7 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-module-transforms": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^",
"babel-plugin-dynamic-import-node": "^2.3.3"
"@babel/helper-plugin-utils": "workspace:^"
},
"keywords": [
"babel-plugin"
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-transform-modules-amd/src/index.ts
Expand Up @@ -9,9 +9,9 @@ import {
ensureStatementsHoisted,
wrapInterop,
getModuleName,
getDynamicImportSource,
} from "@babel/helper-module-transforms";
import { template, types as t } from "@babel/core";
import { getImportSource } from "babel-plugin-dynamic-import-node/utils";
import type { PluginOptions } from "@babel/helper-module-transforms";
import type { NodePath } from "@babel/traverse";

Expand Down Expand Up @@ -102,7 +102,7 @@ export default declare<State>((api, options: Options) => {
template.expression.ast`
new Promise((${resolveId}, ${rejectId}) =>
${requireId}(
[${getImportSource(t, path.node)}],
[${getDynamicImportSource(path.node)}],
imported => ${t.cloneNode(resolveId)}(${result}),
${t.cloneNode(rejectId)}
)
Expand Down
Expand Up @@ -15,8 +15,7 @@
"dependencies": {
"@babel/helper-module-transforms": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^",
"@babel/helper-simple-access": "workspace:^",
"babel-plugin-dynamic-import-node": "^2.3.3"
"@babel/helper-simple-access": "workspace:^"
},
"keywords": [
"babel-plugin"
Expand Down
@@ -0,0 +1,35 @@
// Heavily inspired by
// https://github.com/airbnb/babel-plugin-dynamic-import-node/blob/master/src/utils.js

import type { NodePath } from "@babel/traverse";
import { types as t, template, type File } from "@babel/core";
import { getDynamicImportSource } from "@babel/helper-module-transforms";

const requireNoInterop = (source: t.Expression) =>
template.expression.ast`require(${source})`;
const requireInterop = (source: t.Expression, file: File) =>
t.callExpression(file.addHelper("interopRequireWildcard"), [
requireNoInterop(source),
]);

export function transformDynamicImport(
path: NodePath<t.CallExpression>,
noInterop: boolean,
file: File,
) {
const buildRequire = noInterop ? requireNoInterop : requireInterop;

const source = getDynamicImportSource(path.node);

const replacement = path.scope.isPure(source, /* constantsOnly */ true)
? template.expression.ast`
Promise.resolve().then(() => ${buildRequire(source, file)})
`
: template.expression.ast`
Promise.resolve(${source}).then(
s => ${buildRequire(t.identifier("s"), file)}
)
`;

path.replaceWith(replacement);
}
8 changes: 3 additions & 5 deletions packages/babel-plugin-transform-modules-commonjs/src/index.ts
Expand Up @@ -14,7 +14,7 @@ import { template, types as t } from "@babel/core";
import type { PluginOptions } from "@babel/helper-module-transforms";
import type { Visitor, Scope } from "@babel/traverse";

import { createDynamicImportTransform } from "babel-plugin-dynamic-import-node/utils";
import { transformDynamicImport } from "./dynamic-import";

export interface Options extends PluginOptions {
allowCommonJSExports?: boolean;
Expand All @@ -32,8 +32,6 @@ export interface Options extends PluginOptions {
export default declare((api, options: Options) => {
api.assertVersion(7);

const transformImportCall = createDynamicImportTransform(api);

const {
// 'true' for imports to strictly have .default, instead of having
// destructuring-like behavior for their properties. This matches the behavior
Expand Down Expand Up @@ -174,14 +172,14 @@ export default declare((api, options: Options) => {
visitor: {
CallExpression(path) {
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
if (!path.get("callee").isImport()) return;
if (!t.isImport(path.node)) return;

let { scope } = path;
do {
scope.rename("require");
} while ((scope = scope.parent));

transformImportCall(this, path.get("callee"));
transformDynamicImport(path, noInterop, this.file);
},

Program: {
Expand Down
Expand Up @@ -16,8 +16,7 @@
"@babel/helper-hoist-variables": "workspace:^",
"@babel/helper-module-transforms": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^",
"@babel/helper-validator-identifier": "workspace:^",
"babel-plugin-dynamic-import-node": "^2.3.3"
"@babel/helper-validator-identifier": "workspace:^"
},
"keywords": [
"babel-plugin"
Expand Down
9 changes: 6 additions & 3 deletions packages/babel-plugin-transform-modules-systemjs/src/index.ts
@@ -1,8 +1,11 @@
import { declare } from "@babel/helper-plugin-utils";
import hoistVariables from "@babel/helper-hoist-variables";
import { template, types as t } from "@babel/core";
import { getImportSource } from "babel-plugin-dynamic-import-node/utils";
import { rewriteThis, getModuleName } from "@babel/helper-module-transforms";
import {
rewriteThis,
getModuleName,
getDynamicImportSource,
} from "@babel/helper-module-transforms";
import type { PluginOptions } from "@babel/helper-module-transforms";
import { isIdentifierName } from "@babel/helper-validator-identifier";
import type { NodePath, Scope, Visitor } from "@babel/traverse";
Expand Down Expand Up @@ -276,7 +279,7 @@ export default declare<PluginState>((api, options: Options) => {
t.identifier(state.contextIdent),
t.identifier("import"),
),
[getImportSource(t, path.node)],
[getDynamicImportSource(path.node)],
),
);
}
Expand Down
3 changes: 0 additions & 3 deletions yarn.lock
Expand Up @@ -2634,7 +2634,6 @@ __metadata:
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
"@babel/plugin-external-helpers": "workspace:^"
babel-plugin-dynamic-import-node: ^2.3.3
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
Expand Down Expand Up @@ -2666,7 +2665,6 @@ __metadata:
"@babel/plugin-external-helpers": "workspace:^"
"@babel/plugin-syntax-class-static-block": ^7.14.5
"@babel/plugin-syntax-object-rest-spread": ^7.8.3
babel-plugin-dynamic-import-node: ^2.3.3
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
Expand Down Expand Up @@ -2698,7 +2696,6 @@ __metadata:
"@babel/helper-plugin-utils": "workspace:^"
"@babel/helper-validator-identifier": "workspace:^"
"@babel/plugin-syntax-dynamic-import": ^7.8.3
babel-plugin-dynamic-import-node: ^2.3.3
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
Expand Down

0 comments on commit 8d317e1

Please sign in to comment.