Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamicImportFunction option (#2723)
* feat: add dynamicImportFunction option This option allows users of `es` format to rename the `import()` function, which right now has mixed support in browsers. By allowing this function to be renamed we can have polyfills (which cannot use the reserved `import` keyword but could go by a different name like `importModule`). * docs: add dynamicImportFunction docs * feat: warn when using dynamicImportFunction in non-esm format * Add functional tests and align warning messages * Fix merge * Add dynamicImportFunction to two more places and fix sorting
- Loading branch information
1 parent
7c141d2
commit 91ce981
Showing
40 changed files
with
206 additions
and
46 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
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 |
---|---|---|
|
@@ -134,6 +134,7 @@ const outputOptions = { | |
|
||
// danger zone | ||
amd, | ||
dynamicImportFunction, | ||
esModule, | ||
exports, | ||
freeze, | ||
|
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
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
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
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
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,15 @@ | ||
module.exports = { | ||
description: 'allows specifying a custom importer function', | ||
options: { | ||
input: 'main.js', | ||
onwarn() {}, | ||
plugins: { | ||
resolveDynamicImport() { | ||
return false; | ||
} | ||
}, | ||
output: { | ||
dynamicImportFunction: 'foobar' | ||
} | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
5
test/chunking-form/samples/dynamic-import-name/_expected/amd/main.js
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,5 @@ | ||
define(['require'], function (require) { 'use strict'; | ||
|
||
new Promise(function (resolve, reject) { require(['./foo.js'], resolve, reject) }).then(result => console.log(result)); | ||
|
||
}); |
3 changes: 3 additions & 0 deletions
3
test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js
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,3 @@ | ||
'use strict'; | ||
|
||
Promise.resolve(require('./foo.js')).then(result => console.log(result)); |
1 change: 1 addition & 0 deletions
1
test/chunking-form/samples/dynamic-import-name/_expected/es/main.js
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 @@ | ||
foobar('./foo.js').then(result => console.log(result)); |
10 changes: 10 additions & 0 deletions
10
test/chunking-form/samples/dynamic-import-name/_expected/system/main.js
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,10 @@ | ||
System.register([], function (exports, module) { | ||
'use strict'; | ||
return { | ||
execute: function () { | ||
|
||
module.import('./foo.js').then(result => console.log(result)); | ||
|
||
} | ||
}; | ||
}); |
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 @@ | ||
import('./foo.js').then(result => console.log(result)); |
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,32 @@ | ||
const assert = require('assert'); | ||
|
||
module.exports = { | ||
description: 'warns when specifying a custom importer function for formats other than "esm"', | ||
context: { | ||
require(path) { | ||
assert.equal(path, './foo.js'); | ||
return 42; | ||
} | ||
}, | ||
options: { | ||
input: 'main.js', | ||
plugins: { | ||
resolveDynamicImport() { | ||
return false; | ||
} | ||
}, | ||
output: { | ||
dynamicImportFunction: 'myImporter', | ||
format: 'cjs' | ||
} | ||
}, | ||
exports(exports) { | ||
return exports.fromFoo.then(value => assert.strictEqual(value, 42)); | ||
}, | ||
warnings: [ | ||
{ | ||
code: 'INVALID_OPTION', | ||
message: '"output.dynamicImportFunction" is ignored for formats other than "esm".' | ||
} | ||
] | ||
}; |
5 changes: 5 additions & 0 deletions
5
test/function/samples/dynamic-import-name-warn/_expected/amd/main.js
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,5 @@ | ||
define(['require'], function (require) { 'use strict'; | ||
|
||
new Promise(function (resolve, reject) { require(['./foo.js'], resolve, reject) }); | ||
|
||
}); |
3 changes: 3 additions & 0 deletions
3
test/function/samples/dynamic-import-name-warn/_expected/cjs/main.js
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,3 @@ | ||
'use strict'; | ||
|
||
Promise.resolve(require('./foo.js')); |
1 change: 1 addition & 0 deletions
1
test/function/samples/dynamic-import-name-warn/_expected/es/main.js
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 @@ | ||
foobar('./foo.js'); |
10 changes: 10 additions & 0 deletions
10
test/function/samples/dynamic-import-name-warn/_expected/system/main.js
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,10 @@ | ||
System.register([], function (exports, module) { | ||
'use strict'; | ||
return { | ||
execute: function () { | ||
|
||
module.import('./foo.js'); | ||
|
||
} | ||
}; | ||
}); |
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 @@ | ||
export const fromFoo = import('./foo.js'); |
Oops, something went wrong.