Skip to content

Commit 92d8600

Browse files
committed
fix(commonjs): Only proxy detected commonjs entry points
1 parent b8cf7b8 commit 92d8600

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

packages/commonjs/src/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export default function commonjs(options = {}) {
4545
} = options;
4646
const extensions = options.extensions || ['.js'];
4747
const filter = createFilter(options.include, options.exclude);
48+
const isPossibleCjsId = (id) => {
49+
const extName = extname(id);
50+
return extName === '.cjs' || (extensions.includes(extName) && filter(id));
51+
};
52+
4853
const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
4954

5055
const getRequireReturnsDefault =
@@ -99,7 +104,7 @@ export default function commonjs(options = {}) {
99104
};
100105
};
101106

102-
const { currentlyResolving, resolveId } = getResolveId(extensions);
107+
const { currentlyResolving, resolveId } = getResolveId(extensions, isPossibleCjsId);
103108

104109
const sourceMap = options.sourceMap !== false;
105110

@@ -295,10 +300,7 @@ export default function commonjs(options = {}) {
295300
},
296301

297302
transform(code, id) {
298-
const extName = extname(id);
299-
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
300-
return null;
301-
}
303+
if (!isPossibleCjsId(id)) return null;
302304

303305
try {
304306
return transformAndCheckExports.call(this, code, id);

packages/commonjs/src/resolve-id.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function resolveExtensions(importee, importer, extensions) {
4949
return undefined;
5050
}
5151

52-
export default function getResolveId(extensions) {
52+
export default function getResolveId(extensions, isPossibleCjsId) {
5353
const currentlyResolving = new Map();
5454

5555
return {
@@ -141,21 +141,27 @@ export default function getResolveId(extensions) {
141141
!resolved ||
142142
resolved.external ||
143143
resolved.id.endsWith(ENTRY_SUFFIX) ||
144-
isWrappedId(resolved.id, ES_IMPORT_SUFFIX)
144+
isWrappedId(resolved.id, ES_IMPORT_SUFFIX) ||
145+
!isPossibleCjsId(resolved.id)
145146
) {
146147
return resolved;
147148
}
148149
const moduleInfo = await this.load(resolved);
149-
if (resolveOptions.isEntry) {
150-
moduleInfo.moduleSideEffects = true;
151-
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
152-
return resolved.id + ENTRY_SUFFIX;
153-
}
154150
const {
155151
meta: { commonjs: commonjsMeta }
156152
} = moduleInfo;
157-
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
158-
return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
153+
if (commonjsMeta) {
154+
const { isCommonJS } = commonjsMeta;
155+
if (isCommonJS) {
156+
if (resolveOptions.isEntry) {
157+
moduleInfo.moduleSideEffects = true;
158+
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
159+
return resolved.id + ENTRY_SUFFIX;
160+
}
161+
if (isCommonJS === IS_WRAPPED_COMMONJS) {
162+
return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
163+
}
164+
}
159165
}
160166
return resolved;
161167
}

packages/commonjs/test/snapshots/function.js.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -5537,7 +5537,7 @@ Generated by [AVA](https://avajs.dev).
55375537
{
55385538
'main.js': `'use strict';␊
55395539
5540-
require('./polyfill.js');␊
5540+
global.entryDetected = true;␊
55415541
55425542
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
55435543
@@ -5563,16 +5563,10 @@ Generated by [AVA](https://avajs.dev).
55635563
55645564
Object.defineProperty(exports, '__esModule', { value: true });␊
55655565
5566-
require('./polyfill.js');␊
5567-
55685566
const other = true;␊
55695567
55705568
exports.other = other;␊
55715569
`,
5572-
'polyfill.js': `'use strict';␊
5573-
5574-
global.entryDetected = true;␊
5575-
`,
55765570
}
55775571

55785572
## preserve-modules
-33 Bytes
Binary file not shown.

packages/commonjs/test/test.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ test('handles when an imported dependency of an ES module changes type', async (
804804
let bundle = await rollup(options);
805805
t.is(meta.isCommonJS, false);
806806
t.deepEqual((await executeBundle(bundle, t)).exports, 'esm');
807-
t.deepEqual(trackedTransforms, ['main.js', 'dep.js', 'main.js?commonjs-entry']);
807+
t.deepEqual(trackedTransforms, ['main.js', 'dep.js']);
808808
trackedTransforms.length = 0;
809809
const esCode = await getCodeFromBundle(bundle);
810810
t.snapshot(esCode);
@@ -889,7 +889,7 @@ test('handles when a dynamically imported dependency of an ES module changes typ
889889
let bundle = await rollup(options);
890890
t.is(meta.isCommonJS, false);
891891
t.deepEqual(await (await executeBundle(bundle, t)).exports, 'esm');
892-
t.deepEqual(trackedTransforms, ['main.js', 'main.js?commonjs-entry', 'dep.js']);
892+
t.deepEqual(trackedTransforms, ['main.js', 'dep.js']);
893893
trackedTransforms.length = 0;
894894

895895
modules['dep.js'] = "exports.dep = 'cjs';";
@@ -1057,7 +1057,6 @@ test('handles when a required dependency of a mixed ES module changes type', asy
10571057
t.deepEqual(trackedTransforms, [
10581058
'dep.js',
10591059
'main.js',
1060-
'main.js?commonjs-entry',
10611060
'\0commonjsHelpers.js',
10621061
'\0dep.js?commonjs-proxy'
10631062
]);
@@ -1201,11 +1200,11 @@ test('allows the config to be reused', async (t) => {
12011200
let bundle = await rollup({ input: 'foo.js', ...config });
12021201
t.deepEqual(
12031202
bundle.cache.modules.map(({ id }) => id),
1204-
['foo.js', 'foo.js?commonjs-entry']
1203+
['foo.js']
12051204
);
12061205
bundle = await rollup({ input: 'bar.js', ...config });
12071206
t.deepEqual(
12081207
bundle.cache.modules.map(({ id }) => id),
1209-
['bar.js', 'bar.js?commonjs-entry']
1208+
['bar.js']
12101209
);
12111210
});

0 commit comments

Comments
 (0)