Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
Call require.default in dynamic import rewriter. (#356)
Browse files Browse the repository at this point in the history
The require import we construct gets wrapped in the Babel helper
_interopRequireWildcard, which redefines require from a function to a
module object (with the original object at .default).
  • Loading branch information
aomarks committed Apr 5, 2018
1 parent 4c4ae10 commit 0db883d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

<!-- ## Unreleased -->
<!-- Add new, unreleased changes here. -->
* Fix bug with node module resolution where specifiers in dynamic import() we're rewritten
* Fix bug with node module resolution where specifiers in dynamic import() were rewritten.
* Fix bug with dynamic import rewriting where imported require function could not be called.

## [3.0.0-pre.8] - 2018-04-04
* Fix bug where not all percent-encoded characters in URIs would be decoded (in particular, `%40` -> `@` which is important for scoped NPM packages).
Expand Down
6 changes: 5 additions & 1 deletion src/babel-plugin-dynamic-import-amd.ts
Expand Up @@ -66,8 +66,12 @@ export const dynamicImportAmd = {
// Transform the dynamic import callsites
for (const importPath of dynamicImports) {
const specifier = importPath.node.arguments[0];
// Call as `require.default` because the AMD transformer that we assume
// is running next will rewrite `require` from a function to a module
// object with the function at `default`.
importPath.replaceWith(ast`(
new Promise((res, rej) => ${requireId}([${specifier}], res, rej))
new Promise((res, rej) => ${requireId}.default([${
specifier}], res, rej))
)`);
}
},
Expand Down
10 changes: 4 additions & 6 deletions src/test/babel-plugin-dynamic-import-amd_test.ts
Expand Up @@ -21,15 +21,14 @@ import {dynamicImportAmd} from '../babel-plugin-dynamic-import-amd';
const babelTransformModulesAmd = require('@babel/plugin-transform-modules-amd');

suite('babel-plugin-transform-modules-amd', () => {

test('transforms import()', () => {
const input = stripIndent(`
const foo = import('./foo.js');
`);

const expected = stripIndent(`
import * as _require from 'require';
const foo = new Promise((res, rej) => _require(['./foo.js'], res, rej));
const foo = new Promise((res, rej) => _require.default(['./foo.js'], res, rej));
`);
const result =
babelCore.transform(input, {plugins: [dynamicImportAmd]}).code;
Expand All @@ -49,10 +48,10 @@ suite('babel-plugin-transform-modules-amd', () => {
const expected = stripIndent(`
import * as _require3 from 'require';
let _require = true;
const foo = new Promise((res, rej) => _require3(['./foo.js'], res, rej));
const foo = new Promise((res, rej) => _require3.default(['./foo.js'], res, rej));
{
let _require2 = true;
new Promise((res, rej) => _require3(['./bar.js'], res, rej));
new Promise((res, rej) => _require3.default(['./bar.js'], res, rej));
}
`);
const result =
Expand All @@ -74,7 +73,6 @@ suite('babel-plugin-transform-modules-amd', () => {
result, `define(["require", "./bar.js"], function (_require, _bar) {`);
assert.include(
result,
`const foo = new Promise((res, rej) => _require(['./foo.js'], res, rej));`);
`const foo = new Promise((res, rej) => _require.default(['./foo.js'], res, rej));`);
});

});
2 changes: 1 addition & 1 deletion src/test/js-transform_test.ts
Expand Up @@ -314,6 +314,6 @@ suite('jsTransform', () => {
`define(["exports", "require", "dep1"], function (_exports, _require, _dep) {`);
assert.include(
result,
`console.log(new Promise((res, rej) => _require(['./bar.js'], res, rej)));`);
`console.log(new Promise((res, rej) => _require.default(['./bar.js'], res, rej)));`);
});
});

0 comments on commit 0db883d

Please sign in to comment.