Skip to content

Commit 8b1d25e

Browse files
committed
chore: fix
1 parent 3531216 commit 8b1d25e

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

examples/react-component-bundle-false/rslib.config.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,5 @@ export default defineConfig({
3737
// bundle: false,
3838
// },
3939
],
40-
// tools: {
41-
// bundlerChain(config, {CHAIN_ID}) {
42-
// const cssExtract = CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT
43-
// config.plugins.delete(cssExtract)
44-
45-
// },
46-
// cssExtract: {
47-
// loaderOptions: {
48-
// rootDir: './src',
49-
// },
50-
// },
51-
// },
5240
plugins: [pluginReact()],
5341
});

packages/core/src/config.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
Format,
2525
LibConfig,
2626
PkgJson,
27+
Redirect,
2728
RsbuildConfigOutputTarget,
2829
RslibConfig,
2930
RslibConfigAsyncFn,
@@ -718,10 +719,13 @@ const composeEntryConfig = async (
718719

719720
const composeBundleConfig = (
720721
jsExtension: string,
722+
redirect: Redirect,
721723
bundle = true,
722724
): RsbuildConfig => {
723725
if (bundle) return {};
724726

727+
const { style: isStyleRedirect = true } = redirect;
728+
725729
return {
726730
output: {
727731
externals: [
@@ -741,7 +745,27 @@ const composeBundleConfig = (
741745
return callback();
742746
}
743747

744-
request = extname(request)
748+
const isCssModuleEmitted = request.endsWith('_module.css');
749+
if (isCssModuleEmitted) {
750+
return callback(null, request);
751+
}
752+
753+
const ext = extname(request);
754+
const isCssExt = ext === 'css';
755+
if (isCssExt && !isStyleRedirect) {
756+
return callback(null, request);
757+
}
758+
759+
// const jsExtensions = ['js', 'cjs', 'mjs', 'ts', 'mts', 'cts', 'jsx', 'tsx'];
760+
// const isNotJsExt = ext && !jsExtensions.includes(ext)
761+
762+
// 1. not js: css svg asset
763+
// if(isNotJsExt) {
764+
// return callback(null, request)
765+
// }
766+
767+
// 2. js or no ext
768+
request = ext
745769
? request.replace(/\.[^.]+$/, jsExtension)
746770
: `${request}${jsExtension}`;
747771
}
@@ -886,6 +910,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
886910
autoExtension = true,
887911
autoExternal = true,
888912
externalHelpers = false,
913+
redirect = {},
889914
} = config;
890915
const formatConfig = composeFormatConfig(format!);
891916
const externalHelpersConfig = composeExternalHelpersConfig(
@@ -901,7 +926,11 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
901926
jsExtension,
902927
dtsExtension,
903928
} = composeAutoExtensionConfig(config, autoExtension, pkgJson);
904-
const bundleConfig = composeBundleConfig(jsExtension, config.bundle);
929+
const bundleConfig = composeBundleConfig(
930+
jsExtension,
931+
redirect,
932+
config.bundle,
933+
);
905934
const targetConfig = composeTargetConfig(config.output?.target);
906935
const syntaxConfig = composeSyntaxConfig(
907936
config?.syntax,

packages/core/src/types/config/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,20 @@ export type BannerAndFooter = {
4949
dts?: string;
5050
};
5151

52-
// export type Redirect = {
53-
// alias?: boolean;
54-
// style?: boolean;
55-
// asset?: boolean;
56-
// autoExtension?: boolean;
57-
// };
52+
export type Redirect = {
53+
// TODO: others
54+
// alias?: boolean;
55+
style?: boolean;
56+
// asset?: boolean;
57+
// autoExtension?: boolean;
58+
};
5859

5960
export interface LibConfig extends RsbuildConfig {
6061
bundle?: boolean;
6162
format?: Format;
6263
autoExtension?: boolean;
6364
autoExternal?: AutoExternal;
64-
// redirect?: Redirect;
65+
redirect?: Redirect;
6566
/** Support esX and browserslist query */
6667
syntax?: Syntax;
6768
externalHelpers?: boolean;

packages/lib-css-extract-loader/src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,32 @@ export const pitch: LoaderDefinition['pitch'] = function (request, _, _data) {
193193

194194
let importCssFiles = '';
195195

196-
// biome-ignore lint/complexity/noForEach: <explanation>
197-
dependencies.forEach(({ content, filepath }) => {
198-
const distFilepath = path
199-
.relative(rootDir, filepath)
200-
.replace(/\.module\.css/g, '_module.css');
196+
function getRelativePath(from: string, to: string) {
197+
let relativePath = path.relative(from, to);
198+
199+
if (
200+
!relativePath.startsWith('./') &&
201+
!relativePath.startsWith('../') &&
202+
!path.isAbsolute(relativePath)
203+
) {
204+
relativePath = `./${relativePath}`;
205+
}
206+
207+
return relativePath;
208+
}
201209

202-
console.log(distFilepath);
210+
for (const { content, filepath } of dependencies) {
211+
const distFilepath = getRelativePath(rootDir, filepath).replace(
212+
/\.module\.css/g,
213+
'_module.css',
214+
);
215+
216+
console.log(rootDir, filepath, distFilepath, 111);
203217

204218
this.emitFile(distFilepath, content);
205219
importCssFiles += '\n';
206220
importCssFiles += `import "${distFilepath}"`;
207-
});
221+
}
208222

209223
resultSource += importCssFiles;
210224

0 commit comments

Comments
 (0)