hw-weapp-compiler 1.0.14 → 1.0.16

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/prod.js CHANGED
@@ -1,13 +1,14 @@
1
- const path = require('path');
2
1
  const webpack = require('webpack');
3
2
  const TerserPlugin = require('terser-webpack-plugin');
4
3
  const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
5
4
  const { ENV } = require('./config/constants');
6
5
  const webpackConfig = require('./webpack.config');
7
6
  const { createCompilerHandler } = require('./utils/createCompilerHandler');
8
- const { getBuildEnv } = require('./utils/buildEnv');
9
7
 
10
8
  module.exports = async (opts) => {
9
+ // 生产环境禁用 filesystem cache,与 dev 同源:output.clean 后复用旧缓存会导致副作用产物漏生成,
10
+ // 注入逻辑(plugin/index.js 的 PROCESS_ASSETS_STAGE_ADDITIONS)要求每次从干净的 source 重做。
11
+ // production 单次构建本就是冷构建,命中收益低,移除以换确定性。
11
12
  const compiler = webpack(
12
13
  await webpackConfig(
13
14
  {
@@ -28,23 +29,6 @@ module.exports = async (opts) => {
28
29
  }),
29
30
  ],
30
31
  },
31
- cache: {
32
- type: 'filesystem',
33
- cacheDirectory: path.resolve(process.cwd(), 'node_modules/.cache/webpack'),
34
- buildDependencies: {
35
- // webpack.config.js 及其 require 的 getEntries.js / loader / plugin 都作为
36
- // 缓存依赖,本编译器自身代码改动后即时整体失效,避免复用旧 loader 输出。
37
- config: [
38
- path.resolve(__dirname, 'webpack.config.js'),
39
- path.resolve(__dirname, 'config/getEntries.js'),
40
- path.resolve(__dirname, 'plugin/index.js'),
41
- path.resolve(__dirname, 'loader/js-loader.js'),
42
- path.resolve(__dirname, 'loader/wxs-loader.js'),
43
- path.resolve(__dirname, 'loader/wxml-loader.js'),
44
- ],
45
- },
46
- name: `${getBuildEnv()}-${process.version.slice(0, 6)}`,
47
- },
48
32
  },
49
33
  opts || {},
50
34
  ),
@@ -1,14 +1,52 @@
1
+ const { compatiblePath } = require('./compatiblePath');
2
+
1
3
  const nodeModulesUsingComponent = [];
4
+ const nodeModulesUsingComponentDirs = [];
2
5
 
3
- function isNodeModulesUsingComponent(name) {
4
- const key = name.replace(/\.(js|wxml|wxss|json|wxs)$/g, '');
6
+ function normalizeComponentKey(name) {
7
+ const key = compatiblePath(name).replace(/\.(js|wxml|wxss|json|wxs|less|css)$/gi, '');
5
8
  const nmIndex = key.indexOf('node_modules/');
6
- const searchKey = nmIndex !== -1 ? key.substring(nmIndex + 'node_modules/'.length) : key;
7
- return nodeModulesUsingComponent.indexOf(searchKey) !== -1;
9
+
10
+ if (nmIndex !== -1) {
11
+ return key.substring(nmIndex + 'node_modules/'.length);
12
+ }
13
+
14
+ return key;
15
+ }
16
+
17
+ function getComponentDir(key) {
18
+ const segments = key.split('/');
19
+
20
+ if ((key.indexOf('@') === 0 && segments.length <= 2) || segments.length <= 1) {
21
+ return `${key}/`;
22
+ }
23
+
24
+ return `${segments.slice(0, -1).join('/')}/`;
25
+ }
26
+
27
+ function addUnique(list, item) {
28
+ if (list.indexOf(item) === -1) {
29
+ list.push(item);
30
+ }
31
+ }
32
+
33
+ function isNodeModulesUsingComponent(name) {
34
+ const searchKey = normalizeComponentKey(name);
35
+
36
+ if (nodeModulesUsingComponent.indexOf(searchKey) !== -1) {
37
+ return true;
38
+ }
39
+
40
+ return nodeModulesUsingComponentDirs.some((componentDir) => {
41
+ return searchKey.indexOf(componentDir) === 0;
42
+ });
8
43
  }
9
44
 
10
45
  function addNodeModulesUsingComponent(item) {
11
- nodeModulesUsingComponent.push(item);
46
+ const key = normalizeComponentKey(item);
47
+
48
+ addUnique(nodeModulesUsingComponent, key);
49
+ addUnique(nodeModulesUsingComponentDirs, getComponentDir(key));
12
50
  }
13
51
 
14
52
  module.exports = {
@@ -13,6 +13,7 @@ const { getBuildEnv } = require('./utils/buildEnv');
13
13
  const { getConfig } = require('./config/getConfig');
14
14
  const { getEntries } = require('./config/getEntries');
15
15
  const { getAppConfig } = require('./config/getAppConfig');
16
+ const { isNodeModulesUsingComponent } = require('./utils/isNodeModulesUsingComponent');
16
17
 
17
18
  const { isSubpackage } = require('./utils/isSubpackage');
18
19
  const { compatiblePath } = require('./utils/compatiblePath');
@@ -20,6 +21,22 @@ const { ENV, PATTERNS, ASSET_EXT_PATTERN, SRC_DIR, DIST_DIR, ASSETS_DIR } = requ
20
21
 
21
22
  const defaultCopyFiles = ['project.config.json', 'sitemap.json'];
22
23
 
24
+ // 按 dev/prod 把 src/project.config.json 的 appidDev/appidProd 写进 dist 的 appid。
25
+ // 仅改 dist 产物,源文件三段保留不动;缺项则回退源 appid;解析失败原样返回不阻断构建。
26
+ function applyAppidByMode(content, mode) {
27
+ let cfg;
28
+ try {
29
+ cfg = JSON.parse(content.toString());
30
+ } catch (e) {
31
+ return content;
32
+ }
33
+ const pick = mode === ENV.DEV ? 'appidDev' : mode === ENV.PROD ? 'appidProd' : null;
34
+ if (pick && cfg[pick]) {
35
+ cfg.appid = cfg[pick];
36
+ }
37
+ return Buffer.from(JSON.stringify(cfg, null, 2));
38
+ }
39
+
23
40
  module.exports = async (options, { analyzer, quiet } = {}) => {
24
41
  const appConfig = getAppConfig();
25
42
  const { alias = {}, publicPath = 'auto', copyFiles = [] } = getConfig();
@@ -56,7 +73,18 @@ module.exports = async (options, { analyzer, quiet } = {}) => {
56
73
  return resource;
57
74
  };
58
75
 
76
+ const isNodeModulesComponentStyle = (resource) => {
77
+ return (
78
+ resource &&
79
+ PATTERNS.NODE_MODULES.test(resource) &&
80
+ PATTERNS.STYLE_EXTS.test(resource) &&
81
+ isNodeModulesUsingComponent(resource)
82
+ );
83
+ };
84
+
59
85
  const cacheGroups = {
86
+ default: false,
87
+ defaultVendors: false,
60
88
  vendors: {
61
89
  minChunks: 1,
62
90
  test(module) {
@@ -65,7 +93,7 @@ module.exports = async (options, { analyzer, quiet } = {}) => {
65
93
  if (resource === false) {
66
94
  return true;
67
95
  }
68
- if (PATTERNS.WXSS_EXT.test(resource)) {
96
+ if (PATTERNS.STYLE_EXTS.test(resource)) {
69
97
  return false;
70
98
  }
71
99
 
@@ -80,7 +108,23 @@ module.exports = async (options, { analyzer, quiet } = {}) => {
80
108
  },
81
109
  vendors_wxss: {
82
110
  minChunks: 2,
83
- test: PATTERNS.NODE_MODULES,
111
+ test(module) {
112
+ const resource = getResource(module);
113
+
114
+ if (resource === false) {
115
+ return false;
116
+ }
117
+
118
+ if (!PATTERNS.STYLE_EXTS.test(resource)) {
119
+ return false;
120
+ }
121
+
122
+ if (isNodeModulesComponentStyle(resource)) {
123
+ return false;
124
+ }
125
+
126
+ return PATTERNS.NODE_MODULES.test(resource);
127
+ },
84
128
  name: 'vendors_wxss',
85
129
  reuseExistingChunk: true,
86
130
  },
@@ -176,12 +220,17 @@ module.exports = async (options, { analyzer, quiet } = {}) => {
176
220
 
177
221
  const patterns = [];
178
222
  defaultCopyFiles.forEach((file) => {
179
- if (fse.existsSync(path.resolve(SRC_DIR, file))) {
180
- patterns.push({
181
- from: path.resolve(SRC_DIR, file),
182
- to: path.resolve(DIST_DIR, file),
183
- });
223
+ if (!fse.existsSync(path.resolve(SRC_DIR, file))) {
224
+ return;
225
+ }
226
+ const pattern = {
227
+ from: path.resolve(SRC_DIR, file),
228
+ to: path.resolve(DIST_DIR, file),
229
+ };
230
+ if (file === 'project.config.json') {
231
+ pattern.transform = (content) => applyAppidByMode(content, getBuildEnv());
184
232
  }
233
+ patterns.push(pattern);
185
234
  });
186
235
  copyFiles.forEach((file) => {
187
236
  patterns.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hw-weapp-compiler",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "description": "基于 webpack5 的小程序构建工具,兼容 Node.js 14~24",
5
5
  "main": "index.js",
6
6
  "bin": {