diff --git a/examples/react-component-bundle-false/rslib.config.ts b/examples/react-component-bundle-false/rslib.config.ts index 217476eed..e02f94d36 100644 --- a/examples/react-component-bundle-false/rslib.config.ts +++ b/examples/react-component-bundle-false/rslib.config.ts @@ -32,7 +32,7 @@ export default defineConfig({ ], output: { target: 'web', - assetPrefix: 'auto', // TODO: move this line to packages/core/src/asset/assetConfig.ts + assetPrefix: 'auto', // TODO: move this line to packages/core/src/asset/assetConfig.ts, }, plugins: [pluginReact(), pluginSass()], }); diff --git a/packages/core/src/css/libCssExtractLoader.ts b/packages/core/src/css/libCssExtractLoader.ts index 6faa2aae9..1aaecf81c 100644 --- a/packages/core/src/css/libCssExtractLoader.ts +++ b/packages/core/src/css/libCssExtractLoader.ts @@ -255,14 +255,13 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function ( const m = new Map(); - for (const { content, filepath } of dependencies) { + for (const { content, filepath, sourceMap } of dependencies) { let distFilepath = getRelativePath(rootDir, filepath); const ext = extname(distFilepath); if (ext !== 'css') { distFilepath = distFilepath.replace(ext, '.css'); } distFilepath = distFilepath.replace(/\.module\.css/, '_module.css'); - const cssFilename = path.basename(distFilepath); if (content.trim()) { m.get(distFilepath) @@ -272,6 +271,13 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function ( importCssFiles += '\n'; importCssFiles += `import "./${cssFilename}"`; } + if (sourceMap) { + const sourceMapPath = `${distFilepath}.map`; + m.set(sourceMapPath, `${sourceMap}`); + // Associate the source map with the CSS file + const sourceMappingURL = `/*# sourceMappingURL=${cssFilename}.map */`; + m.set(distFilepath, `${m.get(distFilepath)}\n${sourceMappingURL}`); + } } for (const [distFilepath, content] of m.entries()) { this.emitFile(distFilepath, content); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc4104949..2bbb00e47 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -800,6 +800,8 @@ importers: tests/integration/shims/esm: {} + tests/integration/sourcemap/css: {} + tests/integration/sourcemap/default: {} tests/integration/sourcemap/external: {} diff --git a/tests/integration/sourcemap/css/package.json b/tests/integration/sourcemap/css/package.json new file mode 100644 index 000000000..566ed3bd0 --- /dev/null +++ b/tests/integration/sourcemap/css/package.json @@ -0,0 +1,6 @@ +{ + "name": "sourcemap-css-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/sourcemap/css/rslib.config.ts b/tests/integration/sourcemap/css/rslib.config.ts new file mode 100644 index 000000000..2e82f758e --- /dev/null +++ b/tests/integration/sourcemap/css/rslib.config.ts @@ -0,0 +1,17 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [generateBundleEsmConfig({ bundle: false })], + source: { + entry: { + index: ['./src/index.css'], + }, + }, + output: { + target: 'web', + sourceMap: { + css: true, + }, + }, +}); diff --git a/tests/integration/sourcemap/css/src/index.css b/tests/integration/sourcemap/css/src/index.css new file mode 100644 index 000000000..2943d6719 --- /dev/null +++ b/tests/integration/sourcemap/css/src/index.css @@ -0,0 +1,3 @@ +.hello-world { + color: #bfa; +} diff --git a/tests/integration/sourcemap/index.test.ts b/tests/integration/sourcemap/index.test.ts index 7ddc464b7..bc4e348a4 100644 --- a/tests/integration/sourcemap/index.test.ts +++ b/tests/integration/sourcemap/index.test.ts @@ -41,3 +41,19 @@ test('should generate js inline sourcemap: inline-cheap-module-source-map', asyn expect(code[0]).toContain('//# sourceMappingURL=data:application/json'); }); + +test('should generate css sourcemap file', async () => { + const fixturePath = join(__dirname, 'css'); + const { contents } = await buildAndGetResults({ fixturePath, type: 'css' }); + const files = Object.keys(contents.esm); + const code = Object.values(contents.esm); + + expect(files).toMatchInlineSnapshot(` + [ + "/tests/integration/sourcemap/css/dist/esm/index.css", + "/tests/integration/sourcemap/css/dist/esm/index.css.map", + ] + `); + + expect(code[0]).toContain('/*# sourceMappingURL=index.css.map */'); +});