Skip to content

Commit 7810778

Browse files
committed
init: migrate from rspack
0 parents  commit 7810778

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+8721
-0
lines changed

.github/renovate.json5

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3+
"extends": ["config:base", "schedule:monthly", "group:allNonMajor"],
4+
"rangeStrategy": "bump",
5+
"packageRules": [{ "depTypeList": ["peerDependencies"], "enabled": false }]
6+
}

.github/workflows/release.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
name: Release Full
3+
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
version:
8+
type: choice
9+
description: "Release Version Type"
10+
required: true
11+
default: "patch"
12+
options:
13+
- major
14+
- premajor
15+
- minor
16+
- preminor
17+
- patch
18+
- prepatch
19+
- prerelease
20+
21+
tag:
22+
type: choice
23+
description: "Release Npm Tag"
24+
required: true
25+
default: "latest"
26+
options:
27+
- canary
28+
- nightly
29+
- latest
30+
- beta
31+
- alpha
32+
33+
dry_run:
34+
type: boolean
35+
description: "DryRun release"
36+
required: true
37+
default: false
38+
39+
permissions:
40+
# To publish packages with provenance
41+
id-token: write
42+
43+
jobs:
44+
release:
45+
name: Release
46+
permissions:
47+
contents: write
48+
# To publish packages with provenance
49+
id-token: write
50+
runs-on: ubuntu-latest
51+
52+
steps:
53+
- name: Checkout
54+
uses: actions/checkout@v4
55+
56+
- name: Install Pnpm
57+
run: corepack enable
58+
59+
- name: Setup Node.js
60+
uses: actions/setup-node@v4
61+
with:
62+
node-version: 20
63+
cache: "pnpm"
64+
65+
- name: Install Dependencies
66+
run: pnpm install
67+
68+
- name: Run Test
69+
run: pnpm run test
70+
71+
- name: Try release to npm
72+
run: pnpm run release
73+
env:
74+
DRY_RUN: ${{ inputs.dry_run }}
75+
TAG: ${{ inputs.tag }}
76+
VERSION: ${{ inputs.version }}
77+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
78+
79+

.github/workflows/test.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test
2+
3+
# Controls when the action will run.
4+
on:
5+
# Triggers the workflow on pull request events but only for the main branch
6+
pull_request:
7+
branches: [main]
8+
push:
9+
branches: [main]
10+
# Allows you to run this workflow manually from the Actions tab
11+
workflow_dispatch:
12+
13+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
14+
jobs:
15+
test:
16+
runs-on: ubuntu-latest
17+
18+
# Steps represent a sequence of tasks that will be executed as part of the job
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Install Pnpm
24+
run: corepack enable
25+
26+
- name: Setup Node.js
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: 20
30+
cache: "pnpm"
31+
32+
- name: Install Dependencies
33+
run: pnpm install
34+
35+
- name: Run Test
36+
run: pnpm run test

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Local
2+
.DS_Store
3+
*.local
4+
*.log*
5+
6+
# Dist
7+
node_modules
8+
dist/
9+
test-results
10+
11+
# Test
12+
test/js/
13+
14+
# IDE
15+
.vscode/*
16+
!.vscode/settings.json
17+
!.vscode/extensions.json
18+
.idea
19+
*.tsbuildinfo
20+
21+

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @rspack/plugin-preact-refresh

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022-present Bytedance, Inc. and its affiliates.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<picture>
2+
<source media="(prefers-color-scheme: dark)" srcset="https://assets.rspack.dev/rspack/rspack-banner-plain-dark.png">
3+
<img alt="Rspack Banner" src="https://assets.rspack.dev/rspack/rspack-banner-plain-light.png">
4+
</picture>
5+
6+
# @rspack/plugin-react-refresh
7+
8+
React refresh plugin for [Rspack](https://github.com/web-infra-dev/rspack).
9+
10+
## Installation
11+
12+
First you need to install this plugin and its dependencies:
13+
14+
```bash
15+
npm add @rspack/plugin-react-refresh react-refresh -D
16+
# or
17+
yarn add @rspack/plugin-react-refresh react-refresh -D
18+
# or
19+
pnpm add @rspack/plugin-react-refresh react-refresh -D
20+
# or
21+
bun add @rspack/plugin-react-refresh react-refresh -D
22+
```
23+
24+
## Usage
25+
26+
Enabling [React Fast Refresh](https://reactnative.dev/docs/fast-refresh) functionality primarily involves two aspects: code injection and code transformation.
27+
28+
- Code injection will inject some code from the [react-refresh](https://www.npmjs.com/package/react-refresh) package, as well as some custom runtime code, all of which are integrated in this plugin and can be injected through.
29+
- Code transformation can be added through loaders, such as [jsc.transform.react.refresh](https://swc.rs/docs/configuration/compilation#jsctransformreactrefresh) for [swc-loader](https://swc.rs/docs/usage/swc-loader) or the [react-refresh/babel](https://github.com/facebook/react/tree/main/packages/react-refresh) for [babel-loader](https://github.com/babel/babel-loader).
30+
31+
```js
32+
const ReactRefreshPlugin = require('@rspack/plugin-react-refresh');
33+
const isDev = process.env.NODE_ENV === 'development';
34+
35+
module.exports = {
36+
experiments: {
37+
rspackFuture: {
38+
disableTransformByDefault: true,
39+
},
40+
},
41+
// ...
42+
mode: isDev ? 'development' : 'production',
43+
module: {
44+
rules: [
45+
{
46+
test: /\.jsx$/,
47+
use: {
48+
loader: 'builtin:swc-loader',
49+
options: {
50+
jsc: {
51+
parser: {
52+
syntax: 'ecmascript',
53+
jsx: true,
54+
},
55+
transform: {
56+
react: {
57+
development: isDev,
58+
refresh: isDev,
59+
},
60+
},
61+
},
62+
},
63+
},
64+
},
65+
],
66+
},
67+
plugins: [isDev && new ReactRefreshPlugin()].filter(Boolean),
68+
};
69+
```
70+
71+
Compared to the previous approach, this method decouples the React Fast Refresh code injection logic from the transformation logic. The code injection logic is handled uniformly by this plugin, while the code transformation is handled by loaders. This means that this plugin can be used in conjunction with `builtin:swc-loader`, `swc-loader`, or `babel-loader`.
72+
73+
## Example
74+
75+
- For usage with `builtin:swc-loader`, you can refer to the example at [examples/react-refresh](https://github.com/rspack-contrib/rspack-examples/tree/main/rspack/react-refresh/rspack.config.js), When using with `swc-loader`, simply replace `builtin:swc-loader` with `swc-loader`.
76+
- For usage with `babel-loader`, you can refer to the example at [examples/react-refresh-babel-loader](https://github.com/rspack-contrib/rspack-examples/tree/main/rspack/react-refresh-babel-loader/rspack.config.js)
77+
78+
## License
79+
80+
Rspack is [MIT licensed](https://github.com/web-infra-dev/rspack/blob/main/LICENSE).

biome.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
3+
"organizeImports": {
4+
"enabled": true
5+
},
6+
"vcs": {
7+
"enabled": true,
8+
"defaultBranch": "main",
9+
"clientKind": "git",
10+
"useIgnoreFile": true
11+
},
12+
"formatter": {
13+
"indentStyle": "space",
14+
"ignore": ["./test/**/*"]
15+
},
16+
"javascript": {
17+
"formatter": {
18+
"quoteStyle": "single"
19+
}
20+
},
21+
"css": {
22+
"formatter": {
23+
"enabled": true
24+
}
25+
},
26+
"linter": {
27+
"enabled": true,
28+
"rules": {
29+
"recommended": true
30+
},
31+
"ignore": ["./test/**/*", "./client/**/*", "./scripts/**/*"]
32+
}
33+
}

client/errorOverlayEntry.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* global __react_refresh_error_overlay__, __react_refresh_socket__, __resourceQuery */
2+
/**
3+
* The following code is modified based on
4+
* https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/f1c8b9a44198449093ca95f85af5df97925e1cfc/client/ErrorOverlayEntry.js
5+
*
6+
* MIT Licensed
7+
* Author Michael Mok
8+
* Copyright (c) 2019 Michael Mok
9+
* https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/0b960573797bf38926937994c481e4fec9ed8aa6/LICENSE
10+
*/
11+
const events = require('./utils/errorEventHandlers.js');
12+
const formatWebpackErrors = require('./utils/formatWebpackErrors.js');
13+
const runWithRetry = require('./utils/retry.js');
14+
15+
// Setup error states
16+
let isHotReload = false;
17+
let hasRuntimeErrors = false;
18+
19+
/**
20+
* Try dismissing the compile error overlay.
21+
* This will also reset runtime error records (if any),
22+
* because we have new source to evaluate.
23+
* @returns {void}
24+
*/
25+
function tryDismissErrorOverlay() {
26+
__react_refresh_error_overlay__.clearCompileError();
27+
__react_refresh_error_overlay__.clearRuntimeErrors(!hasRuntimeErrors);
28+
hasRuntimeErrors = false;
29+
}
30+
31+
/**
32+
* A function called after a compile success signal is received from Webpack.
33+
* @returns {void}
34+
*/
35+
function handleCompileSuccess() {
36+
isHotReload = true;
37+
38+
if (isHotReload) {
39+
tryDismissErrorOverlay();
40+
}
41+
}
42+
43+
/**
44+
* A function called after a compile errored signal is received from Webpack.
45+
* @param {string[]} errors
46+
* @returns {void}
47+
*/
48+
function handleCompileErrors(errors) {
49+
isHotReload = true;
50+
51+
const formattedErrors = formatWebpackErrors(errors);
52+
53+
// Only show the first error
54+
__react_refresh_error_overlay__.showCompileError(formattedErrors[0]);
55+
}
56+
57+
/**
58+
* Handles compilation messages from Webpack.
59+
* Integrates with a compile error overlay.
60+
* @param {*} message A Webpack HMR message sent via WebSockets.
61+
* @returns {void}
62+
*/
63+
function compileMessageHandler(message) {
64+
switch (message.type) {
65+
case 'ok':
66+
case 'still-ok':
67+
case 'warnings': {
68+
// TODO: Implement handling for warnings
69+
handleCompileSuccess();
70+
break;
71+
}
72+
case 'errors': {
73+
handleCompileErrors(message.data);
74+
break;
75+
}
76+
default: {
77+
// Do nothing.
78+
}
79+
}
80+
}
81+
82+
if (process.env.NODE_ENV !== 'production') {
83+
if (typeof window !== 'undefined') {
84+
function setupOverlay() {
85+
// Only register if no other overlay have been registered
86+
if (!window.__reactRefreshOverlayInjected && __react_refresh_socket__) {
87+
// Registers handlers for compile errors with retry -
88+
// This is to prevent mismatching injection order causing errors to be thrown
89+
runWithRetry(function initSocket() {
90+
__react_refresh_socket__.init(compileMessageHandler, __resourceQuery);
91+
}, 3);
92+
// Registers handlers for runtime errors
93+
events.handleError(function handleError(error) {
94+
hasRuntimeErrors = true;
95+
__react_refresh_error_overlay__.handleRuntimeError(error);
96+
});
97+
events.handleUnhandledRejection(
98+
function handleUnhandledPromiseRejection(error) {
99+
hasRuntimeErrors = true;
100+
__react_refresh_error_overlay__.handleRuntimeError(error);
101+
},
102+
);
103+
104+
// Mark overlay as injected to prevent double-injection
105+
window.__reactRefreshOverlayInjected = true;
106+
}
107+
}
108+
setupOverlay();
109+
}
110+
}

0 commit comments

Comments
 (0)