Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: output.emitAssets changed to boolean type #2649

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/cases/emit-assets/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { defineConfig } from '@rsbuild/core';
export default defineConfig({
output: {
filenameHash: false,
emitAssets: ({ target }) => target !== 'node',
},
environments: {
web: {
Expand All @@ -14,6 +13,7 @@ export default defineConfig({
node: {
output: {
target: 'node',
emitAssets: false,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/svg/svgr-disable-emit-asset/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export default defineConfig({
],
output: {
filenameHash: false,
emitAssets: () => false,
emitAssets: false,
},
});
2 changes: 1 addition & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({
exportGlobals: false,
exportLocalsConvention: 'camelCase',
},
emitAssets: () => true,
emitAssets: true,
});

const createDefaultConfig = (): RsbuildConfig => ({
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type MultiStats,
type NodeEnv,
type NormalizedConfig,
type NormalizedEnvironmentConfig,
type RsbuildTarget,
type Rspack,
type RspackChain,
Expand Down Expand Up @@ -369,17 +370,17 @@ export const ensureAssetPrefix = (url: string, assetPrefix: string) => {
};

export function getFilename(
config: NormalizedConfig,
config: NormalizedConfig | NormalizedEnvironmentConfig,
type: 'js',
isProd: boolean,
): NonNullable<FilenameConfig['js']>;
export function getFilename(
config: NormalizedConfig,
config: NormalizedConfig | NormalizedEnvironmentConfig,
type: Exclude<keyof FilenameConfig, 'js'>,
isProd: boolean,
): string;
export function getFilename(
config: NormalizedConfig,
config: NormalizedConfig | NormalizedEnvironmentConfig,
type: keyof FilenameConfig,
isProd: boolean,
) {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/mergeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const OVERRIDE_PATH = [
'output.inlineStyles',
'output.cssModules.auto',
'output.targets',
'output.emitAssets',
'output.overrideBrowserslist',
'server.open',
'server.printUrls',
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
});
},
});
12 changes: 6 additions & 6 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions packages/shared/src/types/config/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -336,5 +334,5 @@ export interface NormalizedOutputConfig extends OutputConfig {
localIdentName?: string;
mode?: CSSModules['mode'];
};
emitAssets: EmitAssets;
emitAssets: boolean;
}
24 changes: 14 additions & 10 deletions website/docs/en/config/output/emit-assets.mdx
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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,
},
},
},
};
```
24 changes: 14 additions & 10 deletions website/docs/zh/config/output/emit-assets.mdx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# output.emitAssets

- **类型:**

```ts
type EmitAssets = (params: { target: RsbuildTarget }) => boolean;
```

- **默认值:** `() => true`
- **类型:** `boolean`
- **默认值:** `true`

用于控制是否输出图片、字体、音频、视频等静态资源。

Expand All @@ -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,
},
},
},
};
```
Loading