Skip to content

Commit 8cc4cec

Browse files
committed
fix: css source map
1 parent 2941dc7 commit 8cc4cec

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ export default defineConfig({
3232
],
3333
output: {
3434
target: 'web',
35-
assetPrefix: 'auto', // TODO: move this line to packages/core/src/asset/assetConfig.ts
35+
assetPrefix: 'auto', // TODO: move this line to packages/core/src/asset/assetConfig.ts,
36+
sourceMap: {
37+
js: 'source-map',
38+
css: true,
39+
},
3640
},
3741
plugins: [pluginReact(), pluginSass()],
3842
});

packages/core/src/css/libCssExtractLoader.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,13 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function (
255255

256256
const m = new Map<string, string>();
257257

258-
for (const { content, filepath } of dependencies) {
258+
for (const { content, filepath, sourceMap } of dependencies) {
259259
let distFilepath = getRelativePath(rootDir, filepath);
260260
const ext = extname(distFilepath);
261261
if (ext !== 'css') {
262262
distFilepath = distFilepath.replace(ext, '.css');
263263
}
264264
distFilepath = distFilepath.replace(/\.module\.css/, '_module.css');
265-
266265
const cssFilename = path.basename(distFilepath);
267266
if (content.trim()) {
268267
m.get(distFilepath)
@@ -272,6 +271,13 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function (
272271
importCssFiles += '\n';
273272
importCssFiles += `import "./${cssFilename}"`;
274273
}
274+
if (sourceMap) {
275+
const sourceMapPath = `${distFilepath}.map`;
276+
m.set(sourceMapPath, `${sourceMap}`);
277+
// 将 source map 与 CSS 文件关联
278+
const sourceMappingURL = `/*# sourceMappingURL=${cssFilename}.map */`;
279+
m.set(distFilepath, `${m.get(distFilepath)}\n${sourceMappingURL}`);
280+
}
275281
}
276282
for (const [distFilepath, content] of m.entries()) {
277283
this.emitFile(distFilepath, content);

0 commit comments

Comments
 (0)