hw-weapp-compiler 1.0.15 → 1.0.17

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
@@ -2,20 +2,19 @@ const webpack = require('webpack');
2
2
  const { ENV } = require('./config/constants');
3
3
  const webpackConfig = require('./webpack.config');
4
4
  const { createCompilerHandler } = require('./utils/createCompilerHandler');
5
+ const { cleanDistForDev } = require('./utils/cleanDistForDev');
5
6
 
6
7
  module.exports = async (opts) => {
7
- // 开发环境禁用 filesystem cache,避免 output.clean 后复用旧缓存导致副作用产物漏生成。
8
- // watch 进程内仍保留 webpack 的增量编译能力。
8
+ // watch 增量重建不使用 output.clean:app.json / 页面 .json/.wxml 由 js-loader 副作用 emit,
9
+ // 热更新时若入口 JS 未重跑,output.clean 会先删 dist 导致这些文件丢失。
10
+ // 启动时单独清理一次旧产物,保留 weapp.env.json 等启动前写入的文件。
11
+ await cleanDistForDev();
12
+
9
13
  const compiler = webpack(
10
14
  await webpackConfig(
11
15
  {
12
16
  mode: ENV.DEV,
13
17
  devtool: 'cheap-module-source-map',
14
- output: {
15
- clean: {
16
- keep: /^(project\.config\.json|weapp\.env\.json)$/,
17
- },
18
- },
19
18
  },
20
19
  opts || {},
21
20
  ),
@@ -0,0 +1,29 @@
1
+ const path = require('path');
2
+ const fse = require('fs-extra');
3
+ const { DIST_DIR } = require('../config/constants');
4
+
5
+ const KEEP_FILES = new Set(['project.config.json', 'weapp.env.json']);
6
+
7
+ /**
8
+ * 开发模式启动时清理 dist 旧产物。
9
+ * watch 增量重建不再使用 output.clean,避免副作用 emit 的 json/wxml 被清掉后未重生成。
10
+ */
11
+ async function cleanDistForDev() {
12
+ if (!(await fse.pathExists(DIST_DIR))) {
13
+ return;
14
+ }
15
+
16
+ const entries = await fse.readdir(DIST_DIR);
17
+ await Promise.all(
18
+ entries.map(async (entry) => {
19
+ if (KEEP_FILES.has(entry)) {
20
+ return;
21
+ }
22
+ await fse.remove(path.join(DIST_DIR, entry));
23
+ }),
24
+ );
25
+ }
26
+
27
+ module.exports = {
28
+ cleanDistForDev,
29
+ };
@@ -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');
@@ -72,7 +73,18 @@ module.exports = async (options, { analyzer, quiet } = {}) => {
72
73
  return resource;
73
74
  };
74
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
+
75
85
  const cacheGroups = {
86
+ default: false,
87
+ defaultVendors: false,
76
88
  vendors: {
77
89
  minChunks: 1,
78
90
  test(module) {
@@ -81,7 +93,7 @@ module.exports = async (options, { analyzer, quiet } = {}) => {
81
93
  if (resource === false) {
82
94
  return true;
83
95
  }
84
- if (PATTERNS.WXSS_EXT.test(resource)) {
96
+ if (PATTERNS.STYLE_EXTS.test(resource)) {
85
97
  return false;
86
98
  }
87
99
 
@@ -96,7 +108,23 @@ module.exports = async (options, { analyzer, quiet } = {}) => {
96
108
  },
97
109
  vendors_wxss: {
98
110
  minChunks: 2,
99
- 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
+ },
100
128
  name: 'vendors_wxss',
101
129
  reuseExistingChunk: true,
102
130
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hw-weapp-compiler",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "基于 webpack5 的小程序构建工具,兼容 Node.js 14~24",
5
5
  "main": "index.js",
6
6
  "bin": {