From a1a29212e9bdbd3febae410c810644c122912941 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Thu, 20 Jun 2024 22:34:28 +0800 Subject: [PATCH] feat!: output.emitAssets changed to boolean type --- e2e/cases/emit-assets/rsbuild.config.ts | 2 +- .../svgr-disable-emit-asset/rsbuild.config.ts | 2 +- packages/core/src/config.ts | 2 +- packages/core/src/helpers.ts | 7 +++--- packages/core/src/mergeConfig.ts | 1 - packages/core/src/plugins/asset.ts | 14 +++++------ .../__snapshots__/environments.test.ts.snap | 12 +++++----- packages/shared/src/types/config/output.ts | 6 ++--- website/docs/en/config/output/emit-assets.mdx | 24 +++++++++++-------- website/docs/zh/config/output/emit-assets.mdx | 24 +++++++++++-------- 10 files changed, 50 insertions(+), 44 deletions(-) diff --git a/e2e/cases/emit-assets/rsbuild.config.ts b/e2e/cases/emit-assets/rsbuild.config.ts index 75bc593756..114f9f61df 100644 --- a/e2e/cases/emit-assets/rsbuild.config.ts +++ b/e2e/cases/emit-assets/rsbuild.config.ts @@ -3,7 +3,6 @@ import { defineConfig } from '@rsbuild/core'; export default defineConfig({ output: { filenameHash: false, - emitAssets: ({ target }) => target !== 'node', }, environments: { web: { @@ -14,6 +13,7 @@ export default defineConfig({ node: { output: { target: 'node', + emitAssets: false, }, }, }, diff --git a/e2e/cases/svg/svgr-disable-emit-asset/rsbuild.config.ts b/e2e/cases/svg/svgr-disable-emit-asset/rsbuild.config.ts index 59d8d66ec0..a7d44298cf 100644 --- a/e2e/cases/svg/svgr-disable-emit-asset/rsbuild.config.ts +++ b/e2e/cases/svg/svgr-disable-emit-asset/rsbuild.config.ts @@ -13,6 +13,6 @@ export default defineConfig({ ], output: { filenameHash: false, - emitAssets: () => false, + emitAssets: false, }, }); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 3effa84ff2..573867a69f 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -169,7 +169,7 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({ exportGlobals: false, exportLocalsConvention: 'camelCase', }, - emitAssets: () => true, + emitAssets: true, }); const createDefaultConfig = (): RsbuildConfig => ({ diff --git a/packages/core/src/helpers.ts b/packages/core/src/helpers.ts index 5b4059eca3..7a5142c857 100644 --- a/packages/core/src/helpers.ts +++ b/packages/core/src/helpers.ts @@ -6,6 +6,7 @@ import { type MultiStats, type NodeEnv, type NormalizedConfig, + type NormalizedEnvironmentConfig, type RsbuildTarget, type Rspack, type RspackChain, @@ -369,17 +370,17 @@ export const ensureAssetPrefix = (url: string, assetPrefix: string) => { }; export function getFilename( - config: NormalizedConfig, + config: NormalizedConfig | NormalizedEnvironmentConfig, type: 'js', isProd: boolean, ): NonNullable; export function getFilename( - config: NormalizedConfig, + config: NormalizedConfig | NormalizedEnvironmentConfig, type: Exclude, isProd: boolean, ): string; export function getFilename( - config: NormalizedConfig, + config: NormalizedConfig | NormalizedEnvironmentConfig, type: keyof FilenameConfig, isProd: boolean, ) { diff --git a/packages/core/src/mergeConfig.ts b/packages/core/src/mergeConfig.ts index 9da3a644d3..48d2eb015f 100644 --- a/packages/core/src/mergeConfig.ts +++ b/packages/core/src/mergeConfig.ts @@ -11,7 +11,6 @@ const OVERRIDE_PATH = [ 'output.inlineStyles', 'output.cssModules.auto', 'output.targets', - 'output.emitAssets', 'output.overrideBrowserslist', 'server.open', 'server.printUrls', diff --git a/packages/core/src/plugins/asset.ts b/packages/core/src/plugins/asset.ts index 3bca9b4aa0..5346262ddf 100644 --- a/packages/core/src/plugins/asset.ts +++ b/packages/core/src/plugins/asset.ts @@ -74,8 +74,8 @@ export const pluginAsset = (): RsbuildPlugin => ({ name: 'rsbuild:asset', setup(api) { - api.modifyBundlerChain((chain, { isProd, target }) => { - const config = api.getNormalizedConfig(); + api.modifyBundlerChain((chain, { isProd, environment }) => { + const config = api.getNormalizedConfig({ environment }); const createAssetRule = ( assetType: 'image' | 'media' | 'font' | 'svg', @@ -101,16 +101,16 @@ export const pluginAsset = (): RsbuildPlugin => ({ }); }; - const emit = config.output.emitAssets({ target }); + const { emitAssets } = config.output; - createAssetRule('image', IMAGE_EXTENSIONS, emit); - createAssetRule('svg', ['svg'], emit); + createAssetRule('image', IMAGE_EXTENSIONS, emitAssets); + createAssetRule('svg', ['svg'], emitAssets); createAssetRule( 'media', [...VIDEO_EXTENSIONS, ...AUDIO_EXTENSIONS], - emit, + emitAssets, ); - createAssetRule('font', FONT_EXTENSIONS, emit); + createAssetRule('font', FONT_EXTENSIONS, emitAssets); }); }, }); diff --git a/packages/core/tests/__snapshots__/environments.test.ts.snap b/packages/core/tests/__snapshots__/environments.test.ts.snap index 60de3a9ac7..035608a87d 100644 --- a/packages/core/tests/__snapshots__/environments.test.ts.snap +++ b/packages/core/tests/__snapshots__/environments.test.ts.snap @@ -56,7 +56,7 @@ exports[`environment config > should normalize environment config correctly 1`] "wasm": "static/wasm", "worker": "worker", }, - "emitAssets": [Function], + "emitAssets": true, "filename": {}, "filenameHash": true, "injectStyles": false, @@ -179,7 +179,7 @@ exports[`environment config > should print environment config when inspect confi "wasm": "static/wasm", "worker": "worker", }, - "emitAssets": [Function], + "emitAssets": true, "filename": {}, "filenameHash": true, "injectStyles": false, @@ -298,7 +298,7 @@ exports[`environment config > should print environment config when inspect confi "wasm": "static/wasm", "worker": "worker", }, - "emitAssets": [Function], + "emitAssets": true, "filename": {}, "filenameHash": true, "injectStyles": false, @@ -422,7 +422,7 @@ exports[`environment config > should support modify environment config by api.mo "wasm": "static/wasm", "worker": "worker", }, - "emitAssets": [Function], + "emitAssets": true, "filename": {}, "filenameHash": true, "injectStyles": false, @@ -541,7 +541,7 @@ exports[`environment config > should support modify environment config by api.mo "wasm": "static/wasm", "worker": "worker", }, - "emitAssets": [Function], + "emitAssets": true, "filename": {}, "filenameHash": true, "injectStyles": false, @@ -661,7 +661,7 @@ exports[`environment config > should support modify environment config by api.mo "wasm": "static/wasm", "worker": "worker", }, - "emitAssets": [Function], + "emitAssets": true, "filename": {}, "filenameHash": true, "injectStyles": false, diff --git a/packages/shared/src/types/config/output.ts b/packages/shared/src/types/config/output.ts index d23636814b..969c42419d 100644 --- a/packages/shared/src/types/config/output.ts +++ b/packages/shared/src/types/config/output.ts @@ -204,8 +204,6 @@ export type InlineChunkTestFunction = (params: { export type InlineChunkTest = RegExp | InlineChunkTestFunction; -export type EmitAssets = (params: { target: RsbuildTarget }) => boolean; - export interface OutputConfig { /** * Specify build target to run in specified environment. @@ -306,7 +304,7 @@ export interface OutputConfig { * Whether to emit static assets such as image, font, etc. * Return `false` to avoid outputting unnecessary assets for some scenarios such as SSR. */ - emitAssets?: EmitAssets; + emitAssets?: boolean; } export interface NormalizedOutputConfig extends OutputConfig { @@ -336,5 +334,5 @@ export interface NormalizedOutputConfig extends OutputConfig { localIdentName?: string; mode?: CSSModules['mode']; }; - emitAssets: EmitAssets; + emitAssets: boolean; } diff --git a/website/docs/en/config/output/emit-assets.mdx b/website/docs/en/config/output/emit-assets.mdx index 190814163d..2755a0e01c 100644 --- a/website/docs/en/config/output/emit-assets.mdx +++ b/website/docs/en/config/output/emit-assets.mdx @@ -1,12 +1,7 @@ # output.emitAssets -- **Type:** - -```ts -type EmitAssets = (params: { target: RsbuildTarget }) => boolean; -``` - -- **Default:** `() => true` +- **Type:** `boolean` +- **Default:** `true` Control whether to emit static assets such as images, fonts, audio, video, etc. @@ -18,9 +13,18 @@ For example, the following example will emit static assets when building web bun ```js export default { - output: { - targets: ['web', 'node'], - emitAssets: ({ target }) => target !== 'node', + environments: { + web: { + output: { + target: 'web', + }, + }, + node: { + output: { + target: 'node', + emitAssets: false, + }, + }, }, }; ``` diff --git a/website/docs/zh/config/output/emit-assets.mdx b/website/docs/zh/config/output/emit-assets.mdx index 1955e7f49c..57d0d22670 100644 --- a/website/docs/zh/config/output/emit-assets.mdx +++ b/website/docs/zh/config/output/emit-assets.mdx @@ -1,12 +1,7 @@ # output.emitAssets -- **类型:** - -```ts -type EmitAssets = (params: { target: RsbuildTarget }) => boolean; -``` - -- **默认值:** `() => true` +- **类型:** `boolean` +- **默认值:** `true` 用于控制是否输出图片、字体、音频、视频等静态资源。 @@ -18,9 +13,18 @@ type EmitAssets = (params: { target: RsbuildTarget }) => boolean; ```js export default { - output: { - targets: ['web', 'node'], - emitAssets: ({ target }) => target !== 'node', + environments: { + web: { + output: { + target: 'web', + }, + }, + node: { + output: { + target: 'node', + emitAssets: false, + }, + }, }, }; ```