Skip to content

Commit e052b2e

Browse files
committed
feat: support raw: false option in banner and footer
1 parent 8dbb782 commit e052b2e

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

packages/core/src/config.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ export function composeBannerFooterConfig(
327327
banner: BannerAndFooter,
328328
footer: BannerAndFooter,
329329
): RsbuildConfig {
330-
const bannerConfig = pick(banner, ['js', 'css']);
331-
const footerConfig = pick(footer, ['js', 'css']);
330+
const bannerConfig = pick(banner, ['js', 'css', 'raw']);
331+
const footerConfig = pick(footer, ['js', 'css', 'raw']);
332332

333333
if (isEmptyObject(bannerConfig) && isEmptyObject(footerConfig)) {
334334
return {};
@@ -342,7 +342,7 @@ export function composeBannerFooterConfig(
342342
new rspack.BannerPlugin({
343343
banner: bannerConfig.js,
344344
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
345-
raw: true,
345+
raw: bannerConfig?.raw ?? true,
346346
include: /\.(js|mjs|cjs)$/,
347347
}),
348348
);
@@ -352,7 +352,7 @@ export function composeBannerFooterConfig(
352352
new rspack.BannerPlugin({
353353
banner: bannerConfig.css,
354354
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
355-
raw: true,
355+
raw: bannerConfig?.raw ?? true,
356356
include: /\.(css)$/,
357357
}),
358358
);
@@ -365,7 +365,7 @@ export function composeBannerFooterConfig(
365365
new rspack.BannerPlugin({
366366
banner: footerConfig.js,
367367
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
368-
raw: true,
368+
raw: footerConfig?.raw ?? true,
369369
footer: true,
370370
include: /\.(js|mjs|cjs)$/,
371371
}),
@@ -376,7 +376,7 @@ export function composeBannerFooterConfig(
376376
new rspack.BannerPlugin({
377377
banner: footerConfig.css,
378378
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
379-
raw: true,
379+
raw: footerConfig?.raw ?? true,
380380
footer: true,
381381
include: /\.(css)$/,
382382
}),
@@ -934,8 +934,8 @@ const composeDtsConfig = async (
934934
abortOnError: dts?.abortOnError ?? true,
935935
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
936936
autoExternal,
937-
banner: banner?.dts,
938-
footer: footer?.dts,
937+
banner: { content: banner?.dts, raw: banner?.raw ?? true },
938+
footer: { content: footer?.dts, raw: footer?.raw ?? true },
939939
}),
940940
],
941941
};

packages/core/src/types/config/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ export type AutoExternal =
4545
peerDependencies?: boolean;
4646
};
4747

48+
export type BannerAndFooterOptions = { raw?: boolean };
49+
4850
export type BannerAndFooter = {
4951
js?: string;
5052
css?: string;
5153
dts?: string;
52-
};
54+
} & BannerAndFooterOptions;
5355

5456
export type Shims = {
5557
cjs?: {

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import {
66
} from '@microsoft/api-extractor';
77
import { logger } from '@rsbuild/core';
88
import color from 'picocolors';
9-
import type { DtsEntry } from './index';
9+
import type { BannerAndFooter, DtsEntry } from './index';
1010
import { addBannerAndFooter, getTimeCost } from './utils';
1111

1212
export type BundleOptions = {
1313
name: string;
1414
cwd: string;
1515
outDir: string;
1616
dtsExtension: string;
17-
banner?: string;
18-
footer?: string;
17+
banner: BannerAndFooter;
18+
footer: BannerAndFooter;
1919
dtsEntry: DtsEntry;
2020
tsconfigPath?: string;
2121
bundledPackages?: string[];

packages/plugin-dts/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ export type PluginDtsOptions = {
2020
devDependencies?: boolean;
2121
peerDependencies?: boolean;
2222
};
23-
banner?: string;
24-
footer?: string;
23+
banner: BannerAndFooter;
24+
footer: BannerAndFooter;
2525
};
2626

27+
export type BannerAndFooter = { content?: string; raw?: boolean };
28+
2729
export type DtsEntry = {
2830
name?: string;
2931
path?: string;

packages/plugin-dts/src/tsc.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logger } from '@rsbuild/core';
22
import color from 'picocolors';
3+
import type { BannerAndFooter } from 'src';
34
import ts from 'typescript';
45
import {
56
getFileLoc,
@@ -14,8 +15,8 @@ export type EmitDtsOptions = {
1415
configPath: string;
1516
declarationDir: string;
1617
dtsExtension: string;
17-
banner?: string;
18-
footer?: string;
18+
banner: BannerAndFooter;
19+
footer: BannerAndFooter;
1920
};
2021

2122
export async function emitDts(

packages/plugin-dts/src/utils.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import MagicString from 'magic-string';
77
import color from 'picocolors';
88
import { convertPathToPattern, glob } from 'tinyglobby';
99
import ts from 'typescript';
10-
import type { DtsEntry } from './index';
10+
import type { BannerAndFooter, DtsEntry } from './index';
1111

1212
export function loadTsconfig(tsconfigPath: string): ts.ParsedCommandLine {
1313
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
@@ -85,22 +85,34 @@ export function getTimeCost(start: number): string {
8585

8686
export async function addBannerAndFooter(
8787
file: string,
88-
banner?: string,
89-
footer?: string,
88+
banner: BannerAndFooter,
89+
footer: BannerAndFooter,
9090
): Promise<void> {
91-
if (!banner && !footer) {
91+
if (!banner.content && !footer.content) {
9292
return;
9393
}
9494

9595
const content = await fsP.readFile(file, 'utf-8');
9696
const code = new MagicString(content);
9797

98-
if (banner && !content.trimStart().startsWith(banner.trim())) {
99-
code.prepend(`${banner}\n`);
98+
if (
99+
banner.content &&
100+
!content.trimStart().startsWith(banner.content.trim()) &&
101+
!content.trimStart().startsWith(`/*! ${banner.content.trim()} */`)
102+
) {
103+
code.prepend(
104+
banner?.raw ? `${banner.content}\n` : `/*! ${banner.content} */\n`,
105+
);
100106
}
101107

102-
if (footer && !content.trimEnd().endsWith(footer.trim())) {
103-
code.append(`\n${footer}\n`);
108+
if (
109+
footer.content &&
110+
!content.trimEnd().endsWith(footer.content.trim()) &&
111+
!content.trimEnd().endsWith(`/*! ${footer.content.trim()} */`)
112+
) {
113+
code.append(
114+
footer?.raw ? `\n${footer.content}\n` : `\n/*! ${footer.content} */\n`,
115+
);
104116
}
105117

106118
if (code.hasChanged()) {
@@ -112,8 +124,8 @@ export async function processDtsFiles(
112124
bundle: boolean,
113125
dir: string,
114126
dtsExtension: string,
115-
banner?: string,
116-
footer?: string,
127+
banner: BannerAndFooter,
128+
footer: BannerAndFooter,
117129
): Promise<void> {
118130
if (bundle) {
119131
return;

0 commit comments

Comments
 (0)