Skip to content

Commit

Permalink
fix: allow to only generate source map for CSS files
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jan 29, 2025
1 parent 1b5868d commit 35187aa
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
25 changes: 25 additions & 0 deletions e2e/cases/output/source-map/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,28 @@ test('should generate source map correctly in development build', async ({

await rsbuild.close();
});

test('should allow to only generate source map for CSS files', async () => {
const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
output: {
sourceMap: {
js: false,
css: true,
},
},
},
});

const files = await rsbuild.unwrapOutputJSON(false);

const jsMapFiles = Object.keys(files).filter((files) =>
files.endsWith('.js.map'),
);
const cssMapFiles = Object.keys(files).filter((files) =>
files.endsWith('.css.map'),
);
expect(jsMapFiles.length).toEqual(0);
expect(cssMapFiles.length).toBeGreaterThan(0);
});
2 changes: 2 additions & 0 deletions packages/compat/webpack/src/webpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export async function generateWebpackConfig({
DefinePlugin,
IgnorePlugin,
ProvidePlugin,
SourceMapDevToolPlugin,
HotModuleReplacementPlugin,
} = webpack;

Expand All @@ -115,6 +116,7 @@ export async function generateWebpackConfig({
DefinePlugin,
IgnorePlugin,
ProvidePlugin,
SourceMapDevToolPlugin,
HotModuleReplacementPlugin,
},
});
Expand Down
21 changes: 16 additions & 5 deletions packages/core/src/plugins/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import type {
Rspack,
} from '../types';

const getJsSourceMap = (
config: NormalizedEnvironmentConfig,
): Rspack.DevTool => {
const getDevtool = (config: NormalizedEnvironmentConfig): Rspack.DevTool => {
const { sourceMap } = config.output;
const isProd = config.mode === 'production';

Expand All @@ -25,7 +23,7 @@ const getJsSourceMap = (
};

/**
* Provide some basic configs of rspack
* Set some basic Rspack configs
*/
export const pluginBasic = (): RsbuildPlugin => ({
name: 'rsbuild:basic',
Expand All @@ -37,7 +35,20 @@ export const pluginBasic = (): RsbuildPlugin => ({

chain.name(environment.name);

chain.devtool(getJsSourceMap(config));
const devtool = getDevtool(config);
chain.devtool(devtool);

// When JS source map is disabled, but CSS source map is enabled,
// add `SourceMapDevToolPlugin` to let Rspack generate CSS source map.
const { sourceMap } = config.output;
if (!devtool && typeof sourceMap === 'object' && sourceMap.css) {
chain.plugin('source-map-css').use(bundler.SourceMapDevToolPlugin, [
{
test: /\.css$/,
filename: '[file].map[query]',
},
]);
}

// The base directory for resolving entry points and loaders from the configuration.
chain.context(api.context.rootPath);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/provider/rspackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export async function generateRspackConfig({
DefinePlugin,
IgnorePlugin,
ProvidePlugin,
SourceMapDevToolPlugin,
HotModuleReplacementPlugin,
} = rspack;

Expand All @@ -173,6 +174,7 @@ export async function generateRspackConfig({
DefinePlugin,
IgnorePlugin,
ProvidePlugin,
SourceMapDevToolPlugin,
HotModuleReplacementPlugin,
},
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export type ModifyBundlerChainUtils = ModifyChainUtils & {
DefinePlugin: PluginInstance;
IgnorePlugin: PluginInstance;
ProvidePlugin: PluginInstance;
SourceMapDevToolPlugin: PluginInstance;
HotModuleReplacementPlugin: PluginInstance;
};
};
Expand Down
6 changes: 1 addition & 5 deletions website/docs/en/plugins/dev/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,7 @@ type ModifyBundlerChainUtils = {
CHAIN_ID: ChainIdentifier;
HtmlPlugin: typeof import('html-rspack-plugin');
bundler: {
BannerPlugin: rspack.BannerPlugin;
DefinePlugin: rspack.DefinePlugin;
IgnorePlugin: rspack.IgnorePlugin;
ProvidePlugin: rspack.ProvidePlugin;
HotModuleReplacementPlugin: rspack.HotModuleReplacementPlugin;
// some Rspack built-in plugins
};
};

Expand Down
6 changes: 1 addition & 5 deletions website/docs/zh/plugins/dev/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,7 @@ type ModifyBundlerChainUtils = {
CHAIN_ID: ChainIdentifier;
HtmlPlugin: typeof import('html-rspack-plugin');
bundler: {
BannerPlugin: rspack.BannerPlugin;
DefinePlugin: rspack.DefinePlugin;
IgnorePlugin: rspack.IgnorePlugin;
ProvidePlugin: rspack.ProvidePlugin;
HotModuleReplacementPlugin: rspack.HotModuleReplacementPlugin;
// some Rspack built-in plugins
};
};

Expand Down

0 comments on commit 35187aa

Please sign in to comment.