Skip to content

Commit 57fe0e3

Browse files
authored
feat: add ESM application example (#324)
1 parent e3abfc2 commit 57fe0e3

File tree

13 files changed

+188
-0
lines changed

13 files changed

+188
-0
lines changed

pnpm-lock.yaml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rspack/library-esm/rspack.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default defineConfig({
1212
library: {
1313
type: "modern-module",
1414
},
15+
chunkLoading: 'import',
16+
workerChunkLoading: 'import',
1517
},
1618
module: {
1719
parser: {
@@ -21,6 +23,8 @@ export default defineConfig({
2123
},
2224
},
2325
optimization: {
26+
concatenateModules: true,
27+
avoidEntryIife: true,
2428
minimize: false, // no need to minify for library
2529
},
2630
experiments: {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Document</title>
9+
</head>
10+
11+
<body>
12+
<div id="root"></div>
13+
</body>
14+
15+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "example-rspack-react-refresh-esm",
3+
"version": "1.0.0",
4+
"private": true,
5+
"license": "MIT",
6+
"main": "index.js",
7+
"scripts": {
8+
"build": "cross-env NODE_ENV=production rspack build",
9+
"dev": "rspack serve "
10+
},
11+
"dependencies": {
12+
"react": "^19.1.0",
13+
"react-dom": "^19.1.0"
14+
},
15+
"devDependencies": {
16+
"@rspack/cli": "1.4.10",
17+
"@rspack/core": "1.4.10",
18+
"@rspack/plugin-react-refresh": "^1.4.3",
19+
"react-refresh": "0.17.0"
20+
}
21+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { rspack } = require('@rspack/core');
2+
const ReactRefreshPlugin = require('@rspack/plugin-react-refresh');
3+
4+
const isProduction = process.env.NODE_ENV === 'production';
5+
6+
/** @type {import('@rspack/cli').Configuration} */
7+
const config = {
8+
entry: { main: './src/index.tsx' },
9+
devtool: 'source-map',
10+
output: {
11+
module: true,
12+
chunkFormat: 'array-push',
13+
chunkLoading: 'jsonp',
14+
workerChunkLoading: 'import',
15+
},
16+
resolve: {
17+
extensions: ['...', '.ts', '.tsx', '.jsx'],
18+
},
19+
experiments: {
20+
css: true,
21+
outputModule: true,
22+
},
23+
module: {
24+
rules: [
25+
{
26+
test: /\.tsx$/,
27+
use: {
28+
loader: 'builtin:swc-loader',
29+
options: {
30+
sourceMap: true,
31+
jsc: {
32+
parser: {
33+
syntax: 'typescript',
34+
tsx: true,
35+
},
36+
transform: {
37+
react: {
38+
runtime: 'automatic',
39+
development: !isProduction,
40+
refresh: !isProduction,
41+
},
42+
},
43+
},
44+
},
45+
},
46+
},
47+
],
48+
},
49+
plugins: [
50+
new rspack.HtmlRspackPlugin({ template: './index.html', scriptLoading: "module" }),
51+
!isProduction && new ReactRefreshPlugin(),
52+
].filter(Boolean),
53+
};
54+
55+
module.exports = config;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { lazy, Suspense } from 'react';
2+
import { ArrowFunction } from './ArrowFunction';
3+
import ClassDefault from './ClassDefault';
4+
import { ClassNamed } from './ClassNamed';
5+
import FunctionDefault from './FunctionDefault';
6+
import { FunctionNamed } from './FunctionNamed';
7+
8+
const LazyComponent = lazy(() => import('./LazyComponent'));
9+
10+
function App() {
11+
return (
12+
<div>
13+
<ClassDefault />
14+
<ClassNamed />
15+
<FunctionDefault />
16+
<FunctionNamed />
17+
<ArrowFunction />
18+
<Suspense fallback={<h1>Loading</h1>}>
19+
<LazyComponent />
20+
</Suspense>
21+
</div>
22+
);
23+
}
24+
25+
export default App;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ArrowFunction = () => <h1>Arrow Function</h1>;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Component } from 'react';
2+
3+
class ClassDefault extends Component {
4+
render() {
5+
return <h1>Default Export Class</h1>;
6+
}
7+
}
8+
9+
export default ClassDefault;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from 'react';
2+
3+
export class ClassNamed extends Component {
4+
render() {
5+
return <h1>Named Export Class</h1>;
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function FunctionDefault() {
2+
return <h1>Default Export Function</h1>;
3+
}
4+
5+
export default FunctionDefault;

0 commit comments

Comments
 (0)