Skip to content

Commit a673004

Browse files
committed
chore: rebase
1 parent a823737 commit a673004

File tree

20 files changed

+505
-138
lines changed

20 files changed

+505
-138
lines changed

packages/core/src/config.ts

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ const composeBundlelessExternalConfig = (
983983
} => {
984984
if (bundle) return { config: {} };
985985

986-
const isStyleRedirected = redirect.style ?? true;
986+
const styleRedirectPath = redirect.style?.path ?? true;
987+
const styleRedirectExtension = redirect.style?.extension ?? true;
987988
const jsRedirectPath = redirect.js?.path ?? true;
988989
const jsRedirectExtension = redirect.js?.extension ?? true;
989990

@@ -1002,60 +1003,72 @@ const composeBundlelessExternalConfig = (
10021003
if (!request || !getResolve || !context || !contextInfo) {
10031004
return callback();
10041005
}
1006+
const { issuer } = contextInfo;
10051007

10061008
if (!resolver) {
10071009
resolver = (await getResolve()) as RspackResolver;
10081010
}
10091011

1012+
async function redirectPath(
1013+
request: string,
1014+
): Promise<string | undefined> {
1015+
try {
1016+
let resolvedRequest = request;
1017+
// use resolver to resolve the request
1018+
resolvedRequest = await resolver!(context!, resolvedRequest);
1019+
1020+
// only handle the request that is not in node_modules
1021+
if (!resolvedRequest.includes('node_modules')) {
1022+
resolvedRequest = normalizeSlash(
1023+
path.relative(path.dirname(issuer), resolvedRequest),
1024+
);
1025+
// Requests that fall through here cannot be matched by any other externals config ahead.
1026+
// Treat all these requests as relative import of source code. Node.js won't add the
1027+
// leading './' to the relative path resolved by `path.relative`. So add manually it here.
1028+
if (resolvedRequest[0] !== '.') {
1029+
resolvedRequest = `./${resolvedRequest}`;
1030+
}
1031+
return resolvedRequest;
1032+
}
1033+
// NOTE: If request is a phantom dependency, which means it can be resolved but not specified in dependencies or peerDependencies in package.json, the output will be incorrect to use when the package is published
1034+
// return the original request instead of the resolved request
1035+
return undefined;
1036+
} catch (e) {
1037+
// catch error when request can not be resolved by 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.debug(
1041+
`Failed to resolve module ${color.green(`"${request}"`)} from ${color.green(issuer)}. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.`,
1042+
);
1043+
return request;
1044+
}
1045+
}
1046+
10101047
// Issuer is not empty string when the module is imported by another module.
10111048
// Prevent from externalizing entry modules here.
1012-
if (contextInfo.issuer) {
1049+
if (issuer) {
10131050
let resolvedRequest: string = request;
10141051

1015-
const cssExternal = cssExternalHandler(
1052+
const cssExternal = await cssExternalHandler(
10161053
resolvedRequest,
10171054
callback,
10181055
jsExtension,
10191056
cssModulesAuto,
1020-
isStyleRedirected,
1057+
styleRedirectPath,
1058+
styleRedirectExtension,
1059+
redirectPath,
10211060
);
10221061

10231062
if (cssExternal !== false) {
10241063
return cssExternal;
10251064
}
10261065

10271066
if (jsRedirectPath) {
1028-
try {
1029-
// use resolver to resolve the request
1030-
resolvedRequest = await resolver(context, resolvedRequest);
1031-
1032-
// only handle the request that is not in node_modules
1033-
if (!resolvedRequest.includes('node_modules')) {
1034-
resolvedRequest = normalizeSlash(
1035-
path.relative(
1036-
path.dirname(contextInfo.issuer),
1037-
resolvedRequest,
1038-
),
1039-
);
1040-
// Requests that fall through here cannot be matched by any other externals config ahead.
1041-
// Treat all these requests as relative import of source code. Node.js won't add the
1042-
// leading './' to the relative path resolved by `path.relative`. So add manually it here.
1043-
if (resolvedRequest[0] !== '.') {
1044-
resolvedRequest = `./${resolvedRequest}`;
1045-
}
1046-
} else {
1047-
// NOTE: If request is a phantom dependency, which means it can be resolved but not specified in dependencies or peerDependencies in package.json, the output will be incorrect to use when the package is published
1048-
// return the original request instead of the resolved request
1049-
return callback(undefined, request);
1050-
}
1051-
} catch (e) {
1052-
// catch error when request can not be resolved by resolver
1053-
// e.g. A react component library importing and using 'react' but while not defining
1054-
// it in devDependencies and peerDependencies. Preserve 'react' as-is if so.
1055-
logger.debug(
1056-
`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.`,
1057-
);
1067+
const redirectedPath = await redirectPath(resolvedRequest);
1068+
if (redirectedPath === undefined) {
1069+
return callback(undefined, request);
10581070
}
1071+
resolvedRequest = redirectedPath;
10591072
}
10601073

10611074
// Node.js ECMAScript module loader does no extension searching.

packages/core/src/css/cssConfig.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,48 @@ export function isCssGlobalFile(
7979

8080
type ExternalCallback = (arg0?: undefined, arg1?: string) => void;
8181

82-
export function cssExternalHandler(
82+
export async function cssExternalHandler(
8383
request: string,
8484
callback: ExternalCallback,
8585
jsExtension: string,
8686
auto: CssLoaderOptionsAuto,
87-
isStyleRedirect: boolean,
88-
): void | false {
89-
const isCssModulesRequest = isCssModulesFile(request, auto);
90-
87+
styleRedirectPath: boolean,
88+
styleRedirectExtension: boolean,
89+
redirectPath: (request: string) => Promise<string | undefined>,
90+
): Promise<false | void> {
9191
// cssExtract would execute the file handled by css-loader, so we cannot external the "helper import" from css-loader
9292
// do not external @rsbuild/core/compiled/css-loader/noSourceMaps.js, sourceMaps.js, api.mjs etc.
9393
if (/compiled\/css-loader\//.test(request)) {
9494
return callback();
9595
}
9696

97+
let resolvedRequest = request;
98+
99+
if (styleRedirectPath) {
100+
const resolved = await redirectPath(resolvedRequest);
101+
if (resolved) {
102+
resolvedRequest = resolved;
103+
}
104+
}
105+
106+
if (!isCssFile(resolvedRequest)) {
107+
return false;
108+
}
109+
97110
// 1. css modules: import './CounterButton.module.scss' -> import './CounterButton.module.mjs'
98111
// 2. css global: import './CounterButton.scss' -> import './CounterButton.css'
99-
if (request[0] === '.' && isCssFile(request)) {
100-
// preserve import './CounterButton.module.scss'
101-
if (!isStyleRedirect) {
102-
return callback(undefined, request);
103-
}
112+
if (styleRedirectExtension) {
113+
const isCssModulesRequest = isCssModulesFile(resolvedRequest, auto);
104114
if (isCssModulesRequest) {
105-
return callback(undefined, request.replace(/\.[^.]+$/, jsExtension));
115+
return callback(
116+
undefined,
117+
resolvedRequest.replace(/\.[^.]+$/, jsExtension),
118+
);
106119
}
107-
return callback(undefined, request.replace(/\.[^.]+$/, '.css'));
120+
return callback(undefined, resolvedRequest.replace(/\.[^.]+$/, '.css'));
108121
}
109122

110-
return false;
123+
return callback(undefined, resolvedRequest);
111124
}
112125

113126
const PLUGIN_NAME = 'rsbuild:lib-css';

packages/core/src/types/config.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ export type JsRedirect = {
9191
extension?: boolean;
9292
};
9393

94+
export type StyleRedirect = {
95+
/**
96+
* Whether to automatically redirect the import paths of style output files.
97+
* @defaultValue `true`
98+
*/
99+
path?: boolean;
100+
/**
101+
* Whether to automatically add the file extension to import paths based on the style output files.
102+
* @defaultValue `true`
103+
*/
104+
extension?: boolean;
105+
};
106+
94107
// @ts-expect-error TODO: support dts redirect in the future
95108
type DtsRedirect = {
96109
path?: boolean;
@@ -101,7 +114,7 @@ export type Redirect = {
101114
/** Controls the redirect of the import paths of output JavaScript files. */
102115
js?: JsRedirect;
103116
/** Whether to redirect the import path of the style file. */
104-
style?: boolean;
117+
style?: StyleRedirect;
105118
// TODO: support other redirects
106119
// asset?: boolean;
107120
// dts?: DtsRedirect;

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/redirect/style-false/rslib.config.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/integration/redirect/style-false/src/index.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)