Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide synthetic namespace for dynamic imports when perserving modul…
…es (#4913)
- Loading branch information
1 parent
3682f30
commit eb9777c
Showing
18 changed files
with
133 additions
and
33 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ module.exports = { | |
} | ||
}, | ||
output: { | ||
exports: 'named', | ||
preserveModules: true | ||
} | ||
} | ||
|
27 changes: 25 additions & 2 deletions
27
...nking-form/samples/dynamic-import-synthetic-exports-preserve-modules/_expected/amd/lib.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 |
---|---|---|
@@ -1,8 +1,31 @@ | ||
define((function () { 'use strict'; | ||
define(['exports'], (function (exports) { 'use strict'; | ||
|
||
function _mergeNamespaces(n, m) { | ||
m.forEach(function (e) { | ||
e && typeof e !== 'string' && !Array.isArray(e) && Object.keys(e).forEach(function (k) { | ||
if (k !== 'default' && !(k in n)) { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
}); | ||
return Object.freeze(n); | ||
} | ||
|
||
const __moduleExports = { foo: 'bar' }; | ||
var lib = 'baz'; | ||
|
||
return lib; | ||
var lib$1 = /*#__PURE__*/_mergeNamespaces({ | ||
__proto__: null, | ||
default: lib | ||
}, [__moduleExports]); | ||
|
||
exports.default = lib; | ||
exports.lib = lib$1; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
})); |
4 changes: 1 addition & 3 deletions
4
...king-form/samples/dynamic-import-synthetic-exports-preserve-modules/_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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
define(['require'], (function (require) { 'use strict'; | ||
|
||
function _interopNamespaceDefaultOnly (e) { return Object.freeze({ __proto__: null, default: e }); } | ||
|
||
new Promise(function (resolve, reject) { require(['./lib'], function (m) { resolve(/*#__PURE__*/_interopNamespaceDefaultOnly(m)); }, reject); }).then(console.log); | ||
new Promise(function (resolve, reject) { require(['./lib'], resolve, reject); }).then(function (n) { return n.lib; }).then(console.log); | ||
|
||
})); |
25 changes: 24 additions & 1 deletion
25
...nking-form/samples/dynamic-import-synthetic-exports-preserve-modules/_expected/cjs/lib.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 |
---|---|---|
@@ -1,6 +1,29 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
function _mergeNamespaces(n, m) { | ||
m.forEach(function (e) { | ||
e && typeof e !== 'string' && !Array.isArray(e) && Object.keys(e).forEach(function (k) { | ||
if (k !== 'default' && !(k in n)) { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
}); | ||
return Object.freeze(n); | ||
} | ||
|
||
const __moduleExports = { foo: 'bar' }; | ||
var lib = 'baz'; | ||
|
||
module.exports = lib; | ||
var lib$1 = /*#__PURE__*/_mergeNamespaces({ | ||
__proto__: null, | ||
default: lib | ||
}, [__moduleExports]); | ||
|
||
exports.default = lib; | ||
exports.lib = lib$1; |
4 changes: 1 addition & 3 deletions
4
...king-form/samples/dynamic-import-synthetic-exports-preserve-modules/_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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
'use strict'; | ||
|
||
function _interopNamespaceDefaultOnly (e) { return Object.freeze({ __proto__: null, default: e }); } | ||
|
||
Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./lib.js')); }).then(console.log); | ||
Promise.resolve().then(function () { return require('./lib.js'); }).then(function (n) { return n.lib; }).then(console.log); |
22 changes: 21 additions & 1 deletion
22
...unking-form/samples/dynamic-import-synthetic-exports-preserve-modules/_expected/es/lib.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 |
---|---|---|
@@ -1,4 +1,24 @@ | ||
function _mergeNamespaces(n, m) { | ||
m.forEach(function (e) { | ||
e && typeof e !== 'string' && !Array.isArray(e) && Object.keys(e).forEach(function (k) { | ||
if (k !== 'default' && !(k in n)) { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
}); | ||
return Object.freeze(n); | ||
} | ||
|
||
const __moduleExports = { foo: 'bar' }; | ||
var lib = 'baz'; | ||
|
||
export { lib as default }; | ||
var lib$1 = /*#__PURE__*/_mergeNamespaces({ | ||
__proto__: null, | ||
default: lib | ||
}, [__moduleExports]); | ||
|
||
export { lib as default, lib$1 as l }; |
2 changes: 1 addition & 1 deletion
2
...nking-form/samples/dynamic-import-synthetic-exports-preserve-modules/_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 |
---|---|---|
@@ -1 +1 @@ | ||
import('./lib.js').then(console.log); | ||
import('./lib.js').then(function (n) { return n.l; }).then(console.log); |
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
24 changes: 24 additions & 0 deletions
24
test/function/samples/synthetic-named-exports/preserve-modules/_config.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,24 @@ | ||
const assert = require('node:assert'); | ||
|
||
module.exports = { | ||
description: 'handles a dynamic import with synthetic named exports in preserveModules mode', | ||
options: { | ||
output: { | ||
preserveModules: true | ||
}, | ||
plugins: [ | ||
{ | ||
name: 'test', | ||
transform() { | ||
return { | ||
syntheticNamedExports: '__synthetic' | ||
}; | ||
} | ||
} | ||
] | ||
}, | ||
async exports(exports) { | ||
const { foo } = await exports.promise; | ||
assert.strictEqual(foo, 'bar'); | ||
} | ||
}; |
1 change: 1 addition & 0 deletions
1
test/function/samples/synthetic-named-exports/preserve-modules/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 @@ | ||
export const promise = import('./other'); |
1 change: 1 addition & 0 deletions
1
test/function/samples/synthetic-named-exports/preserve-modules/other.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 __synthetic = { foo: 'bar' }; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.