Skip to content

Commit adb9b34

Browse files
committed
fix: do not handle the path that can’t be resolved
1 parent a2df46a commit adb9b34

File tree

8 files changed

+200
-24
lines changed

8 files changed

+200
-24
lines changed

packages/core/src/config.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ const composeShimsConfig = (
727727
};
728728

729729
export const composeModuleImportWarn = (request: string): string => {
730-
return `The externalized commonjs request ${color.green(`"${request}"`)} will use ${color.blue('"module"')} external type in ESM format. If you want to specify other external type, considering set the request and type with ${color.blue('"output.externals"')}.`;
730+
return `The externalized commonjs request ${color.green(`"${request}"`)} will use ${color.blue('"module"')} external type in ESM format. If you want to specify other external type, consider setting the request and type with ${color.blue('"output.externals"')}.`;
731731
};
732732

733733
const composeExternalsConfig = (
@@ -1022,26 +1022,25 @@ const composeBundlelessExternalConfig = (
10221022
if (jsRedirectPath) {
10231023
try {
10241024
resolvedRequest = await resolver(context, resolvedRequest);
1025+
resolvedRequest = normalizeSlash(
1026+
path.relative(
1027+
path.dirname(contextInfo.issuer),
1028+
resolvedRequest,
1029+
),
1030+
);
1031+
// Requests that fall through here cannot be matched by any other externals config ahead.
1032+
// Treat all these requests as relative import of source code. Node.js won't add the
1033+
// leading './' to the relative path resolved by `path.relative`. So add manually it here.
1034+
if (resolvedRequest[0] !== '.') {
1035+
resolvedRequest = `./${resolvedRequest}`;
1036+
}
10251037
} catch (e) {
1026-
// Do nothing, fallthrough to other external matches.
1027-
logger.debug(
1028-
`Failed to resolve ${resolvedRequest} with resolver`,
1038+
// e.g. A react component library importing and using 'react' but while not defining
1039+
// it in devDependencies and peerDependencies. Preserve 'react' as-is if so.
1040+
logger.warn(
1041+
`Failed to resolve module ${color.green(`"${resolvedRequest}"`)} from ${color.green(contextInfo.issuer)}. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.`,
10291042
);
10301043
}
1031-
1032-
resolvedRequest = normalizeSlash(
1033-
path.relative(
1034-
path.dirname(contextInfo.issuer),
1035-
resolvedRequest,
1036-
),
1037-
);
1038-
1039-
// Requests that fall through here cannot be matched by any other externals config ahead.
1040-
// Treat all these requests as relative import of source code. Node.js won't add the
1041-
// leading './' to the relative path resolved by `path.relative`. So add manually it here.
1042-
if (resolvedRequest[0] !== '.') {
1043-
resolvedRequest = `./${resolvedRequest}`;
1044-
}
10451044
}
10461045

10471046
// Node.js ECMAScript module loader does no extension searching.
@@ -1050,7 +1049,7 @@ const composeBundlelessExternalConfig = (
10501049
// If data.request already have an extension, we replace it with new extension
10511050
// This may result in a change in semantics,
10521051
// user should use copy to keep origin file or use another separate entry to deal this
1053-
if (jsRedirectExtension) {
1052+
if (jsRedirectExtension && resolvedRequest.startsWith('.')) {
10541053
const ext = extname(resolvedRequest);
10551054
if (ext) {
10561055
if (JS_EXTENSIONS_PATTERN.test(resolvedRequest)) {

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "redirect-js-no-resolve-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
// 0 default
7+
generateBundleEsmConfig({
8+
bundle: false,
9+
output: {
10+
distPath: {
11+
root: 'dist/default/esm',
12+
},
13+
},
14+
}),
15+
// 1 js.path: false
16+
generateBundleEsmConfig({
17+
bundle: false,
18+
output: {
19+
distPath: {
20+
root: 'dist/js-path-false/esm',
21+
},
22+
},
23+
redirect: {
24+
js: {
25+
path: false,
26+
},
27+
},
28+
}),
29+
// 2 js.extension: false
30+
generateBundleEsmConfig({
31+
bundle: false,
32+
output: {
33+
distPath: {
34+
root: 'dist/js-extension-false/esm',
35+
},
36+
},
37+
redirect: {
38+
js: {
39+
extension: false,
40+
},
41+
},
42+
}),
43+
],
44+
resolve: {
45+
alias: {
46+
'~': './src',
47+
},
48+
},
49+
source: {
50+
entry: {
51+
index: './src/**',
52+
},
53+
},
54+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import lodash from 'lodash';
2+
import bar from './bar.js';
3+
import foo from './foo';
4+
5+
export default lodash.toUpper(foo + bar);

tests/integration/redirect/js.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ test('redirect.js.path false', async () => {
4444

4545
expect(indexContent).toMatchInlineSnapshot(`
4646
"import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
47-
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_fb2b582c__ from "@/bar.js";
48-
import * as __WEBPACK_EXTERNAL_MODULE__foo_js_ce8863d2__ from "@/foo.js";
49-
import * as __WEBPACK_EXTERNAL_MODULE__baz_js_b1797427__ from "~/baz.js";
47+
import * as __WEBPACK_EXTERNAL_MODULE__bar_943a8c75__ from "@/bar";
48+
import * as __WEBPACK_EXTERNAL_MODULE__foo_a5f33889__ from "@/foo";
49+
import * as __WEBPACK_EXTERNAL_MODULE__baz_3ce4598c__ from "~/baz";
5050
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__ from "./bar.js";
5151
import * as __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__ from "./foo.js";
52-
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__.foo + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__.bar + __WEBPACK_EXTERNAL_MODULE__foo_js_ce8863d2__.foo + __WEBPACK_EXTERNAL_MODULE__bar_js_fb2b582c__.bar + __WEBPACK_EXTERNAL_MODULE__baz_js_b1797427__.baz);
52+
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__.foo + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__.bar + __WEBPACK_EXTERNAL_MODULE__foo_a5f33889__.foo + __WEBPACK_EXTERNAL_MODULE__bar_943a8c75__.bar + __WEBPACK_EXTERNAL_MODULE__baz_3ce4598c__.baz);
5353
export { src_rslib_entry_ as default };
5454
"
5555
`);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import path from 'node:path';
2+
import stripAnsi from 'strip-ansi';
3+
import { buildAndGetResults, proxyConsole, queryContent } from 'test-helper';
4+
import { expect, test } from 'vitest';
5+
6+
test('redirect.js default', async () => {
7+
const fixturePath = path.resolve(__dirname, './js-not-resolve');
8+
const { logs } = proxyConsole();
9+
const contents = (await buildAndGetResults({ fixturePath, lib: ['esm0'] }))
10+
.contents;
11+
12+
const logStrings = logs
13+
.map((log) => stripAnsi(log))
14+
.filter((log) => log.startsWith('warn'))
15+
.sort();
16+
17+
expect(logStrings).toMatchInlineSnapshot(
18+
`
19+
[
20+
"warn Failed to resolve module "./bar.js" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
21+
"warn Failed to resolve module "./foo" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
22+
"warn Failed to resolve module "lodash" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
23+
]
24+
`,
25+
);
26+
27+
const { content: indexContent } = queryContent(
28+
contents.esm0!,
29+
/esm\/index\.js/,
30+
);
31+
32+
expect(indexContent).toMatchInlineSnapshot(`
33+
"import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
34+
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__ from "./bar.js";
35+
import * as __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__ from "./foo.js";
36+
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__["default"] + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__["default"]);
37+
export { src_rslib_entry_ as default };
38+
"
39+
`);
40+
});
41+
42+
test('redirect.js.path false', async () => {
43+
const fixturePath = path.resolve(__dirname, './js-not-resolve');
44+
const { logs } = proxyConsole();
45+
const contents = (await buildAndGetResults({ fixturePath, lib: ['esm1'] }))
46+
.contents;
47+
48+
const logStrings = logs
49+
.map((log) => stripAnsi(log))
50+
.filter((log) => log.startsWith('warn'));
51+
52+
expect(logStrings.length).toBe(0);
53+
54+
const { content: indexContent } = queryContent(
55+
contents.esm1!,
56+
/esm\/index\.js/,
57+
);
58+
59+
expect(indexContent).toMatchInlineSnapshot(`
60+
"import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
61+
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__ from "./bar.js";
62+
import * as __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__ from "./foo.js";
63+
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__["default"] + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__["default"]);
64+
export { src_rslib_entry_ as default };
65+
"
66+
`);
67+
});
68+
69+
test('redirect.js.extension: false', async () => {
70+
const fixturePath = path.resolve(__dirname, './js-not-resolve');
71+
const { logs } = proxyConsole();
72+
const contents = (await buildAndGetResults({ fixturePath, lib: ['esm2'] }))
73+
.contents;
74+
75+
const logStrings = logs
76+
.map((log) => stripAnsi(log))
77+
.filter((log) => log.startsWith('warn'))
78+
.sort();
79+
80+
expect(logStrings).toMatchInlineSnapshot(
81+
`
82+
[
83+
"warn Failed to resolve module "./bar.js" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
84+
"warn Failed to resolve module "./foo" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
85+
"warn Failed to resolve module "lodash" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
86+
]
87+
`,
88+
);
89+
90+
const { content: indexContent } = queryContent(
91+
contents.esm2!,
92+
/esm\/index\.js/,
93+
);
94+
95+
expect(indexContent).toMatchInlineSnapshot(`
96+
"import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
97+
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__ from "./bar.js";
98+
import * as __WEBPACK_EXTERNAL_MODULE__foo_23da6eef__ from "./foo";
99+
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_23da6eef__["default"] + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__["default"]);
100+
export { src_rslib_entry_ as default };
101+
"
102+
`);
103+
});

tests/scripts/shared.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,25 +195,28 @@ export async function rslibBuild({
195195
cwd,
196196
path,
197197
modifyConfig,
198+
lib,
198199
}: {
199200
cwd: string;
200201
path?: string;
201202
modifyConfig?: (config: RslibConfig) => void;
203+
lib?: string[];
202204
}) {
203205
const { content: rslibConfig } = await loadConfig({
204206
cwd,
205207
path,
206208
});
207209
modifyConfig?.(rslibConfig);
208210
process.chdir(cwd);
209-
const rsbuildInstance = await build(rslibConfig);
211+
const rsbuildInstance = await build(rslibConfig, { lib });
210212
return { rsbuildInstance, rslibConfig };
211213
}
212214

213215
export async function buildAndGetResults(options: {
214216
fixturePath: string;
215217
configPath?: string;
216218
type: 'all';
219+
lib?: string[];
217220
}): Promise<{
218221
js: BuildResult;
219222
dts: BuildResult;
@@ -223,19 +226,23 @@ export async function buildAndGetResults(options: {
223226
fixturePath: string;
224227
configPath?: string;
225228
type?: 'js' | 'dts' | 'css';
229+
lib?: string[];
226230
}): Promise<BuildResult>;
227231
export async function buildAndGetResults({
228232
fixturePath,
229233
configPath,
230234
type = 'js',
235+
lib,
231236
}: {
232237
fixturePath: string;
233238
configPath?: string;
234239
type?: 'js' | 'dts' | 'css' | 'all';
240+
lib?: string[];
235241
}) {
236242
const { rsbuildInstance, rslibConfig } = await rslibBuild({
237243
cwd: fixturePath,
238244
path: configPath,
245+
lib,
239246
});
240247
const {
241248
origin: { bundlerConfigs, rsbuildConfig },

0 commit comments

Comments
 (0)