hw-weapp-compiler 1.0.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.
@@ -0,0 +1,381 @@
1
+ const { merge } = require('webpack-merge');
2
+ const path = require('path');
3
+ const webpack = require('webpack');
4
+ const fse = require('fs-extra');
5
+ const CopyPlugin = require('copy-webpack-plugin');
6
+ const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
7
+ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
8
+ const autoprefixer = require('autoprefixer');
9
+ const postcssPresetEnv = require('postcss-preset-env');
10
+
11
+ const WeappPlugin = require('./plugin');
12
+ const getResourceAccept = require('./config/getResourceAccept');
13
+ const { getBuildEnv } = require('./utils/buildEnv');
14
+ const getContext = require('./config/getContext');
15
+ const getConfig = require('./config/getConfig');
16
+ const getOutput = require('./config/getOutput');
17
+ const getEntrys = require('./config/getEntrys');
18
+ const getAssets = require('./config/getAssets');
19
+ const getAppConfig = require('./config/getAppConfig');
20
+
21
+ const isSubpackage = require('./utils/isSubpackage');
22
+ const compatiblePath = require('./utils/compatiblePath');
23
+ const ENV = require('./config/env');
24
+
25
+ const appConfig = getAppConfig();
26
+ const assets = getAssets();
27
+ const context = getContext();
28
+ const { alias, publicPath = 'auto', copyFiles = [] } = getConfig();
29
+ const output = getOutput();
30
+ const entrys = getEntrys();
31
+
32
+ const defaultCopyFiles = ['project.config.json', 'sitemap.json'];
33
+
34
+ // 删除output目录文件
35
+ if (fse.existsSync(output)) {
36
+ const files = fse.readdirSync(output);
37
+
38
+ files.forEach((item) => {
39
+ if (!/project\.config\.json/g.test(item)) {
40
+ fse.removeSync(path.resolve(output, item));
41
+ }
42
+ });
43
+ } else {
44
+ fse.mkdirSync(output);
45
+ }
46
+
47
+ module.exports = (options, { analyzer, quiet } = {}) => {
48
+ // eslint-disable-next-line
49
+ const assetsName = (resourcePath, resourceQuery) => {
50
+ if (/node_modules/g.test(resourcePath)) {
51
+ return path.relative(path.resolve(process.cwd(), 'node_modules'), resourcePath);
52
+ }
53
+ return '[path][name].[ext]';
54
+ };
55
+ const plugins = [
56
+ new MiniCssExtractPlugin({
57
+ filename: '[name].wxss',
58
+ // filename(asset) {
59
+ // console.log(asset.chunk.name);
60
+ // return '[name].wxss';
61
+ // },
62
+ }),
63
+ new webpack.DefinePlugin({
64
+ 'process.env.BUILD_ENV': JSON.stringify(getBuildEnv()),
65
+ }),
66
+ new WeappPlugin(),
67
+ ];
68
+
69
+ const getResource = (module) => {
70
+ // eslint-disable-next-line
71
+ let resource = module.resource || module._identifier;
72
+
73
+ if (!resource) {
74
+ return false;
75
+ }
76
+
77
+ resource = resource.split('!');
78
+ resource = resource[resource.length - 1];
79
+
80
+ return resource;
81
+ };
82
+
83
+ // chunk
84
+ const cacheGroups = {
85
+ vendors: {
86
+ minChunks: 1,
87
+ // test: /\/node_modules\//,
88
+ test(module) {
89
+ const resource = getResource(module);
90
+
91
+ if (resource === false) {
92
+ return true;
93
+ }
94
+ if (/\.wxss/g.test(resource)) {
95
+ return false;
96
+ }
97
+
98
+ if (/node_modules/.test(resource)) {
99
+ return true;
100
+ }
101
+
102
+ return false;
103
+ },
104
+ name: 'vendors',
105
+ reuseExistingChunk: true,
106
+ },
107
+ vendors_wxss: {
108
+ minChunks: 2,
109
+ test: /node_modules/,
110
+ name: 'vendors_wxss',
111
+ reuseExistingChunk: true,
112
+ },
113
+ commons: {
114
+ minChunks: 2,
115
+ // test: /^((?!.*(\/node_modules\/)).)*$/,
116
+ test(module) {
117
+ const resource = getResource(module);
118
+
119
+ if (resource === false) {
120
+ return false;
121
+ }
122
+ if (/node_modules/.test(resource)) {
123
+ return false;
124
+ }
125
+
126
+ if (isSubpackage(path.relative(context, resource))) {
127
+ return false;
128
+ }
129
+ return true;
130
+ },
131
+ name: 'commons',
132
+ reuseExistingChunk: true,
133
+ },
134
+ };
135
+ (appConfig.subpackages || []).forEach((pkg) => {
136
+ let { root } = pkg;
137
+ const name = path.join(root, 'subpackage_common');
138
+ cacheGroups[name] = {
139
+ name,
140
+ test(module) {
141
+ const resource = getResource(module);
142
+
143
+ if (resource === false) {
144
+ return false;
145
+ }
146
+ if (!/\/$/g.test(root)) {
147
+ root = `${root}/`;
148
+ }
149
+
150
+ if (/node_modules/.test(resource)) {
151
+ return false;
152
+ }
153
+ return compatiblePath(path.relative(context, resource)).indexOf(root) === 0;
154
+ },
155
+ minChunks: 2,
156
+ reuseExistingChunk: true,
157
+ };
158
+ });
159
+
160
+ const getCssLoader = ({ use = [] } = {}) => {
161
+ return [
162
+ {
163
+ loader: MiniCssExtractPlugin.loader,
164
+ },
165
+ {
166
+ loader: 'css-loader',
167
+ options: {
168
+ importLoaders: use.length + 1,
169
+ },
170
+ },
171
+ {
172
+ loader: 'postcss-loader',
173
+ options: {
174
+ postcssOptions: {
175
+ plugins: [
176
+ autoprefixer,
177
+ [
178
+ postcssPresetEnv,
179
+ {
180
+ // Options
181
+ },
182
+ ],
183
+ ],
184
+ },
185
+ },
186
+ },
187
+ ...use,
188
+ ];
189
+ };
190
+
191
+ if (analyzer) {
192
+ plugins.push(new BundleAnalyzerPlugin());
193
+ }
194
+ if (quiet !== true) {
195
+ plugins.push(
196
+ new webpack.ProgressPlugin({
197
+ activeModules: false,
198
+ entries: true,
199
+ // handler(percentage, message, ...args) {
200
+ // // custom logic
201
+ // },
202
+ modules: true,
203
+ modulesCount: 5000,
204
+ profile: false,
205
+ dependencies: true,
206
+ dependenciesCount: 10000,
207
+ percentBy: null,
208
+ }),
209
+ );
210
+ }
211
+ // 配置copy plugin
212
+ const patterns = [];
213
+ defaultCopyFiles.forEach((file) => {
214
+ if (fse.existsSync(path.resolve(context, file))) {
215
+ patterns.push({
216
+ from: path.resolve(context, file),
217
+ to: path.resolve(output, file),
218
+ });
219
+ }
220
+ });
221
+ copyFiles.forEach((file) => {
222
+ patterns.push({
223
+ from: path.resolve(context, file.from),
224
+ to: path.resolve(output, file.to),
225
+ });
226
+ });
227
+
228
+ plugins.push(
229
+ new CopyPlugin({
230
+ patterns,
231
+ options: {
232
+ concurrency: 100,
233
+ },
234
+ }),
235
+ );
236
+
237
+ return merge(
238
+ {
239
+ mode: ENV.PROD,
240
+ entry: entrys,
241
+ context,
242
+ stats: {
243
+ errorDetails: true,
244
+ },
245
+ output: {
246
+ path: output,
247
+ publicPath,
248
+ },
249
+ plugins,
250
+ resolve: {
251
+ alias,
252
+ preferRelative: true,
253
+ },
254
+ module: {
255
+ rules: [
256
+ {
257
+ test: getResourceAccept(),
258
+ use: [
259
+ {
260
+ loader: 'url-loader',
261
+ options: {
262
+ limit: 0,
263
+ // true 需要 .default, false 不需要 .defualt
264
+ esModule: false,
265
+ fallback: {
266
+ loader: 'file-loader',
267
+ options: {
268
+ name: `${assets}/[name].[hash].[ext]`,
269
+ },
270
+ },
271
+ },
272
+ },
273
+ ],
274
+ },
275
+ {
276
+ test: /\.(wxss|css)$/i,
277
+ include: /node_modules/,
278
+ use: [...getCssLoader()],
279
+ },
280
+ {
281
+ // test: /\.less$/i,
282
+ test: /\.(less|wxss|css)$/i,
283
+ exclude: /node_modules/,
284
+ use: [
285
+ ...getCssLoader({
286
+ use: [
287
+ {
288
+ loader: 'less-loader', // compiles Less to CSS
289
+ options: {
290
+ lessOptions() {
291
+ return {
292
+ paths: [context],
293
+ };
294
+ },
295
+ },
296
+ },
297
+ ],
298
+ }),
299
+ ],
300
+ },
301
+ {
302
+ test: /\.(json)$/i,
303
+ type: 'javascript/auto',
304
+ use: [
305
+ {
306
+ loader: 'file-loader',
307
+ options: {
308
+ name: assetsName,
309
+ },
310
+ },
311
+ ],
312
+ },
313
+ {
314
+ test: /\.(wxs)$/i,
315
+ type: 'javascript/auto',
316
+ use: [
317
+ {
318
+ loader: 'file-loader',
319
+ options: {
320
+ name: assetsName,
321
+ },
322
+ },
323
+ {
324
+ loader: path.resolve(__dirname, 'loader/wxs-loader'),
325
+ },
326
+ ],
327
+ },
328
+ {
329
+ test: /\.(wxml)$/i,
330
+ use: [
331
+ {
332
+ loader: 'file-loader',
333
+ options: {
334
+ name: assetsName,
335
+ },
336
+ },
337
+ {
338
+ loader: path.resolve(__dirname, 'loader/wxml-loader'),
339
+ },
340
+ ],
341
+ },
342
+ {
343
+ test: /\.(js)$/i,
344
+ use: [
345
+ {
346
+ loader: 'babel-loader',
347
+ options: {
348
+ cacheDirectory: true,
349
+ presets: [
350
+ [
351
+ '@babel/preset-env',
352
+ {
353
+ targets: 'defaults',
354
+ modules: 'commonjs',
355
+ },
356
+ ],
357
+ ],
358
+ plugins: ['@babel/plugin-transform-runtime'],
359
+ },
360
+ },
361
+ {
362
+ loader: path.resolve(__dirname, 'loader/js-loader'),
363
+ },
364
+ ],
365
+ },
366
+ ],
367
+ },
368
+ optimization: {
369
+ runtimeChunk: {
370
+ name: 'runtime',
371
+ },
372
+ splitChunks: {
373
+ minSize: 0,
374
+ chunks: 'all',
375
+ cacheGroups,
376
+ },
377
+ },
378
+ },
379
+ options,
380
+ );
381
+ };
package/index.js ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env node
2
+
3
+ // 高版本 Node.js (>=17) 兼容 OpenSSL 3.0
4
+ const nodeMajor = process.versions.node.split('.')[0];
5
+ if (nodeMajor >= 17) {
6
+ process.env.NODE_OPTIONS = `${process.env.NODE_OPTIONS || ''} --openssl-legacy-provider`.trim();
7
+ }
8
+
9
+ const { program } = require('commander');
10
+ const path = require('path');
11
+ const fs = require('fs');
12
+ const dev = require('./build/dev');
13
+ const prod = require('./build/prod');
14
+ const { setBuildEnv } = require('./build/utils/buildEnv');
15
+ const ENV = require('./build/config/env');
16
+
17
+ const { version } = JSON.parse(
18
+ fs.readFileSync(path.resolve(__dirname, './package.json')).toString(),
19
+ );
20
+ const compiler = {
21
+ dev,
22
+ prod,
23
+ };
24
+
25
+ program.version(version);
26
+ program.option('-a, --analyzer', 'webpack-bundle-analyzer');
27
+ program.option('-s, --simulation', 'process.env.BUILD_ENV = simulation');
28
+ program.option('-d, --development', 'process.env.BUILD_ENV = development');
29
+ program.option('-p, --production', 'process.env.BUILD_ENV = production');
30
+ program.option('-q, --quiet', '安静模式,打印减少');
31
+
32
+ program.command('dev').action(() => {
33
+ setBuildEnv({
34
+ mode: ENV.DEV,
35
+ ...program.opts(),
36
+ });
37
+ compiler.dev(program.opts());
38
+ });
39
+ program.command('build').action(() => {
40
+ setBuildEnv({
41
+ mode: ENV.PROD,
42
+ ...program.opts(),
43
+ });
44
+ compiler.prod(program.opts());
45
+ });
46
+
47
+ program.parse(process.argv);
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "hw-weapp-compiler",
3
+ "version": "1.0.0",
4
+ "description": "基于 webpack5 的小程序构建工具,兼容 Node.js 14~24",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "weapp": "index.js"
8
+ },
9
+ "author": "go1024",
10
+ "license": "ISC",
11
+ "publishConfig": {
12
+ "registry": "https://registry.npmjs.org"
13
+ },
14
+ "engines": {
15
+ "node": ">=14.17.1"
16
+ },
17
+ "dependencies": {
18
+ "@babel/core": "^7.13.14",
19
+ "@babel/plugin-transform-runtime": "^7.13.10",
20
+ "@babel/preset-env": "^7.13.12",
21
+ "ali-oss": "^6.15.2",
22
+ "autoprefixer": "^10.3.4",
23
+ "babel-loader": "^8.2.2",
24
+ "chalk": "^4.1.0",
25
+ "commander": "^7.2.0",
26
+ "copy-webpack-plugin": "^8.1.0",
27
+ "css-loader": "^5.2.0",
28
+ "esdk-obs-nodejs": "^3.21.3",
29
+ "file-loader": "^6.2.0",
30
+ "fs-extra": "^9.1.0",
31
+ "hasha": "^5.2.2",
32
+ "htmlparser2": "^6.0.1",
33
+ "less": "^4.1.1",
34
+ "less-loader": "^8.0.0",
35
+ "loader-utils": "^2.0.0",
36
+ "mini-css-extract-plugin": "^1.6.0",
37
+ "postcss-loader": "^5.2.0",
38
+ "postcss-preset-env": "^9.6.0",
39
+ "progress": "^2.0.3",
40
+ "style-loader": "^2.0.0",
41
+ "throttle-debounce": "^3.0.1",
42
+ "uglify-js": "^3.14.2",
43
+ "url-loader": "^4.1.1",
44
+ "weapp-compiler-hw": "^0.0.1",
45
+ "webpack": "^5.61.0",
46
+ "webpack-bundle-analyzer": "^4.4.0",
47
+ "webpack-merge": "^5.7.3",
48
+ "webpack-sources": "^2.2.0"
49
+ },
50
+ "devDependencies": {
51
+ "nodemon": "^3.1.14"
52
+ },
53
+ "scripts": {
54
+ "build": "node ./index.js build",
55
+ "dev": "nodemon --inspect --trace-deprecation --watch build ./index.js dev",
56
+ "simulation": "node ./index.js build -s"
57
+ }
58
+ }