Skip to content

Commit 87623e3

Browse files
committed
feat(commonjs): inject __esModule marker into ES namespaces
1 parent 55a9cb2 commit 87623e3

File tree

9 files changed

+471
-141
lines changed

9 files changed

+471
-141
lines changed

packages/commonjs/src/helpers.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ export function getDefaultExportFromCjs (x) {
3737
3838
export function createCommonjsModule(fn, basedir, module) {
3939
return module = {
40-
path: basedir,
41-
exports: {},
42-
require: function (path, base) {
43-
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
44-
}
40+
path: basedir,
41+
exports: {},
42+
require: function (path, base) {
43+
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
44+
}
4545
}, fn(module, module.exports), module.exports;
4646
}
4747
@@ -52,6 +52,20 @@ export function getDefaultExportFromNamespaceIfPresent (n) {
5252
export function getDefaultExportFromNamespaceIfNotNamed (n) {
5353
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
5454
}
55+
56+
export function getAugmentedNamespace(n) {
57+
var a = Object.defineProperty({}, '__esModule', {value: true});
58+
Object.keys(n).forEach(function (k) {
59+
var d = Object.getOwnPropertyDescriptor(n, k);
60+
Object.defineProperty(a, k, d.get ? d : {
61+
enumerable: true,
62+
get: function () {
63+
return n[k];
64+
}
65+
});
66+
});
67+
return a;
68+
}
5569
`;
5670

5771
const HELPER_NON_DYNAMIC = `

packages/commonjs/src/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,14 @@ export default function commonjs(options = {}) {
189189

190190
transform(code, id) {
191191
const extName = extname(id);
192-
if (extName !== '.cjs' && id !== DYNAMIC_PACKAGES_ID && !id.startsWith(DYNAMIC_JSON_PREFIX)) {
193-
if (!filter(id) || !extensions.includes(extName)) {
194-
setIsCjsPromise(id, null);
195-
return null;
196-
}
192+
if (
193+
extName !== '.cjs' &&
194+
id !== DYNAMIC_PACKAGES_ID &&
195+
!id.startsWith(DYNAMIC_JSON_PREFIX) &&
196+
(!filter(id) || !extensions.includes(extName))
197+
) {
198+
setIsCjsPromise(id, null);
199+
return null;
197200
}
198201

199202
let transformed;

packages/commonjs/src/proxies.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ export async function getStaticRequireProxy(
5959
!esModulesWithDefaultExport.has(id) ||
6060
(esModulesWithNamedExports.has(id) && requireReturnsDefault === 'auto'))
6161
) {
62-
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
62+
return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
63+
id
64+
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
6365
}
6466
return `export {default} from ${JSON.stringify(id)};`;
6567
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'adds the __esModule property when requiring an ES module and support live-bindings'
3+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable import/no-mutable-exports */
2+
let foo = 'foo';
3+
let bar = 'bar';
4+
export { foo as default, bar };
5+
6+
export function update(newFoo, newBar) {
7+
foo = newFoo;
8+
bar = newBar;
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable */
2+
var lib = require('./lib.js');
3+
4+
function _interopDefault(e) {
5+
return e && e.__esModule ? e : { default: e };
6+
}
7+
8+
var lib__default = /*#__PURE__*/_interopDefault(lib);
9+
t.is(lib__default['default'], 'foo')
10+
t.is(lib.bar, 'bar')
11+
12+
lib.update('newFoo', 'newBar');
13+
t.is(lib__default['default'], 'newFoo')
14+
t.is(lib.bar, 'newBar')
15+

0 commit comments

Comments
 (0)