Skip to content

Commit

Permalink
feat(commonjs): inject __esModule marker into ES namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Aug 22, 2020
1 parent 55a9cb2 commit 87623e3
Show file tree
Hide file tree
Showing 9 changed files with 471 additions and 141 deletions.
24 changes: 19 additions & 5 deletions packages/commonjs/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export function getDefaultExportFromCjs (x) {
export function createCommonjsModule(fn, basedir, module) {
return module = {
path: basedir,
exports: {},
require: function (path, base) {
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
}
path: basedir,
exports: {},
require: function (path, base) {
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
}
}, fn(module, module.exports), module.exports;
}
Expand All @@ -52,6 +52,20 @@ export function getDefaultExportFromNamespaceIfPresent (n) {
export function getDefaultExportFromNamespaceIfNotNamed (n) {
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
}
export function getAugmentedNamespace(n) {
var a = Object.defineProperty({}, '__esModule', {value: true});
Object.keys(n).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(n, k);
Object.defineProperty(a, k, d.get ? d : {
enumerable: true,
get: function () {
return n[k];
}
});
});
return a;
}
`;

const HELPER_NON_DYNAMIC = `
Expand Down
13 changes: 8 additions & 5 deletions packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,14 @@ export default function commonjs(options = {}) {

transform(code, id) {
const extName = extname(id);
if (extName !== '.cjs' && id !== DYNAMIC_PACKAGES_ID && !id.startsWith(DYNAMIC_JSON_PREFIX)) {
if (!filter(id) || !extensions.includes(extName)) {
setIsCjsPromise(id, null);
return null;
}
if (
extName !== '.cjs' &&
id !== DYNAMIC_PACKAGES_ID &&
!id.startsWith(DYNAMIC_JSON_PREFIX) &&
(!filter(id) || !extensions.includes(extName))
) {
setIsCjsPromise(id, null);
return null;
}

let transformed;
Expand Down
4 changes: 3 additions & 1 deletion packages/commonjs/src/proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export async function getStaticRequireProxy(
!esModulesWithDefaultExport.has(id) ||
(esModulesWithNamedExports.has(id) && requireReturnsDefault === 'auto'))
) {
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
id
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
}
return `export {default} from ${JSON.stringify(id)};`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'adds the __esModule property when requiring an ES module and support live-bindings'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/no-mutable-exports */
let foo = 'foo';
let bar = 'bar';
export { foo as default, bar };

export function update(newFoo, newBar) {
foo = newFoo;
bar = newBar;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable */
var lib = require('./lib.js');

function _interopDefault(e) {
return e && e.__esModule ? e : { default: e };
}

var lib__default = /*#__PURE__*/_interopDefault(lib);
t.is(lib__default['default'], 'foo')
t.is(lib.bar, 'bar')

lib.update('newFoo', 'newBar');
t.is(lib__default['default'], 'newFoo')
t.is(lib.bar, 'newBar')

Loading

0 comments on commit 87623e3

Please sign in to comment.