@lark-apaas/fullstack-rspack-preset 0.1.0 → 0.1.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/preset.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type Configuration } from '@rspack/core';
|
|
2
2
|
export interface CreateRecommendRspackConfigOptions {
|
|
3
3
|
isDev?: boolean;
|
|
4
|
+
/** 是否启用 React Refresh (需要配合 @rspack/plugin-react-refresh 使用) */
|
|
5
|
+
enableReactRefresh?: boolean;
|
|
4
6
|
/** 是否需要插件解析路由 */
|
|
5
7
|
needRoutes?: boolean;
|
|
6
8
|
/** 客户端基础路径 */
|
package/lib/preset.js
CHANGED
|
@@ -11,11 +11,12 @@ const dev_server_listener_1 = require("./utils/dev-server-listener");
|
|
|
11
11
|
const route_parser_plugin_1 = __importDefault(require("./rspack-plugins/route-parser-plugin"));
|
|
12
12
|
const slardar_performance_monitor_plugin_1 = __importDefault(require("./rspack-plugins/slardar-performance-monitor-plugin"));
|
|
13
13
|
function createRecommendRspackConfig(options) {
|
|
14
|
-
const { isDev = true, needRoutes = true, clientBasePath = '', publicPath = '', // 静态资源路径
|
|
14
|
+
const { isDev = true, enableReactRefresh = false, needRoutes = true, clientBasePath = '', publicPath = '', // 静态资源路径
|
|
15
15
|
} = options;
|
|
16
16
|
const rootDir = process.cwd();
|
|
17
17
|
const serverPort = process.env.SERVER_PORT || '3000';
|
|
18
18
|
return {
|
|
19
|
+
mode: isDev ? 'development' : 'production',
|
|
19
20
|
cache: false,
|
|
20
21
|
experiments: {
|
|
21
22
|
css: true,
|
|
@@ -96,7 +97,7 @@ function createRecommendRspackConfig(options) {
|
|
|
96
97
|
}
|
|
97
98
|
: {}),
|
|
98
99
|
development: isDev,
|
|
99
|
-
refresh:
|
|
100
|
+
refresh: enableReactRefresh,
|
|
100
101
|
},
|
|
101
102
|
},
|
|
102
103
|
},
|
|
@@ -123,7 +124,7 @@ function createRecommendRspackConfig(options) {
|
|
|
123
124
|
}),
|
|
124
125
|
new core_1.default.DefinePlugin({
|
|
125
126
|
'process.env.NODE_ENV': JSON.stringify(isDev ? 'development' : 'production'),
|
|
126
|
-
'process.env.runtimeMode': 'fullstack',
|
|
127
|
+
'process.env.runtimeMode': JSON.stringify('fullstack'),
|
|
127
128
|
'process.env.CLIENT_BASE_PATH': JSON.stringify(clientBasePath),
|
|
128
129
|
// FIXME:安全漏洞,会读取到服务路径,不应该在客户端代码中使用
|
|
129
130
|
'process.env.CWD': JSON.stringify(process.cwd()),
|
|
@@ -141,6 +142,13 @@ function createRecommendRspackConfig(options) {
|
|
|
141
142
|
},
|
|
142
143
|
],
|
|
143
144
|
}),
|
|
145
|
+
new core_1.default.HtmlRspackPlugin({
|
|
146
|
+
template: path_1.default.resolve(rootDir, './client/index.html'),
|
|
147
|
+
filename: 'index.html',
|
|
148
|
+
templateParameters: {
|
|
149
|
+
publicPath,
|
|
150
|
+
}
|
|
151
|
+
}),
|
|
144
152
|
// 性能监控插件
|
|
145
153
|
new slardar_performance_monitor_plugin_1.default(),
|
|
146
154
|
// 开发环境下,解析路由
|
|
@@ -7,19 +7,30 @@ class SlardarPerformanceMonitorPlugin {
|
|
|
7
7
|
apply(compiler) {
|
|
8
8
|
compiler.hooks.compilation.tap('SlardarPerformanceMonitorPlugin', (compilation) => {
|
|
9
9
|
try {
|
|
10
|
+
// 从 compiler.webpack 或 compiler.rspack 获取 HtmlRspackPlugin
|
|
11
|
+
// 这样可以避免在 npm link 环境下的模块解析问题
|
|
10
12
|
let HtmlPlugin;
|
|
11
13
|
try {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
HtmlPlugin = require('html-webpack-plugin');
|
|
14
|
+
// 尝试从 compiler 中获取 rspack
|
|
15
|
+
const rspack = compiler.webpack || compiler.rspack || compiler.constructor.webpack;
|
|
16
|
+
if (rspack && rspack.HtmlRspackPlugin) {
|
|
17
|
+
HtmlPlugin = rspack.HtmlRspackPlugin;
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
else {
|
|
20
|
+
// 降级到 require,但这在 npm link 下可能失败
|
|
21
|
+
try {
|
|
22
|
+
HtmlPlugin = require('html-webpack-plugin');
|
|
23
|
+
}
|
|
24
|
+
catch (e2) {
|
|
25
|
+
console.warn('SlardarPerformanceMonitorPlugin: HtmlRspackPlugin not found');
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
21
28
|
}
|
|
22
29
|
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
console.warn('SlardarPerformanceMonitorPlugin: Failed to get HtmlRspackPlugin');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
23
34
|
const hooks = HtmlPlugin.getHooks(compilation);
|
|
24
35
|
hooks.beforeEmit.tapAsync('SlardarPerformanceMonitorPlugin', (data, cb) => {
|
|
25
36
|
const position = this.options.position ?? 'head-end';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/fullstack-rspack-preset",
|
|
3
|
-
"version": "0.1.0",
|
|
3
|
+
"version": "0.1.2-alpha.0",
|
|
4
4
|
"files": [
|
|
5
5
|
"lib"
|
|
6
6
|
],
|
|
@@ -32,13 +32,15 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@lark-apaas/devtool-kits": "^1.0.0",
|
|
35
|
-
"@rspack/core": "^1.
|
|
35
|
+
"@rspack/core": "^1.5.5",
|
|
36
36
|
"@types/node": "^22.15.30",
|
|
37
37
|
"typescript": "^5.9.2",
|
|
38
38
|
"vitest": "^2.1.8"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@lark-apaas/devtool-kits": "^1.0.0",
|
|
42
|
-
"@rspack/core": "
|
|
42
|
+
"@rspack/core": "^1.5.5",
|
|
43
|
+
"react": ">=16.14.0",
|
|
44
|
+
"react-dom": ">=16.14.0"
|
|
43
45
|
}
|
|
44
46
|
}
|