Skip to content

Commit ee425c1

Browse files
authored
feat: support banner and footer (#175)
1 parent eac6954 commit ee425c1

File tree

20 files changed

+377
-14
lines changed

20 files changed

+377
-14
lines changed

.changeset/nice-hotels-cheer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'rsbuild-plugin-dts': patch
3+
'@rslib/core': patch
4+
---
5+
6+
release 0.0.5

e2e/cases/banner-footer/index.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { buildAndGetResults } from '@e2e/helper';
2+
import { expect, test } from 'vitest';
3+
4+
enum BannerFooter {
5+
JS_BANNER = '/*! hello banner js */',
6+
JS_FOOTER = '/*! hello footer js */',
7+
CSS_BANNER = '/*! hello banner css */',
8+
CSS_FOOTER = '/*! hello footer css */',
9+
DTS_BANNER = '/*! hello banner dts */',
10+
DTS_FOOTER = '/*! hello footer dts */',
11+
}
12+
13+
test('banner and footer should work in js, css and dts', async () => {
14+
const fixturePath = __dirname;
15+
const { js, css, dts } = await buildAndGetResults(fixturePath, 'all');
16+
17+
const jsContents = Object.values(js.contents);
18+
const cssContents = Object.values(css.contents);
19+
const dtsContents = Object.values(dts.contents);
20+
21+
// There are 5 cases included:
22+
// 1. bundle esm
23+
// 2. bundle cjs
24+
// 3. bundleless esm
25+
// 4. bundleless cjs
26+
// 5. bundle esm with minify enabled
27+
const checkBannerAndFooter = (
28+
contents: Record<string, string>[],
29+
type: 'js' | 'css' | 'dts',
30+
) => {
31+
for (const content of Object.values(contents)) {
32+
if (content) {
33+
const expectedBanner =
34+
type === 'js'
35+
? BannerFooter.JS_BANNER
36+
: type === 'css'
37+
? BannerFooter.CSS_BANNER
38+
: BannerFooter.DTS_BANNER;
39+
const expectedFooter =
40+
type === 'js'
41+
? BannerFooter.JS_FOOTER
42+
: type === 'css'
43+
? BannerFooter.CSS_FOOTER
44+
: BannerFooter.DTS_FOOTER;
45+
46+
for (const value of Object.values(content)) {
47+
expect(value).toContain(expectedBanner);
48+
expect(value).toContain(expectedFooter);
49+
}
50+
}
51+
}
52+
};
53+
54+
checkBannerAndFooter(jsContents, 'js');
55+
checkBannerAndFooter(cssContents, 'css');
56+
checkBannerAndFooter(dtsContents, 'dts');
57+
});

e2e/cases/banner-footer/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "banner-footer-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
2+
import { type LibConfig, defineConfig } from '@rslib/core';
3+
4+
const bannerFooterConfig: LibConfig = {
5+
banner: {
6+
js: '/*! hello banner js */',
7+
css: '/*! hello banner css */',
8+
dts: '/*! hello banner dts */',
9+
},
10+
footer: {
11+
js: '/*! hello footer js */',
12+
css: '/*! hello footer css */',
13+
dts: '/*! hello footer dts */',
14+
},
15+
};
16+
17+
export default defineConfig({
18+
lib: [
19+
// bundle esm
20+
generateBundleEsmConfig({
21+
output: {
22+
distPath: {
23+
root: './dist/esm/bundle',
24+
},
25+
},
26+
dts: {
27+
bundle: true,
28+
},
29+
...bannerFooterConfig,
30+
}),
31+
// bundle cjs
32+
generateBundleCjsConfig({
33+
output: {
34+
distPath: {
35+
root: './dist/cjs/bundle',
36+
},
37+
},
38+
dts: {
39+
bundle: true,
40+
},
41+
...bannerFooterConfig,
42+
}),
43+
// bundleless esm
44+
generateBundleEsmConfig({
45+
output: {
46+
distPath: {
47+
root: './dist/esm/bundleless',
48+
},
49+
},
50+
bundle: false,
51+
dts: {
52+
bundle: false,
53+
},
54+
// TODO: bundleless css
55+
source: {
56+
entry: {
57+
index: ['./src/**/*.ts'],
58+
},
59+
},
60+
...bannerFooterConfig,
61+
}),
62+
// bundleless cjs
63+
generateBundleCjsConfig({
64+
output: {
65+
distPath: {
66+
root: './dist/cjs/bundleless',
67+
},
68+
},
69+
bundle: false,
70+
dts: {
71+
bundle: false,
72+
},
73+
// TODO: bundleless css
74+
source: {
75+
entry: {
76+
index: ['./src/**/*.ts'],
77+
},
78+
},
79+
...bannerFooterConfig,
80+
}),
81+
// bundle esm with minify enabled
82+
generateBundleEsmConfig({
83+
output: {
84+
distPath: {
85+
root: './dist/esm/bundle-minify',
86+
},
87+
minify: true,
88+
},
89+
dts: {
90+
bundle: true,
91+
},
92+
...bannerFooterConfig,
93+
}),
94+
],
95+
source: {
96+
entry: {
97+
index: './src/index.ts',
98+
},
99+
},
100+
});

e2e/cases/banner-footer/src/foo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'foo';

e2e/cases/banner-footer/src/index.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
color: black;
3+
}

e2e/cases/banner-footer/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './index.css';
2+
import { foo } from './foo';
3+
4+
export const text = foo;

e2e/cases/banner-footer/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./"
5+
},
6+
"include": ["src"]
7+
}

e2e/cases/extension-alias/rslib.config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@ export default defineConfig({
77
entry: {
88
index: './src/index.ts',
99
},
10-
alias: {
11-
'@src': 'src',
12-
},
1310
},
1411
});

e2e/scripts/shared.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type BuildResult = {
5050

5151
export async function getResults(
5252
rslibConfig: RslibConfig,
53-
type: 'js' | 'dts',
53+
type: 'js' | 'dts' | 'css',
5454
): Promise<Omit<BuildResult, 'rspackConfig' | 'rsbuildConfig' | 'isSuccess'>> {
5555
const files: Record<string, string[]> = {};
5656
const contents: Record<string, Record<string, string>> = {};
@@ -79,7 +79,7 @@ export async function getResults(
7979
key = currentFormatCount === 1 ? format! : `${format}${currentFormatIndex}`;
8080

8181
let globFolder = '';
82-
if (type === 'js') {
82+
if (type === 'js' || type === 'css') {
8383
globFolder = libConfig?.output?.distPath?.root!;
8484
} else if (type === 'dts' && libConfig.dts !== false) {
8585
globFolder =
@@ -88,7 +88,12 @@ export async function getResults(
8888

8989
if (!globFolder) continue;
9090

91-
const regex = type === 'dts' ? /\.d.(ts|cts|mts)$/ : /\.(js|cjs|mjs)$/;
91+
const regex =
92+
type === 'dts'
93+
? /\.d.(ts|cts|mts)$/
94+
: type === 'css'
95+
? /\.css$/
96+
: /\.(js|cjs|mjs)$/;
9297

9398
const content: Record<string, string> = await globContentJSON(globFolder, {
9499
absolute: true,
@@ -138,14 +143,15 @@ export async function buildAndGetResults(
138143
): Promise<{
139144
js: BuildResult;
140145
dts: BuildResult;
146+
css: BuildResult;
141147
}>;
142148
export async function buildAndGetResults(
143149
fixturePath: string,
144-
type?: 'js' | 'dts',
150+
type?: 'js' | 'dts' | 'css',
145151
): Promise<BuildResult>;
146152
export async function buildAndGetResults(
147153
fixturePath: string,
148-
type: 'js' | 'dts' | 'all' = 'js',
154+
type: 'js' | 'dts' | 'css' | 'all' = 'js',
149155
) {
150156
const rslibConfig = await loadConfig({
151157
cwd: fixturePath,
@@ -158,6 +164,7 @@ export async function buildAndGetResults(
158164
if (type === 'all') {
159165
const jsResults = await getResults(rslibConfig, 'js');
160166
const dtsResults = await getResults(rslibConfig, 'dts');
167+
const cssResults = await getResults(rslibConfig, 'css');
161168
return {
162169
js: {
163170
contents: jsResults.contents,
@@ -177,6 +184,15 @@ export async function buildAndGetResults(
177184
rsbuildConfig: rsbuildConfig,
178185
isSuccess: Boolean(rsbuildInstance),
179186
},
187+
css: {
188+
contents: cssResults.contents,
189+
files: cssResults.files,
190+
entries: cssResults.entries,
191+
entryFiles: cssResults.entryFiles,
192+
rspackConfig: bundlerConfigs,
193+
rsbuildConfig: rsbuildConfig,
194+
isSuccess: Boolean(rsbuildInstance),
195+
},
180196
};
181197
}
182198

0 commit comments

Comments
 (0)