From 51e29b8aa8f9bddbb1e8a04d046f3ae26ca90ce1 Mon Sep 17 00:00:00 2001 From: SoonIter Date: Fri, 10 Jan 2025 18:10:57 +0800 Subject: [PATCH] chore: update --- .../react-component-bundle/rslib.config.ts | 2 -- packages/core/src/asset/assetConfig.ts | 34 +++++++++++------- packages/core/src/css/LibCssExtractPlugin.ts | 3 +- .../tests/__snapshots__/config.test.ts.snap | 36 ++++++++----------- tests/integration/asset/index.test.ts | 1 - 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/examples/react-component-bundle/rslib.config.ts b/examples/react-component-bundle/rslib.config.ts index 1852b87e5..59d42518d 100644 --- a/examples/react-component-bundle/rslib.config.ts +++ b/examples/react-component-bundle/rslib.config.ts @@ -7,7 +7,6 @@ export default defineConfig({ { format: 'esm', dts: true, - // syntax: 'es5', output: { distPath: { root: './dist/esm', @@ -16,7 +15,6 @@ export default defineConfig({ }, { format: 'cjs', - syntax: 'es5', dts: true, output: { distPath: { diff --git a/packages/core/src/asset/assetConfig.ts b/packages/core/src/asset/assetConfig.ts index e322de259..7b65cc4ca 100644 --- a/packages/core/src/asset/assetConfig.ts +++ b/packages/core/src/asset/assetConfig.ts @@ -2,7 +2,7 @@ import type { EnvironmentConfig, RsbuildPlugin } from '@rsbuild/core'; import type { Format } from '../types'; import { LibAssetExtractPlugin } from './LibAssetExtractPlugin'; -const PLUGIN_NAME = 'rsbuild:lib-asset-bundleless'; +const PLUGIN_NAME = 'rsbuild:lib-asset'; const RSBUILD_SVGR_PLUGIN_NAME = 'rsbuild:svgr'; const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({ @@ -10,23 +10,39 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({ pre: [RSBUILD_SVGR_PLUGIN_NAME], setup(api) { api.modifyBundlerChain((config, { CHAIN_ID }) => { - // only support transform the svg asset to mixedImport svgr file - // remove issuer to make every svg asset is transformed const isUsingSvgr = Boolean( config.module .rule(CHAIN_ID.RULE.SVG) .oneOf(CHAIN_ID.RULE.SVG) .uses.has(CHAIN_ID.USE.SVGR), ); - if (isUsingSvgr) { + if (isUsingSvgr && !bundle) { + // in bundleless, only support transform the svg asset to mixedImport svgr file + // remove issuer to make every svg asset is transformed const rule = config.module .rule(CHAIN_ID.RULE.SVG) - .oneOf(CHAIN_ID.RULE.SVG); + .oneOf(CHAIN_ID.ONE_OF.SVG); rule.issuer([]); } config .plugin(LibAssetExtractPlugin.name) .use(LibAssetExtractPlugin, [{ bundle, isUsingSvgr }]); + + if (bundle) { + // preserve './' in css url + // https://github.com/web-infra-dev/rspack/pull/8946 + config.plugins.get(CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT)?.tap((options) => { + if (bundle) { + return [ + { + ...options[0], + enforceRelative: true, + }, + ]; + } + return options; + }); + } }); }, }); @@ -43,13 +59,7 @@ export const composeAssetConfig = ( dataUriLimit: 0, // default: no inline asset assetPrefix: 'auto', }, - tools: { - rspack: { - plugins: [ - new LibAssetExtractPlugin({ bundle: true, isUsingSvgr: false }), - ], - }, - }, + plugins: [pluginLibAsset({ bundle: true })], }; } return { diff --git a/packages/core/src/css/LibCssExtractPlugin.ts b/packages/core/src/css/LibCssExtractPlugin.ts index 541c9d5e7..18001e841 100644 --- a/packages/core/src/css/LibCssExtractPlugin.ts +++ b/packages/core/src/css/LibCssExtractPlugin.ts @@ -67,7 +67,8 @@ class LibCssExtractPlugin implements Rspack.RspackPluginInstance { const undoPath = getUndoPath( name, compilation.outputOptions.path!, - false, + // https://github.com/web-infra-dev/rspack/pull/8946 + true, ); replace(`${ABSOLUTE_PUBLIC_PATH}${AUTO_PUBLIC_PATH}`, undoPath); replace(ABSOLUTE_PUBLIC_PATH, ''); diff --git a/packages/core/tests/__snapshots__/config.test.ts.snap b/packages/core/tests/__snapshots__/config.test.ts.snap index c0d42d89c..5b9221534 100644 --- a/packages/core/tests/__snapshots__/config.test.ts.snap +++ b/packages/core/tests/__snapshots__/config.test.ts.snap @@ -114,6 +114,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i "name": "rsbuild:format", "setup": [Function], }, + { + "name": "rsbuild:lib-asset", + "pre": [ + "rsbuild:svgr", + ], + "setup": [Function], + }, { "name": "rsbuild:lib-entry-chunk", "setup": [Function], @@ -214,17 +221,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i { "externalsType": "module-import", }, - { - "plugins": [ - LibAssetExtractPlugin { - "name": "LIB_ASSET_EXTRACT_PLUGIN", - "options": { - "bundle": true, - "isUsingSvgr": false, - }, - }, - ], - }, { "plugins": [ EntryChunkPlugin { @@ -369,6 +365,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i "name": "rsbuild:cjs-import-meta-url-shim", "setup": [Function], }, + { + "name": "rsbuild:lib-asset", + "pre": [ + "rsbuild:svgr", + ], + "setup": [Function], + }, { "name": "rsbuild:lib-entry-chunk", "setup": [Function], @@ -461,17 +464,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i { "externalsType": "commonjs-import", }, - { - "plugins": [ - LibAssetExtractPlugin { - "name": "LIB_ASSET_EXTRACT_PLUGIN", - "options": { - "bundle": true, - "isUsingSvgr": false, - }, - }, - ], - }, { "plugins": [ EntryChunkPlugin { diff --git a/tests/integration/asset/index.test.ts b/tests/integration/asset/index.test.ts index dea543168..21e0e0847 100644 --- a/tests/integration/asset/index.test.ts +++ b/tests/integration/asset/index.test.ts @@ -137,7 +137,6 @@ test('set the assets output path', async () => { test('set the assets public path', async () => { const fixturePath = join(__dirname, 'public-path'); const { contents } = await buildAndGetResults({ fixturePath }); - // umd should preserve '__webpack_require__.p' const { content: indexUmdJs } = queryContent(contents.umd!, /index\.js/);