@pixui-dev/pxw 0.1.35 → 0.1.36

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.
Files changed (97) hide show
  1. package/bin/pxw.js +56 -56
  2. package/bin/wpbuild.js +10 -10
  3. package/config/daemon.js +157 -157
  4. package/config/default.conf +20 -20
  5. package/config/devops.js +550 -550
  6. package/config/h5es.js +14 -14
  7. package/config/index.html +183 -183
  8. package/config/pfbs.js +259 -259
  9. package/config/util.js +143 -143
  10. package/config/webpack.js +325 -325
  11. package/lib/assets/check.html +62 -62
  12. package/lib/assets/preact.js +4354 -4354
  13. package/lib/check/main.less +62 -62
  14. package/lib/check/main.tsx +41 -41
  15. package/lib/check/tool.js +3 -3
  16. package/lib/check/util.tsx +110 -110
  17. package/lib/grpc-web/dist/ChunkParser.js +117 -117
  18. package/lib/grpc-web/dist/Code.js +58 -58
  19. package/lib/grpc-web/dist/client.js +299 -299
  20. package/lib/grpc-web/dist/debug.js +16 -16
  21. package/lib/grpc-web/dist/detach.js +7 -7
  22. package/lib/grpc-web/dist/index.js +29 -29
  23. package/lib/grpc-web/dist/invoke.js +32 -32
  24. package/lib/grpc-web/dist/message.js +3 -3
  25. package/lib/grpc-web/dist/metadata.js +5 -5
  26. package/lib/grpc-web/dist/service.js +3 -3
  27. package/lib/grpc-web/dist/transports/Transport.js +15 -15
  28. package/lib/grpc-web/dist/transports/http/fetch.js +117 -117
  29. package/lib/grpc-web/dist/transports/http/http.js +15 -15
  30. package/lib/grpc-web/dist/transports/http/xhr.js +136 -136
  31. package/lib/grpc-web/dist/transports/http/xhrUtil.js +36 -36
  32. package/lib/grpc-web/dist/transports/websocket/websocket.js +95 -95
  33. package/lib/grpc-web/dist/typings/ChunkParser.d.ts +17 -17
  34. package/lib/grpc-web/dist/typings/Code.d.ts +20 -20
  35. package/lib/grpc-web/dist/typings/client.d.ts +25 -25
  36. package/lib/grpc-web/dist/typings/debug.d.ts +1 -1
  37. package/lib/grpc-web/dist/typings/detach.d.ts +1 -1
  38. package/lib/grpc-web/dist/typings/index.d.ts +45 -45
  39. package/lib/grpc-web/dist/typings/invoke.d.ts +20 -20
  40. package/lib/grpc-web/dist/typings/message.d.ts +8 -8
  41. package/lib/grpc-web/dist/typings/metadata.d.ts +2 -2
  42. package/lib/grpc-web/dist/typings/service.d.ts +16 -16
  43. package/lib/grpc-web/dist/typings/transports/Transport.d.ts +22 -22
  44. package/lib/grpc-web/dist/typings/transports/http/fetch.d.ts +6 -6
  45. package/lib/grpc-web/dist/typings/transports/http/http.d.ts +5 -5
  46. package/lib/grpc-web/dist/typings/transports/http/xhr.d.ts +27 -27
  47. package/lib/grpc-web/dist/typings/transports/http/xhrUtil.d.ts +3 -3
  48. package/lib/grpc-web/dist/typings/transports/websocket/websocket.d.ts +2 -2
  49. package/lib/grpc-web/dist/typings/unary.d.ts +23 -23
  50. package/lib/grpc-web/dist/typings/util.d.ts +2 -2
  51. package/lib/grpc-web/dist/unary.js +44 -44
  52. package/lib/grpc-web/dist/util.js +11 -11
  53. package/lib/grpcTransport/PixHttp2Transport.ts +107 -107
  54. package/lib/grpcTransport/PixLuaTransport.ts +82 -82
  55. package/lib/h5es-types/v1.9.2/h5es.d.ts +1756 -1756
  56. package/lib/h5es-types/v3.5.0/h5es.d.ts +1851 -1851
  57. package/lib/h5es-types/v3.5.6/h5es.d.ts +1911 -1911
  58. package/lib/pi_component/tinyList/tinyList.js +483 -483
  59. package/lib/pi_component/tinyList/tinyList.tsx +517 -517
  60. package/lib/preact-router.js +395 -395
  61. package/lib/preact.js +4355 -4355
  62. package/lib/preact.tq.js +4385 -4385
  63. package/lib/react-window/src/FixedSizeGrid.js +172 -172
  64. package/lib/react-window/src/FixedSizeList.js +91 -91
  65. package/lib/react-window/src/VariableSizeGrid.js +329 -329
  66. package/lib/react-window/src/VariableSizeList.js +231 -231
  67. package/lib/react-window/src/__tests__/FixedSizeGrid.js +942 -942
  68. package/lib/react-window/src/__tests__/FixedSizeList.js +749 -749
  69. package/lib/react-window/src/__tests__/VariableSizeGrid.js +598 -598
  70. package/lib/react-window/src/__tests__/VariableSizeList.js +345 -345
  71. package/lib/react-window/src/__tests__/__snapshots__/FixedSizeGrid.js.snap +912 -912
  72. package/lib/react-window/src/__tests__/__snapshots__/FixedSizeList.js.snap +568 -568
  73. package/lib/react-window/src/__tests__/__snapshots__/VariableSizeGrid.js.snap +542 -542
  74. package/lib/react-window/src/__tests__/__snapshots__/VariableSizeList.js.snap +331 -331
  75. package/lib/react-window/src/__tests__/areEqual.js +28 -28
  76. package/lib/react-window/src/__tests__/shouldComponentUpdate.js +32 -32
  77. package/lib/react-window/src/areEqual.js +13 -13
  78. package/lib/react-window/src/createGridComponent.js +657 -657
  79. package/lib/react-window/src/createListComponent.js +574 -574
  80. package/lib/react-window/src/domHelpers.js +69 -69
  81. package/lib/react-window/src/index.js +9 -9
  82. package/lib/react-window/src/shallowDiffers.js +17 -17
  83. package/lib/react-window/src/shouldComponentUpdate.js +11 -11
  84. package/lib/react-window/src/test.js.flow +382 -382
  85. package/lib/react-window/src/timer.js +36 -36
  86. package/lib/types/dom.ts +17 -17
  87. package/lib/types/ext.d.ts +75 -75
  88. package/lib/types/preact/css.d.ts +7478 -7476
  89. package/lib/types/preact/index.d.ts +340 -340
  90. package/lib/types/preact/internal.d.ts +94 -94
  91. package/lib/types/preact/jsx.d.ts +368 -368
  92. package/lib/types/preact-router/index.d.ts +84 -84
  93. package/package.json +127 -127
  94. package/scripts/pack.js +40 -40
  95. package/scripts/postinstall.js +12 -12
  96. package/scripts/run-pxw.js +19 -19
  97. package/tsconfig.json +30 -30
package/config/webpack.js CHANGED
@@ -1,325 +1,325 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const webpack = require('webpack');
4
- const argv = require('yargs').argv;
5
-
6
- const MiniCssExtractPlugin = require('mini-css-extract-plugin');
7
-
8
- const { CleanWebpackPlugin } = require('clean-webpack-plugin');
9
- const HtmlWebpackPlugin = require('html-webpack-plugin');
10
- const InlineChunkHtmlPlugin = require('inline-chunk-html-plugin');
11
-
12
- const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
13
- const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
14
-
15
- const sveltePreprocess = require('svelte-preprocess');
16
-
17
- const { getLocalIP, loadProjectConfig, getH5esType, setH5esType } = require('./util');
18
-
19
- const pr = path.resolve;
20
- let cwd = process.cwd();
21
- let isPxwSelf = cwd == pr(__dirname + '/..');
22
- let isShipping = argv.mode == 'production';
23
- // console.log('isPxwSelf:', isPxwSelf);
24
- // console.log('isShipping:', isShipping);
25
- let rootDir = pr(__dirname + '/../');
26
- let buildOutputPath = pr(cwd, 'dist', 'html' + (isShipping ? '-pro' : '-dev'));
27
-
28
- function getHtmlName(v) {
29
- switch (v.name) {
30
- case 'app': {
31
- return 'index.html'; // app不允许自定义
32
- }
33
- case 'preprocess': {
34
- return 'preprocess.html'; // preprocess不允许自定义
35
- }
36
- default: {
37
- return v.output ? `${v.output}.html` : `${v.name}.html`;
38
- }
39
- }
40
- }
41
-
42
- let projectConfig = loadProjectConfig();
43
- ///////////////////////////////////
44
- //所有app定义在这里
45
- let appDef = [];
46
- let pienv = {};
47
- //从链接的src里读取配置
48
- projectConfig.pages.forEach((p) => {
49
- p.entry = `${cwd}/${p.entry}`;
50
- p.template = `${cwd}/${p.template}`;
51
- appDef.push(p);
52
- });
53
-
54
- let env = projectConfig.env;
55
- Object.entries(env || {}).forEach(([k, v]) => {
56
- pienv['process.env.' + k] = JSON.stringify(v);
57
- });
58
-
59
- if (!projectConfig.noSelfCheck && (!isShipping || process.env.withSelfcheck === 'true' || isPxwSelf)) {
60
- appDef.push({
61
- name: 'selfcheck',
62
- template: __dirname + '/../lib/assets/check.html',
63
- entry: __dirname + '/../lib/check/main.tsx',
64
- output: 'index',
65
- });
66
- projectConfig.pages.push({
67
- name: 'selfcheck',
68
- });
69
- }
70
- ///////////////////////////////////
71
-
72
- let plugins = [
73
- new CleanWebpackPlugin({
74
- cleanOnceBeforeBuildPatterns: ['**/*', path.join(cwd, 'dist/.build')],
75
- }),
76
- new ForkTsCheckerWebpackPlugin({ async: !isShipping }), //开发时异步检查,打包时同步检查以阻止错误代码
77
- new MiniCssExtractPlugin({
78
- filename: '[name]/[name].css',
79
- }),
80
- new webpack.DefinePlugin(pienv),
81
- ...(projectConfig.onWebpackConfig || []),
82
- ...appDef.map((v) => {
83
- return new HtmlWebpackPlugin({
84
- minify: isShipping,
85
- chunks: [v.name],
86
- template: v.template,
87
- filename: v.name + '/' + getHtmlName(v),
88
- });
89
- }),
90
- ].filter((v) => !!v);
91
-
92
- let alias = {};
93
-
94
- //调整svelte-loader里用到的compiler,也要改用定制版本
95
- let usePikaSvelte = projectConfig.usePikaSvelte;
96
- if (usePikaSvelte) {
97
- let sa = {
98
- ['svelte']: path.resolve('./lib/pika-svelte'),
99
- ['svelte/internal']: path.resolve('./lib/pika-svelte/internal'),
100
- ['svelte/compiler']: path.resolve('./lib/pika-svelte/compiler'),
101
- };
102
- require('module-alias/').addAliases(sa);
103
- alias = Object.assign(alias, sa);
104
- }
105
-
106
- let loadBabelPlugin = function (name) {
107
- return require(name);
108
- };
109
- // console.debug('appDef', appDef);
110
-
111
- module.exports = {
112
- entry: Object.fromEntries(appDef.map((v) => [v.name, v.entry])),
113
- output: {
114
- path: buildOutputPath,
115
- filename: '[name]/[name].js',
116
- publicPath: isShipping
117
- ? '../' //发布后都从文件系统加载,使用相对路径引用素材
118
- : `http://${getLocalIP()[0]}/`, //开发时绑定到外部ip,方便手机访问
119
- },
120
-
121
- externals: {
122
- // preact: 'window.cpreact',
123
- // react: 'window.cpreact',
124
- // 'preact/hooks': 'window.cpreact',
125
- // 'preact/compat': 'window.cpreact',
126
- // 'pxwdom': 'window.pxwdom'
127
- csharp: 'esm globalThis.CSharp',
128
- },
129
-
130
- resolve: {
131
- modules: [pr(__dirname, '../node_modules'), pr(cwd, 'node_modules')], // 搜索当前子包下的 node_modules,主工程是业务包默认不会带这些工具库
132
- extensions: ['.js', '.jsx', '.ts', '.tsx', '.svelte'],
133
-
134
- alias: {
135
- src: cwd + '/src',
136
- lib: cwd + '/lib',
137
- ['faas-lib']: cwd + '/faas-lib',
138
- ['preact$']: __dirname + '/../lib/preact.js',
139
- ['preact/hooks$']: __dirname + '/../lib/preact.js',
140
- ['preact-router$']: __dirname + '/../lib/preact-router.js',
141
- react: __dirname + '/../lib/preact.js',
142
- // ['react-window']: path.resolve('./lib/react-window/src'),
143
- // ['@improbable-eng/grpc-web']: path.resolve('./lib/grpc-web/dist'),
144
- ...alias,
145
- },
146
- },
147
-
148
- module: {
149
- rules: [
150
- {
151
- test: /.(js|ts)x?$/,
152
- use: {
153
- loader: require.resolve('babel-loader'),
154
- options: {
155
- presets: [[loadBabelPlugin('@babel/preset-typescript'), { allowNamespaces: true }]],
156
- plugins: [
157
- loadBabelPlugin('@babel/plugin-transform-flow-strip-types'),
158
- loadBabelPlugin('@babel/plugin-syntax-jsx'),
159
- [loadBabelPlugin('@babel/plugin-proposal-decorators'), { legacy: true }],
160
- [loadBabelPlugin('@babel/plugin-transform-class-properties')],
161
- [loadBabelPlugin('@babel/plugin-transform-optional-chaining')],
162
-
163
- //这个插件将 jsx 标记转为 h 函数调用,但是在webpack打包后的代码里,import preact, { h } from 'preact' 里的 h 没了
164
- //单个 h 成为无法识别的变量, 即使把 pragma 写成 preact.createElement 也没用,因为 preact 也被改名了
165
- [loadBabelPlugin('@babel/plugin-transform-react-jsx'), { pragma: 'h' }],
166
- //这个插件将 h 换成从 preact 中引入的 createElement,它知道 preact 被改名后的值
167
- [
168
- loadBabelPlugin('babel-plugin-jsx-pragmatic'),
169
- {
170
- module: 'preact',
171
- import: 'h',
172
- export: 'createElement',
173
- },
174
- ],
175
- ],
176
- },
177
- },
178
- },
179
- {
180
- test: /\.(png|jpg|gif|mp4|lottie)$/,
181
- loader: require.resolve('url-loader'),
182
- options: {
183
- name: 'static/media/[name].[hash:8].[ext]',
184
- limit: 1,
185
- },
186
- },
187
- {
188
- test: /\.svelte$/,
189
- use: [
190
- {
191
- loader: 'svelte-loader',
192
- options: {
193
- compilerOptions: {
194
- dev: !isShipping,
195
- },
196
- emitCss: true,
197
- preprocess: sveltePreprocess({}),
198
- hotReload: false,
199
- },
200
- },
201
- ],
202
- },
203
- {
204
- test: /\.css$/,
205
- use: [
206
- MiniCssExtractPlugin.loader,
207
- {
208
- loader: require.resolve('css-loader'),
209
- options: {
210
- modules: false,
211
- },
212
- },
213
- ],
214
- },
215
- {
216
- test: /\.less$/,
217
- use: [
218
- {
219
- loader: MiniCssExtractPlugin.loader,
220
- options: {
221
- publicPath: '../',
222
- },
223
- },
224
- {
225
- loader: require.resolve('css-loader'),
226
- options: {
227
- modules: true,
228
- esModule: false,
229
- },
230
- },
231
- {
232
- loader: require.resolve('less-loader'),
233
- options: { javascriptEnabled: true },
234
- },
235
- ],
236
- },
237
- ],
238
- },
239
-
240
- optimization: {
241
- minimize: false, // 最终发布会用pfbs转成二进制,所以源码就不用压缩了,方便阅读调试
242
- },
243
-
244
- devtool: 'source-map',
245
-
246
- plugins,
247
-
248
- //这个属性webpack打包时是不用的,是给pxw里的express做扩展用的,之所以要包在devServer这一层key里是因为webpack5会强制校验配置文件中不包含未知的key,只有这个key作为曾经的配置项被保留下来,刚好用来传递变量
249
- devServer: {
250
- getMiddleware: function () {
251
- return getMiddleware;
252
- },
253
- },
254
- };
255
-
256
- var getMiddleware = function (appPort, notifyAppReload) {
257
- // console.log('webpack getMiddleware', appPort, notifyAppReload);
258
- const middleware = require('webpack-dev-middleware');
259
- let h5es = require('./h5es');
260
-
261
- process.env.mode = 'development';
262
- let config = module.exports;
263
-
264
- config.mode = 'development';
265
- config.output.publicPath = `http://${getLocalIP()[0]}:${appPort}/`;
266
- config.plugins.push(
267
- //有html文件重新生成时,通知已经打开的页面reload
268
- new (class PxAutobackupPlugin {
269
- apply(compiler) {
270
- compiler.hooks.afterEmit.tap('PxAutoReloadPlugin', (info) => {
271
- let assets = Object.entries(info.assets).map(([name, asset]) => {
272
- if (name.endsWith('.html')) {
273
- console.log('webpack afterEmit', name);
274
- notifyAppReload(name.split('/')[0]);
275
- }
276
- });
277
- });
278
- }
279
- })(),
280
- );
281
- const compiler = webpack(config);
282
- return [
283
- (req, res, next) => {
284
- if (req.url == '/') return res.sendFile(pr(rootDir, 'config/index.html'));
285
- if (req.url == '/api/get-project-config') return res.json({ ...projectConfig, h5esVersions: h5es.h5esVersions, defaultH5esType: getH5esType() });
286
- if (req.url == '/api/update-tsconfig') {
287
- try {
288
- console.log(req.body);
289
- let h5esType = setH5esType(req.body.typeRoots);
290
- res.json({
291
- success: true,
292
- restartRecommended: true,
293
- newConfig: h5esType,
294
- });
295
- }
296
- catch (error) {
297
- console.log(error);
298
- res.status(500).json({
299
- error: `更新配置失败: ${error.message}`,
300
- detail: error.stack,
301
- });
302
- }
303
- return;
304
- }
305
-
306
- //rewrites page's subpath /name/a/b/c to /name
307
- for (let i = 0; i < projectConfig.pages.length; i++) {
308
- let p = projectConfig.pages[i];
309
- let prefix = `/${p.name}/`;
310
- console.log('check rewrite', req.url, prefix);
311
- if (req.url.startsWith(prefix) && req.url.indexOf('.') < 0) {
312
- req.url = prefix;
313
- return next();
314
- }
315
- }
316
- return next();
317
- },
318
- middleware(compiler, {
319
- writeToDisk: true,
320
- mimeTypes: {
321
- js: 'text/javascript',
322
- },
323
- }),
324
- ];
325
- };
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const webpack = require('webpack');
4
+ const argv = require('yargs').argv;
5
+
6
+ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
7
+
8
+ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
9
+ const HtmlWebpackPlugin = require('html-webpack-plugin');
10
+ const InlineChunkHtmlPlugin = require('inline-chunk-html-plugin');
11
+
12
+ const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
13
+ const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
14
+
15
+ const sveltePreprocess = require('svelte-preprocess');
16
+
17
+ const { getLocalIP, loadProjectConfig, getH5esType, setH5esType } = require('./util');
18
+
19
+ const pr = path.resolve;
20
+ let cwd = process.cwd();
21
+ let isPxwSelf = cwd == pr(__dirname + '/..');
22
+ let isShipping = argv.mode == 'production';
23
+ // console.log('isPxwSelf:', isPxwSelf);
24
+ // console.log('isShipping:', isShipping);
25
+ let rootDir = pr(__dirname + '/../');
26
+ let buildOutputPath = pr(cwd, 'dist', 'html' + (isShipping ? '-pro' : '-dev'));
27
+
28
+ function getHtmlName(v) {
29
+ switch (v.name) {
30
+ case 'app': {
31
+ return 'index.html'; // app不允许自定义
32
+ }
33
+ case 'preprocess': {
34
+ return 'preprocess.html'; // preprocess不允许自定义
35
+ }
36
+ default: {
37
+ return v.output ? `${v.output}.html` : `${v.name}.html`;
38
+ }
39
+ }
40
+ }
41
+
42
+ let projectConfig = loadProjectConfig();
43
+ ///////////////////////////////////
44
+ //所有app定义在这里
45
+ let appDef = [];
46
+ let pienv = {};
47
+ //从链接的src里读取配置
48
+ projectConfig.pages.forEach((p) => {
49
+ p.entry = `${cwd}/${p.entry}`;
50
+ p.template = `${cwd}/${p.template}`;
51
+ appDef.push(p);
52
+ });
53
+
54
+ let env = projectConfig.env;
55
+ Object.entries(env || {}).forEach(([k, v]) => {
56
+ pienv['process.env.' + k] = JSON.stringify(v);
57
+ });
58
+
59
+ if (!projectConfig.noSelfCheck && (!isShipping || process.env.withSelfcheck === 'true' || isPxwSelf)) {
60
+ appDef.push({
61
+ name: 'selfcheck',
62
+ template: __dirname + '/../lib/assets/check.html',
63
+ entry: __dirname + '/../lib/check/main.tsx',
64
+ output: 'index',
65
+ });
66
+ projectConfig.pages.push({
67
+ name: 'selfcheck',
68
+ });
69
+ }
70
+ ///////////////////////////////////
71
+
72
+ let plugins = [
73
+ new CleanWebpackPlugin({
74
+ cleanOnceBeforeBuildPatterns: ['**/*', path.join(cwd, 'dist/.build')],
75
+ }),
76
+ new ForkTsCheckerWebpackPlugin({ async: !isShipping }), //开发时异步检查,打包时同步检查以阻止错误代码
77
+ new MiniCssExtractPlugin({
78
+ filename: '[name]/[name].css',
79
+ }),
80
+ new webpack.DefinePlugin(pienv),
81
+ ...(projectConfig.onWebpackConfig || []),
82
+ ...appDef.map((v) => {
83
+ return new HtmlWebpackPlugin({
84
+ minify: isShipping,
85
+ chunks: [v.name],
86
+ template: v.template,
87
+ filename: v.name + '/' + getHtmlName(v),
88
+ });
89
+ }),
90
+ ].filter((v) => !!v);
91
+
92
+ let alias = {};
93
+
94
+ //调整svelte-loader里用到的compiler,也要改用定制版本
95
+ let usePikaSvelte = projectConfig.usePikaSvelte;
96
+ if (usePikaSvelte) {
97
+ let sa = {
98
+ ['svelte']: path.resolve('./lib/pika-svelte'),
99
+ ['svelte/internal']: path.resolve('./lib/pika-svelte/internal'),
100
+ ['svelte/compiler']: path.resolve('./lib/pika-svelte/compiler'),
101
+ };
102
+ require('module-alias/').addAliases(sa);
103
+ alias = Object.assign(alias, sa);
104
+ }
105
+
106
+ let loadBabelPlugin = function (name) {
107
+ return require(name);
108
+ };
109
+ // console.debug('appDef', appDef);
110
+
111
+ module.exports = {
112
+ entry: Object.fromEntries(appDef.map((v) => [v.name, v.entry])),
113
+ output: {
114
+ path: buildOutputPath,
115
+ filename: '[name]/[name].js',
116
+ publicPath: isShipping
117
+ ? '../' //发布后都从文件系统加载,使用相对路径引用素材
118
+ : `http://${getLocalIP()[0]}/`, //开发时绑定到外部ip,方便手机访问
119
+ },
120
+
121
+ externals: {
122
+ // preact: 'window.cpreact',
123
+ // react: 'window.cpreact',
124
+ // 'preact/hooks': 'window.cpreact',
125
+ // 'preact/compat': 'window.cpreact',
126
+ // 'pxwdom': 'window.pxwdom'
127
+ csharp: 'esm globalThis.CSharp',
128
+ },
129
+
130
+ resolve: {
131
+ modules: [pr(__dirname, '../node_modules'), pr(cwd, 'node_modules')], // 搜索当前子包下的 node_modules,主工程是业务包默认不会带这些工具库
132
+ extensions: ['.js', '.jsx', '.ts', '.tsx', '.svelte'],
133
+
134
+ alias: {
135
+ src: cwd + '/src',
136
+ lib: cwd + '/lib',
137
+ ['faas-lib']: cwd + '/faas-lib',
138
+ ['preact$']: __dirname + '/../lib/preact.js',
139
+ ['preact/hooks$']: __dirname + '/../lib/preact.js',
140
+ ['preact-router$']: __dirname + '/../lib/preact-router.js',
141
+ react: __dirname + '/../lib/preact.js',
142
+ // ['react-window']: path.resolve('./lib/react-window/src'),
143
+ // ['@improbable-eng/grpc-web']: path.resolve('./lib/grpc-web/dist'),
144
+ ...alias,
145
+ },
146
+ },
147
+
148
+ module: {
149
+ rules: [
150
+ {
151
+ test: /.(js|ts)x?$/,
152
+ use: {
153
+ loader: require.resolve('babel-loader'),
154
+ options: {
155
+ presets: [[loadBabelPlugin('@babel/preset-typescript'), { allowNamespaces: true }]],
156
+ plugins: [
157
+ loadBabelPlugin('@babel/plugin-transform-flow-strip-types'),
158
+ loadBabelPlugin('@babel/plugin-syntax-jsx'),
159
+ [loadBabelPlugin('@babel/plugin-proposal-decorators'), { legacy: true }],
160
+ [loadBabelPlugin('@babel/plugin-transform-class-properties')],
161
+ [loadBabelPlugin('@babel/plugin-transform-optional-chaining')],
162
+
163
+ //这个插件将 jsx 标记转为 h 函数调用,但是在webpack打包后的代码里,import preact, { h } from 'preact' 里的 h 没了
164
+ //单个 h 成为无法识别的变量, 即使把 pragma 写成 preact.createElement 也没用,因为 preact 也被改名了
165
+ [loadBabelPlugin('@babel/plugin-transform-react-jsx'), { pragma: 'h' }],
166
+ //这个插件将 h 换成从 preact 中引入的 createElement,它知道 preact 被改名后的值
167
+ [
168
+ loadBabelPlugin('babel-plugin-jsx-pragmatic'),
169
+ {
170
+ module: 'preact',
171
+ import: 'h',
172
+ export: 'createElement',
173
+ },
174
+ ],
175
+ ],
176
+ },
177
+ },
178
+ },
179
+ {
180
+ test: /\.(png|jpg|gif|mp4|lottie)$/,
181
+ loader: require.resolve('url-loader'),
182
+ options: {
183
+ name: 'static/media/[name].[hash:8].[ext]',
184
+ limit: 1,
185
+ },
186
+ },
187
+ {
188
+ test: /\.svelte$/,
189
+ use: [
190
+ {
191
+ loader: 'svelte-loader',
192
+ options: {
193
+ compilerOptions: {
194
+ dev: !isShipping,
195
+ },
196
+ emitCss: true,
197
+ preprocess: sveltePreprocess({}),
198
+ hotReload: false,
199
+ },
200
+ },
201
+ ],
202
+ },
203
+ {
204
+ test: /\.css$/,
205
+ use: [
206
+ MiniCssExtractPlugin.loader,
207
+ {
208
+ loader: require.resolve('css-loader'),
209
+ options: {
210
+ modules: false,
211
+ },
212
+ },
213
+ ],
214
+ },
215
+ {
216
+ test: /\.less$/,
217
+ use: [
218
+ {
219
+ loader: MiniCssExtractPlugin.loader,
220
+ options: {
221
+ publicPath: '../',
222
+ },
223
+ },
224
+ {
225
+ loader: require.resolve('css-loader'),
226
+ options: {
227
+ modules: true,
228
+ esModule: false,
229
+ },
230
+ },
231
+ {
232
+ loader: require.resolve('less-loader'),
233
+ options: { javascriptEnabled: true },
234
+ },
235
+ ],
236
+ },
237
+ ],
238
+ },
239
+
240
+ optimization: {
241
+ minimize: false, // 最终发布会用pfbs转成二进制,所以源码就不用压缩了,方便阅读调试
242
+ },
243
+
244
+ devtool: 'source-map',
245
+
246
+ plugins,
247
+
248
+ //这个属性webpack打包时是不用的,是给pxw里的express做扩展用的,之所以要包在devServer这一层key里是因为webpack5会强制校验配置文件中不包含未知的key,只有这个key作为曾经的配置项被保留下来,刚好用来传递变量
249
+ devServer: {
250
+ getMiddleware: function () {
251
+ return getMiddleware;
252
+ },
253
+ },
254
+ };
255
+
256
+ var getMiddleware = function (appPort, notifyAppReload) {
257
+ // console.log('webpack getMiddleware', appPort, notifyAppReload);
258
+ const middleware = require('webpack-dev-middleware');
259
+ let h5es = require('./h5es');
260
+
261
+ process.env.mode = 'development';
262
+ let config = module.exports;
263
+
264
+ config.mode = 'development';
265
+ config.output.publicPath = `http://${getLocalIP()[0]}:${appPort}/`;
266
+ config.plugins.push(
267
+ //有html文件重新生成时,通知已经打开的页面reload
268
+ new (class PxAutobackupPlugin {
269
+ apply(compiler) {
270
+ compiler.hooks.afterEmit.tap('PxAutoReloadPlugin', (info) => {
271
+ let assets = Object.entries(info.assets).map(([name, asset]) => {
272
+ if (name.endsWith('.html')) {
273
+ console.log('webpack afterEmit', name);
274
+ notifyAppReload(name.split('/')[0]);
275
+ }
276
+ });
277
+ });
278
+ }
279
+ })(),
280
+ );
281
+ const compiler = webpack(config);
282
+ return [
283
+ (req, res, next) => {
284
+ if (req.url == '/') return res.sendFile(pr(rootDir, 'config/index.html'));
285
+ if (req.url == '/api/get-project-config') return res.json({ ...projectConfig, h5esVersions: h5es.h5esVersions, defaultH5esType: getH5esType() });
286
+ if (req.url == '/api/update-tsconfig') {
287
+ try {
288
+ console.log(req.body);
289
+ let h5esType = setH5esType(req.body.typeRoots);
290
+ res.json({
291
+ success: true,
292
+ restartRecommended: true,
293
+ newConfig: h5esType,
294
+ });
295
+ }
296
+ catch (error) {
297
+ console.log(error);
298
+ res.status(500).json({
299
+ error: `更新配置失败: ${error.message}`,
300
+ detail: error.stack,
301
+ });
302
+ }
303
+ return;
304
+ }
305
+
306
+ //rewrites page's subpath /name/a/b/c to /name
307
+ for (let i = 0; i < projectConfig.pages.length; i++) {
308
+ let p = projectConfig.pages[i];
309
+ let prefix = `/${p.name}/`;
310
+ console.log('check rewrite', req.url, prefix);
311
+ if (req.url.startsWith(prefix) && req.url.indexOf('.') < 0) {
312
+ req.url = prefix;
313
+ return next();
314
+ }
315
+ }
316
+ return next();
317
+ },
318
+ middleware(compiler, {
319
+ writeToDisk: true,
320
+ mimeTypes: {
321
+ js: 'text/javascript',
322
+ },
323
+ }),
324
+ ];
325
+ };