Skip to content

Commit 6855247

Browse files
authored
feat(rspeedy): support source.preEntry (#750)
<!-- Thank you for submitting a pull request! We appreciate the time and effort you have invested in making these changes. Please ensure that you provide enough information to allow others to review your pull request. Upon submission, your pull request will be automatically assigned with reviewers. If you want to learn more about contributing to this project, please visit: https://github.com/lynx-family/lynx-stack/blob/main/CONTRIBUTING.md. --> ## Summary <!-- Can you explain the reasoning behind implementing this change? What problem or issue does this pull request resolve? --> <!-- It would be helpful if you could provide any relevant context, such as GitHub issues or related discussions. --> close #299 When we set `preEntry` in the `lynx.config.js`: ```js source: { entry: { main: './src/index.tsx', main2: './src/index.tsx', }, preEntry: './src/normalize.css', }, ``` The `rspack.config.lynx.mjs` will automatically generate entries with the following structure: ```js main__main-thread: { layer: 'react:main-thread', 'import': [ '/hotModuleReplacement.lepus.cjs', './src/normalize.css', './src/index.tsx' ], filename: '.rspeedy/main/main-thread.js' }, main: { layer: 'react:background', 'import': [ '@lynx-js/react/refresh', '@lynx-js/webpack-dev-transport/client', '@rspack/core/hot/dev-server', './src/normalize.css', './src/index.tsx' ], filename: '.rspeedy/main/background.js' }, main2__main-thread: { layer: 'react:main-thread', 'import': [ '/hotModuleReplacement.lepus.cjs', './src/normalize.css', './src/index.tsx' ], filename: '.rspeedy/main/main-thread.js' }, main2: { layer: 'react:background', 'import': [ '@lynx-js/react/refresh', '@lynx-js/webpack-dev-transport/client', '@rspack/core/hot/dev-server', './src/normalize.css', './src/index.tsx' ], filename: '.rspeedy/main/background.js' }, ``` The CSS files (`main.css` and `main2.css`) will contain: ```css /* from preEntry normalize.css */ .test { background-color: red; } /* The others */ :root { background-color: #000; --color-text: #fff; } ``` ## Checklist <!--- Check and mark with an "x" --> - [x] Tests updated (or not required). - [x] Documentation updated (or not required). --------- Signed-off-by: BitterGourd <[email protected]>
1 parent 71db06c commit 6855247

File tree

7 files changed

+129
-17
lines changed

7 files changed

+129
-17
lines changed

.changeset/cold-terms-battle.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
"@lynx-js/rspeedy": patch
3+
---
4+
5+
Support `source.preEntry`.
6+
7+
Add a script before the entry file of each page. This script will be executed before the page code.
8+
It can be used to execute global logics, such as injecting polyfills, setting global styles, etc.
9+
10+
example:
11+
12+
```js
13+
import { defineConfig } from '@lynx-js/rspeedy';
14+
export default defineConfig({
15+
source: {
16+
preEntry: './src/polyfill.ts',
17+
},
18+
});
19+
```

packages/rspeedy/core/etc/rspeedy.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ export interface Source {
280280
entry?: Entry | undefined;
281281
exclude?: Rspack.RuleSetCondition[] | undefined;
282282
include?: Rspack.RuleSetCondition[] | undefined;
283+
preEntry?: string | string[] | undefined;
283284
transformImport?: TransformImport[] | undefined;
284285
tsconfigPath?: string | undefined;
285286
}

packages/rspeedy/core/src/config/rsbuild/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export function toRsbuildConfig(
6868

6969
include: config.source?.include,
7070

71+
preEntry: config.source?.preEntry,
72+
7173
transformImport: config.source?.transformImport,
7274

7375
tsconfigPath: config.source?.tsconfigPath,

packages/rspeedy/core/src/config/source/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,29 @@ export interface Source {
429429
*/
430430
include?: Rspack.RuleSetCondition[] | undefined
431431

432+
/**
433+
* Add a script before the entry file of each page. This script will be executed before the page code.
434+
* It can be used to execute global logics, such as injecting polyfills, setting global styles, etc.
435+
*
436+
* @remarks
437+
*
438+
* See {@link https://rsbuild.dev/config/source/pre-entry | source.preEntry} for more details.
439+
*
440+
* @example
441+
*
442+
* Relative path will be resolved relative to the project root directory.
443+
*
444+
* ```js
445+
* import { defineConfig } from '@lynx-js/rspeedy'
446+
* export default defineConfig({
447+
* source: {
448+
* preEntry: './src/polyfill.ts',
449+
* },
450+
* })
451+
* ```
452+
*/
453+
preEntry?: string | string[] | undefined
454+
432455
/**
433456
* The {@link TransformImport} option transforms the import paths to enable modular imports from subpaths of third-party packages, similar to the functionality provided by {@link https://npmjs.com/package/babel-plugin-import | babel-plugin-import}.
434457
*

packages/rspeedy/core/test/config/rsbuild.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,5 +803,34 @@ describe('Config - toRsBuildConfig', () => {
803803
}
804804
`)
805805
})
806+
807+
test('source.preEntry string', () => {
808+
const rsbuildConfig = toRsbuildConfig({
809+
source: {
810+
preEntry: './src/polyfill.ts',
811+
},
812+
})
813+
814+
expect(rsbuildConfig.source?.preEntry).toMatchInlineSnapshot(
815+
`"./src/polyfill.ts"`,
816+
)
817+
})
818+
819+
test('source.preEntry string[]', () => {
820+
const rsbuildConfig = toRsbuildConfig({
821+
source: {
822+
preEntry: ['./src/polyfill-a.ts', './src/polyfill-b.ts'],
823+
},
824+
})
825+
826+
expect(rsbuildConfig.source?.preEntry).toMatchInlineSnapshot(
827+
`
828+
[
829+
"./src/polyfill-a.ts",
830+
"./src/polyfill-b.ts",
831+
]
832+
`,
833+
)
834+
})
806835
})
807836
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2024 The Lynx Authors. All rights reserved.
2+
// Licensed under the Apache License Version 2.0 that can be found in the
3+
// LICENSE file in the root directory of this source tree.
4+
import { assertType, describe, test } from 'vitest'
5+
6+
import type { Source } from '../../../src/index.js'
7+
8+
describe('Config - source.preEntry', () => {
9+
test('preEntry', () => {
10+
assertType<Source>({
11+
preEntry: undefined,
12+
})
13+
14+
assertType<Source>({
15+
preEntry: './src/polyfill.ts',
16+
})
17+
18+
assertType<Source>({
19+
preEntry: ['./src/polyfill-a.ts', './src/polyfill-b.ts'],
20+
})
21+
})
22+
})

packages/rspeedy/core/test/config/validate.test.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,23 @@ describe('Config Validation', () => {
18221822
test('valid type', () => {
18231823
const cases: Source[] = [
18241824
{},
1825+
{
1826+
assetsInclude: 'json5',
1827+
},
1828+
{
1829+
assetsInclude: /\.json5$/,
1830+
},
1831+
{
1832+
assetsInclude: [/\.json5$/, /\.pdf$/],
1833+
},
1834+
{
1835+
assetsInclude: (value: string) => value.endsWith('.json5'),
1836+
},
1837+
{
1838+
assetsInclude: {
1839+
not: /\.json5$/,
1840+
},
1841+
},
18251842
{ decorators: {} },
18261843
{ decorators: { version: '2022-03' } },
18271844
{ decorators: { version: 'legacy' } },
@@ -1879,6 +1896,12 @@ describe('Config Validation', () => {
18791896
{ not: /core-js/ },
18801897
],
18811898
},
1899+
{
1900+
preEntry: './src/polyfill.ts',
1901+
},
1902+
{
1903+
preEntry: ['./src/polyfill-a.ts', './src/polyfill-b.ts'],
1904+
},
18821905
{ transformImport: [] },
18831906
{
18841907
transformImport: [
@@ -1903,23 +1926,6 @@ describe('Config Validation', () => {
19031926
},
19041927
],
19051928
},
1906-
{
1907-
assetsInclude: 'json5',
1908-
},
1909-
{
1910-
assetsInclude: /\.json5$/,
1911-
},
1912-
{
1913-
assetsInclude: [/\.json5$/, /\.pdf$/],
1914-
},
1915-
{
1916-
assetsInclude: (value: string) => value.endsWith('.json5'),
1917-
},
1918-
{
1919-
assetsInclude: {
1920-
not: /\.json5$/,
1921-
},
1922-
},
19231929
]
19241930

19251931
cases.forEach(source => {
@@ -2132,6 +2138,16 @@ describe('Config Validation', () => {
21322138
]
21332139
`)
21342140

2141+
expect(() => validate({ source: { preEntry: true } }))
2142+
.toThrowErrorMatchingInlineSnapshot(`
2143+
[Error: Invalid configuration.
2144+
2145+
Invalid config on \`$input.source.preEntry\`.
2146+
- Expect to be (Array<string> | string | undefined)
2147+
- Got: boolean
2148+
]
2149+
`)
2150+
21352151
expect(() => validate({ source: { transformImport: true } }))
21362152
.toThrowErrorMatchingInlineSnapshot(`
21372153
[Error: Invalid configuration.

0 commit comments

Comments
 (0)