Skip to content

Commit 8d3ac10

Browse files
authored
fix(bundleless): only try to redirect files under outBase dir (#760)
1 parent a6d6737 commit 8d3ac10

File tree

8 files changed

+78
-11
lines changed

8 files changed

+78
-11
lines changed

packages/core/src/config.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ const composeEntryConfig = async (
10991099
const outBase = await resolveOutBase(Object.values(resolvedEntries));
11001100
return { resolvedEntries, outBase };
11011101
}
1102+
11021103
return { resolvedEntries, outBase: null };
11031104
};
11041105

@@ -1126,7 +1127,7 @@ const composeBundlelessExternalConfig = (
11261127
redirect: Redirect,
11271128
cssModulesAuto: CssLoaderOptionsAuto,
11281129
bundle: boolean,
1129-
rootPath: string,
1130+
outBase: string | null,
11301131
): {
11311132
config: EnvironmentConfig;
11321133
resolvedJsRedirect?: DeepRequired<JsRedirect>;
@@ -1167,8 +1168,14 @@ const composeBundlelessExternalConfig = (
11671168
let resolvedRequest = request;
11681169
// use resolver to resolve the request
11691170
resolvedRequest = await resolver!(context!, resolvedRequest);
1170-
const isSubpath = resolvedRequest.startsWith(
1171-
rootPath + path.sep,
1171+
if (typeof outBase !== 'string') {
1172+
throw new Error(
1173+
`outBase expect to be a string in bundleless mode, but got ${outBase}`,
1174+
);
1175+
}
1176+
1177+
const isSubpath = normalizeSlash(resolvedRequest).startsWith(
1178+
`${normalizeSlash(outBase)}/`,
11721179
);
11731180

11741181
// only handle the request that within the root path
@@ -1456,12 +1463,19 @@ async function composeLibRsbuildConfig(
14561463
jsExtension,
14571464
dtsExtension,
14581465
} = composeAutoExtensionConfig(config, autoExtension, pkgJson);
1466+
const { entryConfig, outBase } = await composeEntryConfig(
1467+
config.source?.entry!,
1468+
config.bundle,
1469+
rootPath,
1470+
cssModulesAuto,
1471+
config.outBase,
1472+
);
14591473
const { config: bundlelessExternalConfig } = composeBundlelessExternalConfig(
14601474
jsExtension,
14611475
redirect,
14621476
cssModulesAuto,
14631477
bundle,
1464-
rootPath,
1478+
outBase,
14651479
);
14661480
const {
14671481
config: targetConfig,
@@ -1476,13 +1490,6 @@ async function composeLibRsbuildConfig(
14761490
pkgJson,
14771491
userExternals: config.output?.externals,
14781492
});
1479-
const { entryConfig, outBase } = await composeEntryConfig(
1480-
config.source?.entry!,
1481-
config.bundle,
1482-
rootPath,
1483-
cssModulesAuto,
1484-
config.outBase,
1485-
);
14861493
const cssConfig = composeCssConfig(
14871494
outBase,
14881495
config.bundle,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules/

tests/integration/bundle-false/basic/node_modules/dep_add/index.d.ts

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/bundle-false/basic/node_modules/dep_add/index.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/bundle-false/basic/node_modules/dep_add/package.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { add } from 'dep_add';
2+
3+
export const added = add(1, 2);

tests/integration/bundle-false/basic/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mainFiles1 } from './mainFiles1';
22
export { mainFiles1 };
3+
export { added } from './dep';
34

45
export * from './mainFiles2';
56
export * from './utils/numbers';

tests/integration/bundle-false/index.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ test('basic', async () => {
66
const fixturePath = join(__dirname, 'basic');
77
const { files, contents } = await buildAndGetResults({ fixturePath });
88

9+
// ESM
910
expect(files.esm).toMatchInlineSnapshot(`
1011
[
12+
"<ROOT>/tests/integration/bundle-false/basic/dist/esm/dep.js",
1113
"<ROOT>/tests/integration/bundle-false/basic/dist/esm/index.js",
1214
"<ROOT>/tests/integration/bundle-false/basic/dist/esm/mainFiles1/index.js",
1315
"<ROOT>/tests/integration/bundle-false/basic/dist/esm/mainFiles2/index.js",
@@ -23,6 +25,7 @@ test('basic', async () => {
2325

2426
expect(await import(esmIndexPath)).toMatchInlineSnapshot(`
2527
{
28+
"added": 3,
2629
"mainFiles1": "mainFiles1",
2730
"mainFiles2": "mainFiles2",
2831
"num1": 1,
@@ -36,8 +39,46 @@ test('basic', async () => {
3639
}
3740
`);
3841

42+
const { content: indexContent } = queryContent(contents.esm, 'index.js', {
43+
basename: true,
44+
});
45+
const { content: depContent } = queryContent(contents.esm, 'dep.js', {
46+
basename: true,
47+
});
48+
const { content: sumContent } = queryContent(contents.esm, 'sum.js', {
49+
basename: true,
50+
});
51+
expect(indexContent).toMatchInlineSnapshot(`
52+
"import * as __WEBPACK_EXTERNAL_MODULE__mainFiles1_index_js_c28bc628__ from "./mainFiles1/index.js";
53+
import * as __WEBPACK_EXTERNAL_MODULE__dep_js_6cc8d1bf__ from "./dep.js";
54+
export * from "./mainFiles2/index.js";
55+
export * from "./utils/numbers.js";
56+
export * from "./utils/strings.js";
57+
export * from "./sum.js";
58+
var __webpack_exports__added = __WEBPACK_EXTERNAL_MODULE__dep_js_6cc8d1bf__.added;
59+
var __webpack_exports__mainFiles1 = __WEBPACK_EXTERNAL_MODULE__mainFiles1_index_js_c28bc628__.mainFiles1;
60+
export { __webpack_exports__added as added, __webpack_exports__mainFiles1 as mainFiles1 };
61+
"
62+
`);
63+
expect(depContent).toMatchInlineSnapshot(`
64+
"import * as __WEBPACK_EXTERNAL_MODULE_dep_add__ from "dep_add";
65+
const added = (0, __WEBPACK_EXTERNAL_MODULE_dep_add__.add)(1, 2);
66+
export { added };
67+
"
68+
`);
69+
expect(sumContent).toMatchInlineSnapshot(`
70+
"import * as __WEBPACK_EXTERNAL_MODULE__utils_numbers_js_ac3baff3__ from "./utils/numbers.js";
71+
import * as __WEBPACK_EXTERNAL_MODULE__utils_strings_js_56d354bf__ from "./utils/strings.js";
72+
const numSum = __WEBPACK_EXTERNAL_MODULE__utils_numbers_js_ac3baff3__.num1 + __WEBPACK_EXTERNAL_MODULE__utils_numbers_js_ac3baff3__.num2 + __WEBPACK_EXTERNAL_MODULE__utils_numbers_js_ac3baff3__.num3;
73+
const strSum = __WEBPACK_EXTERNAL_MODULE__utils_strings_js_56d354bf__.str1 + __WEBPACK_EXTERNAL_MODULE__utils_strings_js_56d354bf__.str2 + __WEBPACK_EXTERNAL_MODULE__utils_strings_js_56d354bf__.str3;
74+
export { numSum, strSum };
75+
"
76+
`);
77+
78+
// CJS
3979
expect(files.cjs).toMatchInlineSnapshot(`
4080
[
81+
"<ROOT>/tests/integration/bundle-false/basic/dist/cjs/dep.cjs",
4182
"<ROOT>/tests/integration/bundle-false/basic/dist/cjs/index.cjs",
4283
"<ROOT>/tests/integration/bundle-false/basic/dist/cjs/mainFiles1/index.cjs",
4384
"<ROOT>/tests/integration/bundle-false/basic/dist/cjs/mainFiles2/index.cjs",
@@ -53,6 +94,7 @@ test('basic', async () => {
5394

5495
expect((await import(cjsIndexPath)).default).toMatchInlineSnapshot(`
5596
{
97+
"added": 3,
5698
"mainFiles1": "mainFiles1",
5799
"mainFiles2": "mainFiles2",
58100
"num1": 1,

0 commit comments

Comments
 (0)