Skip to content

Commit c261ccf

Browse files
committed
fix: update test-runner-mocha rollup config for rewriteRelativeImportExtensions
TypeScript's rewriteRelativeImportExtensions injects a __rewriteRelativeImportExtension helper into the output. This helper is not needed in browser bundles and breaks test execution. Add stripRewriteImportExtensionPlugin to remove the helper from the rollup output. Also add resolveMochaPlugin for robust monorepo module resolution. Assisted-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 6dad14a commit c261ccf

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

packages/test-runner-mocha/rollup.config.mjs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { createRequire } from 'module';
12
import deepmerge from 'deepmerge';
23
import createConfig from '../../rollup.browser.config.mjs';
34

5+
const require = createRequire(import.meta.url);
6+
47
const REGEXP_DTS_MOCHA = /'..\/..\/..\/node_modules\/mocha\/mocha.js'/g;
58
const REGEXP_DTS_CORE = /'..\/..\/test-runner-core\/browser\/session.js'/g;
69

@@ -16,6 +19,51 @@ const rewriteDtsPlugin = {
1619
},
1720
};
1821

22+
const stripRewriteImportExtensionPlugin = {
23+
name: 'strip-rewrite-import-extension',
24+
renderChunk(code) {
25+
// Remove the __rewriteRelativeImportExtension helper and its calls
26+
// that TypeScript injects when rewriteRelativeImportExtensions is enabled
27+
code = code.replace(/var __rewriteRelativeImportExtension[^;]*;/g, '');
28+
// Replace calls with their argument, handling nested parentheses
29+
const fnName = '__rewriteRelativeImportExtension(';
30+
let result = '';
31+
let i = 0;
32+
while (i < code.length) {
33+
const idx = code.indexOf(fnName, i);
34+
if (idx === -1) {
35+
result += code.slice(i);
36+
break;
37+
}
38+
result += code.slice(i, idx);
39+
// Find the matching closing paren
40+
let depth = 1;
41+
let j = idx + fnName.length;
42+
while (j < code.length && depth > 0) {
43+
if (code[j] === '(') depth++;
44+
else if (code[j] === ')') depth--;
45+
j++;
46+
}
47+
// Extract the argument (everything between the outer parens)
48+
result += code.slice(idx + fnName.length, j - 1);
49+
i = j;
50+
}
51+
return result;
52+
},
53+
};
54+
55+
const resolveMochaPlugin = {
56+
name: 'resolve-mocha',
57+
resolveId(id) {
58+
// Resolve mocha using Node's module resolution from this package,
59+
// rather than relative path traversal which may land in the wrong
60+
// node_modules in a monorepo
61+
if (id.endsWith('node_modules/mocha/mocha.js')) {
62+
return require.resolve('mocha/mocha.js');
63+
}
64+
},
65+
};
66+
1967
const rewriteWebSocketImportPlugin = {
2068
resolveId(id) {
2169
if (id === '/__web-dev-server__web-socket.js') {
@@ -36,7 +84,7 @@ export default [
3684
'wds-socket': '/__web-dev-server__web-socket.js',
3785
},
3886
},
39-
plugins: [rewriteDtsPlugin, rewriteWebSocketImportPlugin],
87+
plugins: [resolveMochaPlugin, rewriteDtsPlugin, rewriteWebSocketImportPlugin, stripRewriteImportExtensionPlugin],
4088
}),
4189
deepmerge(createConfig('src/standalone.ts'), {
4290
output: {
@@ -46,6 +94,6 @@ export default [
4694
'wds-socket': '/__web-dev-server__web-socket.js',
4795
},
4896
},
49-
plugins: [rewriteDtsPlugin, rewriteWebSocketImportPlugin],
97+
plugins: [resolveMochaPlugin, rewriteDtsPlugin, rewriteWebSocketImportPlugin, stripRewriteImportExtensionPlugin],
5098
}),
5199
];

0 commit comments

Comments
 (0)