hw-weapp-compiler 1.0.13 → 1.0.15

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,11 +1,11 @@
1
- const path = require('path');
2
1
  const webpack = require('webpack');
3
2
  const { ENV } = require('./config/constants');
4
3
  const webpackConfig = require('./webpack.config');
5
4
  const { createCompilerHandler } = require('./utils/createCompilerHandler');
6
5
 
7
6
  module.exports = async (opts) => {
8
- // dev 同样启用 filesystem cache,watch 重建复用缓存,二次启动显著提速
7
+ // 开发环境禁用 filesystem cache,避免 output.clean 后复用旧缓存导致副作用产物漏生成。
8
+ // watch 进程内仍保留 webpack 的增量编译能力。
9
9
  const compiler = webpack(
10
10
  await webpackConfig(
11
11
  {
@@ -16,23 +16,6 @@ module.exports = async (opts) => {
16
16
  keep: /^(project\.config\.json|weapp\.env\.json)$/,
17
17
  },
18
18
  },
19
- cache: {
20
- type: 'filesystem',
21
- cacheDirectory: path.resolve(process.cwd(), 'node_modules/.cache/webpack'),
22
- buildDependencies: {
23
- // webpack.config.js 及其 require 的 getEntries.js / loader / plugin 都作为
24
- // 缓存依赖,本编译器自身代码改动后即时整体失效,避免复用旧 loader 输出。
25
- config: [
26
- path.resolve(__dirname, 'webpack.config.js'),
27
- path.resolve(__dirname, 'config/getEntries.js'),
28
- path.resolve(__dirname, 'plugin/index.js'),
29
- path.resolve(__dirname, 'loader/js-loader.js'),
30
- path.resolve(__dirname, 'loader/wxs-loader.js'),
31
- path.resolve(__dirname, 'loader/wxml-loader.js'),
32
- ],
33
- },
34
- name: `dev-${process.version.slice(0, 6)}`,
35
- },
36
19
  },
37
20
  opts || {},
38
21
  ),
@@ -10,6 +10,7 @@ const { getEntries } = require('../config/getEntries');
10
10
  const { loadModule } = require('../utils/loadModule');
11
11
 
12
12
  module.exports = async function loader(source) {
13
+ this.cacheable(true);
13
14
  const filePath = this.resourcePath;
14
15
  const fileInfo = path.parse(filePath);
15
16
  const sourceStr = source.toString();
@@ -17,14 +18,8 @@ module.exports = async function loader(source) {
17
18
  const imports = [sourceStr];
18
19
 
19
20
  const entries = await getEntries();
20
- const entryPath = path.resolve(fileInfo.dir, fileInfo.name);
21
- const isEntryFile = Object.values(entries).indexOf(entryPath) !== -1;
22
21
 
23
- // 入口 JS 会通过 loadModule 产出同名 json/wxml。若入口模块命中持久化缓存,
24
- // dev 二次启动时 output.clean 删除后的 app.json 不会重新 emit。
25
- this.cacheable(!isEntryFile);
26
-
27
- if (isEntryFile) {
22
+ if (Object.values(entries).indexOf(path.resolve(fileInfo.dir, fileInfo.name)) !== -1) {
28
23
  const exts = ['.less', '.wxss', '.css', '.json', '.wxml'];
29
24
  const checks = await Promise.all(
30
25
  exts.map(async (ext) => {
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
  ),
@@ -20,6 +20,22 @@ 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
+ // 按 dev/prod 把 src/project.config.json 的 appidDev/appidProd 写进 dist 的 appid。
24
+ // 仅改 dist 产物,源文件三段保留不动;缺项则回退源 appid;解析失败原样返回不阻断构建。
25
+ function applyAppidByMode(content, mode) {
26
+ let cfg;
27
+ try {
28
+ cfg = JSON.parse(content.toString());
29
+ } catch (e) {
30
+ return content;
31
+ }
32
+ const pick = mode === ENV.DEV ? 'appidDev' : mode === ENV.PROD ? 'appidProd' : null;
33
+ if (pick && cfg[pick]) {
34
+ cfg.appid = cfg[pick];
35
+ }
36
+ return Buffer.from(JSON.stringify(cfg, null, 2));
37
+ }
38
+
23
39
  module.exports = async (options, { analyzer, quiet } = {}) => {
24
40
  const appConfig = getAppConfig();
25
41
  const { alias = {}, publicPath = 'auto', copyFiles = [] } = getConfig();
@@ -176,12 +192,17 @@ module.exports = async (options, { analyzer, quiet } = {}) => {
176
192
 
177
193
  const patterns = [];
178
194
  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
- });
195
+ if (!fse.existsSync(path.resolve(SRC_DIR, file))) {
196
+ return;
197
+ }
198
+ const pattern = {
199
+ from: path.resolve(SRC_DIR, file),
200
+ to: path.resolve(DIST_DIR, file),
201
+ };
202
+ if (file === 'project.config.json') {
203
+ pattern.transform = (content) => applyAppidByMode(content, getBuildEnv());
184
204
  }
205
+ patterns.push(pattern);
185
206
  });
186
207
  copyFiles.forEach((file) => {
187
208
  patterns.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hw-weapp-compiler",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "基于 webpack5 的小程序构建工具,兼容 Node.js 14~24",
5
5
  "main": "index.js",
6
6
  "bin": {