Skip to content

Commit

Permalink
fix(cjs): esm interop in .mjs files (#32)
Browse files Browse the repository at this point in the history
fixes #334
  • Loading branch information
privatenumber authored Jun 3, 2024
1 parent 5e70105 commit aa2b639
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/utils/transform/get-esbuild-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export const patchOptions = (
// https://github.com/evanw/esbuild/issues/1932
if (extension === '.cts' || extension === '.mts') {
options.sourcefile = `${originalSourcefile.slice(0, -3)}ts`;
} else if (extension === '.mjs') { // only used by CJS loader
options.sourcefile = `${originalSourcefile.slice(0, -3)}js`;
}
} else {
// esbuild errors to detect loader when a file doesn't have an extension
Expand Down
22 changes: 14 additions & 8 deletions tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ export const files = {
exports.named = 'named';
`,

'mjs/index.mjs': outdent`
import assert from 'assert';
export const mjsHasCjsContext = ${cjsContextCheck};
import ('pkg-commonjs').then((m) => assert(
!(typeof m.default === 'object' && ('default' in m.default)),
));
`,
mjs: {
'index.mjs': outdent`
import assert from 'assert';
import value from './value.mjs';
export const mjsHasCjsContext = ${cjsContextCheck};
assert(value === 1, 'wrong default export');
import ('pkg-commonjs').then((m) => assert(
!(typeof m.default === 'object' && ('default' in m.default)),
));
`,
'value.mjs': 'export default 1',
},

'ts/index.ts': sourcemap.tag`
import assert from 'assert';
Expand Down

0 comments on commit aa2b639

Please sign in to comment.