hw-weapp-compiler 1.0.1 → 1.0.2

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/build/dev.js CHANGED
@@ -1,9 +1,23 @@
1
+ const path = require('path');
2
+ const fse = require('fs-extra');
1
3
  const webpack = require('webpack');
2
- const { ENV } = require('./config/constants');
4
+ const { ENV, DIST_DIR } = require('./config/constants');
3
5
  const webpackConfig = require('./webpack.config');
4
6
  const { createCompilerHandler } = require('./utils/createCompilerHandler');
5
7
 
6
8
  module.exports = (opts) => {
9
+ // 开发模式启动时清理一次 dist 目录
10
+ if (fse.existsSync(DIST_DIR)) {
11
+ const files = fse.readdirSync(DIST_DIR);
12
+ files.forEach((item) => {
13
+ if (!/project\.config\.json/.test(item)) {
14
+ fse.removeSync(path.resolve(DIST_DIR, item));
15
+ }
16
+ });
17
+ } else {
18
+ fse.mkdirSync(DIST_DIR);
19
+ }
20
+
7
21
  const compiler = webpack(
8
22
  webpackConfig(
9
23
  {
@@ -14,5 +28,12 @@ module.exports = (opts) => {
14
28
  ),
15
29
  );
16
30
 
17
- compiler.watch({}, createCompilerHandler('dev'));
31
+ compiler.watch(
32
+ {
33
+ aggregateTimeout: 300,
34
+ poll: false,
35
+ ignored: /node_modules/,
36
+ },
37
+ createCompilerHandler('dev'),
38
+ );
18
39
  };
@@ -22,19 +22,28 @@ module.exports = async function loader(source) {
22
22
 
23
23
  if (Object.values(entries).indexOf(path.resolve(fileInfo.dir, fileInfo.name)) !== -1) {
24
24
  const exts = ['.less', '.wxss', '.css', '.json', '.wxml'];
25
-
26
- for (let index = 0; index < exts.length; index += 1) {
27
- const ext = exts[index];
28
- const otherFilePath = path.resolve(fileInfo.dir, fileInfo.name + ext);
29
-
30
- if (await fse.pathExists(otherFilePath)) {
25
+ const checks = await Promise.all(
26
+ exts.map(async (ext) => {
27
+ const otherFilePath = path.resolve(fileInfo.dir, fileInfo.name + ext);
28
+ const exists = await fse.pathExists(otherFilePath);
29
+ return { ext, otherFilePath, exists };
30
+ }),
31
+ );
32
+ const existing = checks.filter((r) => r.exists);
33
+ const loadResults = await Promise.all(
34
+ existing.map(async ({ ext, otherFilePath }) => {
31
35
  if (['.less', '.wxss', '.css'].indexOf(ext) !== -1) {
32
- imports.unshift(`import './${fileInfo.name}${ext}'\n`);
33
- } else {
34
- await loadModule.call(this, otherFilePath);
36
+ return { type: 'style', ext, path: otherFilePath };
35
37
  }
38
+ await loadModule.call(this, otherFilePath);
39
+ return { type: 'module', ext, path: otherFilePath };
40
+ }),
41
+ );
42
+ loadResults.forEach((result) => {
43
+ if (result.type === 'style') {
44
+ imports.unshift(`import './${fileInfo.name}${result.ext}'\n`);
36
45
  }
37
- }
46
+ });
38
47
  }
39
48
 
40
49
  callback(null, imports.join(''));
@@ -28,22 +28,23 @@ module.exports = async function loader(source) {
28
28
 
29
29
  const [assets, wxmls] = await getWxmlAssets.call(this, filePath, content);
30
30
 
31
- // 资源文件
32
- for (let index = 0; index < assets.length; index += 1) {
33
- const [attr, file] = assets[index];
34
- const src = await loadModule.call(this, file);
35
-
36
- if (src) {
37
- content = content.replace(attr, withPublicPath(src, publicPath));
31
+ // 资源文件 - 并行 loadModule
32
+ const assetResults = await Promise.all(
33
+ assets.map(async ([attr, file]) => {
34
+ const src = await loadModule.call(this, file);
35
+ return src ? [attr, src] : null;
36
+ }),
37
+ );
38
+ for (let index = 0; index < assetResults.length; index += 1) {
39
+ const result = assetResults[index];
40
+ if (result) {
41
+ const [attr, src] = result;
42
+ content = content.replace(attr, withPublicPath(src));
38
43
  }
39
44
  }
40
45
 
41
- // wxml文件
42
- for (let index = 0; index < wxmls.length; index += 1) {
43
- const [, file] = wxmls[index];
44
-
45
- await loadModule.call(this, file);
46
- }
46
+ // wxml文件 - 并行 loadModule
47
+ await Promise.all(wxmls.map(([, file]) => loadModule.call(this, file)));
47
48
 
48
49
  callback(null, content);
49
50
  };
@@ -16,15 +16,15 @@ module.exports = async function loader(source) {
16
16
 
17
17
  const requires = content.match(/require\(("|').*("|')\)/g) || [];
18
18
 
19
- for (let index = 0; index < requires.length; index += 1) {
20
- const item = requires[index];
21
-
22
- const file = path.resolve(
23
- fileInfo.dir,
24
- item.replace(/^((require\(')|(require\("))/g, '').replace(/(('\))|("\)))$/g, ''),
25
- );
26
- await loadModule.bind(this)(file);
27
- }
19
+ await Promise.all(
20
+ requires.map(async (item) => {
21
+ const file = path.resolve(
22
+ fileInfo.dir,
23
+ item.replace(/^((require\(')|(require\("))/g, '').replace(/(('\))|("\)))$/g, ''),
24
+ );
25
+ await loadModule.call(this, file);
26
+ }),
27
+ );
28
28
 
29
29
  callback(null, content);
30
30
  };
@@ -234,22 +234,23 @@ class WeappPlugin {
234
234
 
235
235
  const { flags, subpackages } = detectCommonModules(items);
236
236
 
237
- for (let index = 0; index < items.length; index += 1) {
238
- const asset = items[index];
239
- const assetName = compatiblePath(asset.name);
240
- const isJs = PATTERNS.JS_EXT.test(assetName);
241
- let content = asset.source.source();
242
-
243
- if (content.toString) {
244
- content = content.toString();
245
- }
246
-
247
- content = injectGlobalModules(content, assetName, flags);
248
- content = injectSubpackageRequires(content, assetName, subpackages);
249
- content = minifyCommonModules(content, assetName, isJs, compiler.options.mode === ENV.DEV);
250
-
251
- compilation.updateAsset(asset.name, new RawSource(content));
252
- }
237
+ await Promise.all(
238
+ items.map(async (asset) => {
239
+ const assetName = compatiblePath(asset.name);
240
+ const isJs = PATTERNS.JS_EXT.test(assetName);
241
+ let content = asset.source.source();
242
+
243
+ if (content.toString) {
244
+ content = content.toString();
245
+ }
246
+
247
+ content = injectGlobalModules(content, assetName, flags);
248
+ content = injectSubpackageRequires(content, assetName, subpackages);
249
+ content = minifyCommonModules(content, assetName, isJs, compiler.options.mode === ENV.DEV);
250
+
251
+ compilation.updateAsset(asset.name, new RawSource(content));
252
+ }),
253
+ );
253
254
  },
254
255
  );
255
256
  });
package/build/prod.js CHANGED
@@ -1,14 +1,39 @@
1
+ const path = require('path');
1
2
  const webpack = require('webpack');
3
+ const TerserPlugin = require('terser-webpack-plugin');
4
+ const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
2
5
  const { ENV } = require('./config/constants');
3
6
  const webpackConfig = require('./webpack.config');
4
7
  const { createCompilerHandler } = require('./utils/createCompilerHandler');
8
+ const { getBuildEnv } = require('./utils/buildEnv');
5
9
 
6
10
  module.exports = (opts) => {
7
11
  const compiler = webpack(
8
12
  webpackConfig(
9
13
  {
10
14
  mode: ENV.PROD,
11
- devtool: 'cheap-module-source-map',
15
+ devtool: false,
16
+ output: {
17
+ clean: {
18
+ keep: /project\.config\.json/,
19
+ },
20
+ },
21
+ optimization: {
22
+ minimizer: [
23
+ new TerserPlugin({
24
+ extractComments: false,
25
+ }),
26
+ new CssMinimizerPlugin(),
27
+ ],
28
+ },
29
+ cache: {
30
+ type: 'filesystem',
31
+ cacheDirectory: path.resolve(process.cwd(), 'node_modules/.cache/webpack'),
32
+ buildDependencies: {
33
+ config: [path.resolve(__dirname, 'webpack.config.js')],
34
+ },
35
+ name: `${getBuildEnv()}-${process.version.slice(0, 6)}`,
36
+ },
12
37
  },
13
38
  opts || {},
14
39
  ),
@@ -68,22 +68,26 @@ function getWxmlAssets(filePath, content) {
68
68
  const assetsImports = [];
69
69
  const wxmlsImports = [];
70
70
 
71
- for (let index = 0; index < wxmls.length; index += 1) {
72
- const attr = wxmls[index];
73
- const result = await resolvePath(attr, path.parse(filePath).dir);
74
-
75
- if (result) {
76
- wxmlsImports.push([attr, result]);
77
- }
71
+ // 并行解析 wxml 路径
72
+ const wxmlResults = await Promise.all(
73
+ wxmls.map(async (attr) => {
74
+ const result = await resolvePath(attr, path.parse(filePath).dir);
75
+ return result ? [attr, result] : null;
76
+ }),
77
+ );
78
+ for (let index = 0; index < wxmlResults.length; index += 1) {
79
+ if (wxmlResults[index]) wxmlsImports.push(wxmlResults[index]);
78
80
  }
79
81
 
80
- for (let index = 0; index < assets.length; index += 1) {
81
- const attr = assets[index];
82
- const result = await resolvePath(attr, path.parse(filePath).dir);
83
-
84
- if (result) {
85
- assetsImports.push([attr, result]);
86
- }
82
+ // 并行解析 asset 路径
83
+ const assetResults = await Promise.all(
84
+ assets.map(async (attr) => {
85
+ const result = await resolvePath(attr, path.parse(filePath).dir);
86
+ return result ? [attr, result] : null;
87
+ }),
88
+ );
89
+ for (let index = 0; index < assetResults.length; index += 1) {
90
+ if (assetResults[index]) assetsImports.push(assetResults[index]);
87
91
  }
88
92
 
89
93
  resolve([assetsImports, wxmlsImports]);
@@ -20,26 +20,11 @@ const { ENV, PATTERNS, ASSET_EXT_PATTERN, SRC_DIR, DIST_DIR, ASSETS_DIR } = requ
20
20
 
21
21
  const defaultCopyFiles = ['project.config.json', 'sitemap.json'];
22
22
 
23
- function cleanOutputDir(output) {
24
- if (fse.existsSync(output)) {
25
- const files = fse.readdirSync(output);
26
- files.forEach((item) => {
27
- if (!/project\.config\.json/.test(item)) {
28
- fse.removeSync(path.resolve(output, item));
29
- }
30
- });
31
- } else {
32
- fse.mkdirSync(output);
33
- }
34
- }
35
-
36
23
  module.exports = (options, { analyzer, quiet } = {}) => {
37
24
  const appConfig = getAppConfig();
38
25
  const { alias, publicPath = 'auto', copyFiles = [] } = getConfig();
39
26
  const entries = getEntries();
40
27
 
41
- cleanOutputDir(DIST_DIR);
42
-
43
28
  const assetsName = (resourcePath) => {
44
29
  if (PATTERNS.NODE_MODULES.test(resourcePath)) {
45
30
  return path.relative(path.resolve(process.cwd(), 'node_modules'), resourcePath);
@@ -229,6 +214,7 @@ module.exports = (options, { analyzer, quiet } = {}) => {
229
214
  resolve: {
230
215
  alias,
231
216
  preferRelative: true,
217
+ symlinks: false,
232
218
  },
233
219
  module: {
234
220
  rules: [
@@ -327,7 +313,7 @@ module.exports = (options, { analyzer, quiet } = {}) => {
327
313
  [
328
314
  '@babel/preset-env',
329
315
  {
330
- targets: 'defaults',
316
+ targets: { chrome: 53 },
331
317
  modules: 'commonjs',
332
318
  },
333
319
  ],
@@ -347,11 +333,12 @@ module.exports = (options, { analyzer, quiet } = {}) => {
347
333
  name: 'runtime',
348
334
  },
349
335
  splitChunks: {
350
- minSize: 0,
336
+ minSize: 10 * 1024,
351
337
  chunks: 'all',
352
338
  cacheGroups,
353
339
  },
354
340
  },
341
+ cache: false,
355
342
  },
356
343
  options,
357
344
  );
package/index.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hw-weapp-compiler",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "基于 webpack5 的小程序构建工具,兼容 Node.js 14~24",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -25,6 +25,7 @@
25
25
  "commander": "^7.2.0",
26
26
  "copy-webpack-plugin": "^8.1.0",
27
27
  "css-loader": "^5.2.0",
28
+ "css-minimizer-webpack-plugin": "^5.0.1",
28
29
  "esdk-obs-nodejs": "^3.21.3",
29
30
  "file-loader": "^6.2.0",
30
31
  "fs-extra": "^9.1.0",
@@ -37,6 +38,7 @@
37
38
  "postcss-loader": "^5.2.0",
38
39
  "postcss-preset-env": "^9.6.0",
39
40
  "progress": "^2.0.3",
41
+ "terser-webpack-plugin": "^5.6.1",
40
42
  "throttle-debounce": "^3.0.1",
41
43
  "uglify-js": "^3.14.2",
42
44
  "url-loader": "^4.1.1",