Skip to content
Closed
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
16 changes: 8 additions & 8 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ export function composeBannerFooterConfig(
banner: BannerAndFooter,
footer: BannerAndFooter,
): RsbuildConfig {
const bannerConfig = pick(banner, ['js', 'css']);
const footerConfig = pick(footer, ['js', 'css']);
const bannerConfig = pick(banner, ['js', 'css', 'raw']);
const footerConfig = pick(footer, ['js', 'css', 'raw']);

if (isEmptyObject(bannerConfig) && isEmptyObject(footerConfig)) {
return {};
Expand All @@ -342,7 +342,7 @@ export function composeBannerFooterConfig(
new rspack.BannerPlugin({
banner: bannerConfig.js,
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
raw: true,
raw: bannerConfig?.raw ?? true,
include: /\.(js|mjs|cjs)$/,
}),
);
Expand All @@ -352,7 +352,7 @@ export function composeBannerFooterConfig(
new rspack.BannerPlugin({
banner: bannerConfig.css,
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
raw: true,
raw: bannerConfig?.raw ?? true,
include: /\.(css)$/,
}),
);
Expand All @@ -365,7 +365,7 @@ export function composeBannerFooterConfig(
new rspack.BannerPlugin({
banner: footerConfig.js,
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
raw: true,
raw: footerConfig?.raw ?? true,
footer: true,
include: /\.(js|mjs|cjs)$/,
}),
Expand All @@ -376,7 +376,7 @@ export function composeBannerFooterConfig(
new rspack.BannerPlugin({
banner: footerConfig.css,
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
raw: true,
raw: footerConfig?.raw ?? true,
footer: true,
include: /\.(css)$/,
}),
Expand Down Expand Up @@ -934,8 +934,8 @@ const composeDtsConfig = async (
abortOnError: dts?.abortOnError ?? true,
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
autoExternal,
banner: banner?.dts,
footer: footer?.dts,
banner: { content: banner?.dts, raw: banner?.raw ?? true },
footer: { content: footer?.dts, raw: footer?.raw ?? true },
}),
],
};
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ export type AutoExternal =
peerDependencies?: boolean;
};

export type BannerAndFooterOptions = { raw?: boolean };

export type BannerAndFooter = {
js?: string;
css?: string;
dts?: string;
};
} & BannerAndFooterOptions;

export type Shims = {
cjs?: {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-dts/src/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import {
} from '@microsoft/api-extractor';
import { logger } from '@rsbuild/core';
import color from 'picocolors';
import type { DtsEntry } from './index';
import type { BannerAndFooter, DtsEntry } from './index';
import { addBannerAndFooter, getTimeCost } from './utils';

export type BundleOptions = {
name: string;
cwd: string;
outDir: string;
dtsExtension: string;
banner?: string;
footer?: string;
banner: BannerAndFooter;
footer: BannerAndFooter;
dtsEntry: DtsEntry;
tsconfigPath?: string;
bundledPackages?: string[];
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export type PluginDtsOptions = {
devDependencies?: boolean;
peerDependencies?: boolean;
};
banner?: string;
footer?: string;
banner: BannerAndFooter;
footer: BannerAndFooter;
};

export type BannerAndFooter = { content?: string; raw?: boolean };

export type DtsEntry = {
name?: string;
path?: string;
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from '@rsbuild/core';
import color from 'picocolors';
import type { BannerAndFooter } from 'src';
import ts from 'typescript';
import {
getFileLoc,
Expand All @@ -14,8 +15,8 @@ export type EmitDtsOptions = {
configPath: string;
declarationDir: string;
dtsExtension: string;
banner?: string;
footer?: string;
banner: BannerAndFooter;
footer: BannerAndFooter;
};

export async function emitDts(
Expand Down
32 changes: 22 additions & 10 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MagicString from 'magic-string';
import color from 'picocolors';
import { convertPathToPattern, glob } from 'tinyglobby';
import ts from 'typescript';
import type { DtsEntry } from './index';
import type { BannerAndFooter, DtsEntry } from './index';

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

export async function addBannerAndFooter(
file: string,
banner?: string,
footer?: string,
banner: BannerAndFooter,
footer: BannerAndFooter,
): Promise<void> {
if (!banner && !footer) {
if (!banner.content && !footer.content) {
return;
}

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

if (banner && !content.trimStart().startsWith(banner.trim())) {
code.prepend(`${banner}\n`);
if (
banner.content &&
!content.trimStart().startsWith(banner.content.trim()) &&
!content.trimStart().startsWith(`/*! ${banner.content.trim()} */`)
) {
code.prepend(
banner?.raw ? `${banner.content}\n` : `/*! ${banner.content} */\n`,
);
}

if (footer && !content.trimEnd().endsWith(footer.trim())) {
code.append(`\n${footer}\n`);
if (
footer.content &&
!content.trimEnd().endsWith(footer.content.trim()) &&
!content.trimEnd().endsWith(`/*! ${footer.content.trim()} */`)
) {
code.append(
footer?.raw ? `\n${footer.content}\n` : `\n/*! ${footer.content} */\n`,
);
}

if (code.hasChanged()) {
Expand All @@ -112,8 +124,8 @@ export async function processDtsFiles(
bundle: boolean,
dir: string,
dtsExtension: string,
banner?: string,
footer?: string,
banner: BannerAndFooter,
footer: BannerAndFooter,
): Promise<void> {
if (bundle) {
return;
Expand Down
16 changes: 14 additions & 2 deletions tests/integration/banner-footer/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'node:path';
import { buildAndGetResults } from 'test-helper';
import { expect, test } from 'vitest';

Expand All @@ -10,8 +11,7 @@ enum BannerFooter {
DTS_FOOTER = '/*! hello footer dts */',
}

test('banner and footer should work in js, css and dts', async () => {
const fixturePath = __dirname;
const testBannerAndFooter = async (fixturePath: string) => {
const { js, css, dts } = await buildAndGetResults({
fixturePath,
type: 'all',
Expand Down Expand Up @@ -57,4 +57,16 @@ test('banner and footer should work in js, css and dts', async () => {
checkBannerAndFooter(jsContents, 'js');
checkBannerAndFooter(cssContents, 'css');
checkBannerAndFooter(dtsContents, 'dts');
};

test('banner and footer should work in js, css and dts', async () => {
const fixturePath = path.join(__dirname, 'raw');

testBannerAndFooter(fixturePath);
});

test('banner and footer should work in js, css, and dts with raw option set to false', async () => {
const fixturePath = path.join(__dirname, 'raw-false');

testBannerAndFooter(fixturePath);
});
6 changes: 6 additions & 0 deletions tests/integration/banner-footer/raw-false/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "banner-footer-raw-false-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
102 changes: 102 additions & 0 deletions tests/integration/banner-footer/raw-false/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { type LibConfig, defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

const bannerFooterConfig: LibConfig = {
banner: {
js: 'hello banner js',
css: 'hello banner css',
dts: 'hello banner dts',
raw: false,
},
footer: {
js: 'hello footer js',
css: 'hello footer css',
dts: 'hello footer dts',
raw: false,
},
};

export default defineConfig({
lib: [
// bundle esm
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm/bundle',
},
},
dts: {
bundle: true,
},
...bannerFooterConfig,
}),
// bundle cjs
generateBundleCjsConfig({
output: {
distPath: {
root: './dist/cjs/bundle',
},
},
dts: {
bundle: true,
},
...bannerFooterConfig,
}),
// bundleless esm
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm/bundleless',
},
},
bundle: false,
dts: {
bundle: false,
},
// TODO: bundleless css
source: {
entry: {
index: ['./src/**/*.ts'],
},
},
...bannerFooterConfig,
}),
// bundleless cjs
generateBundleCjsConfig({
output: {
distPath: {
root: './dist/cjs/bundleless',
},
},
bundle: false,
dts: {
bundle: false,
},
// TODO: bundleless css
source: {
entry: {
index: ['./src/**/*.ts'],
},
},
...bannerFooterConfig,
}),
// bundle esm with minify enabled
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm/bundle-minify',
},
minify: true,
},
dts: {
bundle: true,
},
...bannerFooterConfig,
}),
],
source: {
entry: {
index: './src/index.ts',
},
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "banner-footer-test",
"name": "banner-footer-raw-test",
"version": "1.0.0",
"private": true,
"type": "module"
Expand Down
1 change: 1 addition & 0 deletions tests/integration/banner-footer/raw/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
3 changes: 3 additions & 0 deletions tests/integration/banner-footer/raw/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a {
color: black;
}
4 changes: 4 additions & 0 deletions tests/integration/banner-footer/raw/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// import './index.css';
import { foo } from './foo';

export const text = foo;
7 changes: 7 additions & 0 deletions tests/integration/banner-footer/raw/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}