Skip to content

Commit f409e4d

Browse files
authored
fix: emit css source map in bundleless mode (#698)
1 parent 2941dc7 commit f409e4d

File tree

7 files changed

+53
-3
lines changed

7 files changed

+53
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ 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,
3636
},
3737
plugins: [pluginReact(), pluginSass()],
3838
});

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+
// Associate the source map with the CSS file
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);

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": "sourcemap-css-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [generateBundleEsmConfig({ bundle: false })],
6+
source: {
7+
entry: {
8+
index: ['./src/index.css'],
9+
},
10+
},
11+
output: {
12+
target: 'web',
13+
sourceMap: {
14+
css: true,
15+
},
16+
},
17+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.hello-world {
2+
color: #bfa;
3+
}

tests/integration/sourcemap/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ test('should generate js inline sourcemap: inline-cheap-module-source-map', asyn
4141

4242
expect(code[0]).toContain('//# sourceMappingURL=data:application/json');
4343
});
44+
45+
test('should generate css sourcemap file', async () => {
46+
const fixturePath = join(__dirname, 'css');
47+
const { contents } = await buildAndGetResults({ fixturePath, type: 'css' });
48+
const files = Object.keys(contents.esm);
49+
const code = Object.values(contents.esm);
50+
51+
expect(files).toMatchInlineSnapshot(`
52+
[
53+
"<ROOT>/tests/integration/sourcemap/css/dist/esm/index.css",
54+
"<ROOT>/tests/integration/sourcemap/css/dist/esm/index.css.map",
55+
]
56+
`);
57+
58+
expect(code[0]).toContain('/*# sourceMappingURL=index.css.map */');
59+
});

0 commit comments

Comments
 (0)