hw-weapp-compiler 1.0.0 → 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.
@@ -18,4 +18,6 @@ function traverseDir(dir) {
18
18
  return result;
19
19
  }
20
20
 
21
- module.exports = traverseDir;
21
+ module.exports = {
22
+ traverseDir,
23
+ };
@@ -1,15 +1,19 @@
1
+ /**
2
+ * upload - 构建产物上传工具
3
+ *
4
+ * 支持将构建产物上传到华为云 OBS 或阿里云 OSS,
5
+ * 通过增量检测避免重复上传,并展示上传进度。
6
+ */
1
7
  const path = require('path');
2
8
  const OBSClient = require('esdk-obs-nodejs');
3
9
  const OSSClient = require('ali-oss');
4
- // const chalk = require('chalk');
5
10
  const Progress = require('progress');
6
- const getConfig = require('../config/getConfig');
11
+ const { getConfig } = require('../config/getConfig');
7
12
 
8
13
  const { getStorage, setStorage } = require('./storage');
9
- const compatiblePath = require('./compatiblePath');
10
- const getOutput = require('../config/getOutput');
14
+ const { compatiblePath } = require('./compatiblePath');
15
+ const { DIST_DIR } = require('../config/constants');
11
16
 
12
- const output = getOutput();
13
17
  const { obsConfig, ossConfig } = getConfig();
14
18
 
15
19
  let obsClient;
@@ -37,13 +41,13 @@ function getOssClient() {
37
41
 
38
42
  function doOssUpload(file) {
39
43
  return getOssClient().put(
40
- compatiblePath(path.join(ossConfig.dir, path.relative(output, file))),
44
+ compatiblePath(path.join(ossConfig.dir, path.relative(DIST_DIR, file))),
41
45
  file,
42
46
  );
43
47
  }
44
48
 
45
49
  function getOssStat(file) {
46
- return getOssClient().head(compatiblePath(path.join(ossConfig.dir, path.relative(output, file))));
50
+ return getOssClient().head(compatiblePath(path.join(ossConfig.dir, path.relative(DIST_DIR, file))));
47
51
  }
48
52
 
49
53
  function doObsUpload(file) {
@@ -51,19 +55,16 @@ function doObsUpload(file) {
51
55
  getObsClient().putObject(
52
56
  {
53
57
  Bucket: obsConfig.bucket,
54
- Key: compatiblePath(path.join(obsConfig.dir, path.relative(output, file))),
58
+ Key: compatiblePath(path.join(obsConfig.dir, path.relative(DIST_DIR, file))),
55
59
  SourceFile: file,
56
60
  },
57
61
  (err, result) => {
58
62
  if (err) {
59
63
  reject(err);
60
- // console.log(chalk.red("上传失败"), file);
61
64
  } else if (result.CommonMsg.Status >= 300) {
62
65
  reject(new Error(`OBS upload failed with status ${result.CommonMsg.Status}`));
63
- // console.log(chalk.red("上传失败"), file);
64
66
  } else {
65
67
  resolve(result);
66
- // console.log(chalk.green("上传成功"), file);
67
68
  }
68
69
  },
69
70
  );
@@ -74,7 +75,7 @@ async function getObsStat(file) {
74
75
  getObsClient().getObjectMetadata(
75
76
  {
76
77
  Bucket: obsConfig.bucket,
77
- Key: compatiblePath(path.join(obsConfig.dir, path.relative(output, file))),
78
+ Key: compatiblePath(path.join(obsConfig.dir, path.relative(DIST_DIR, file))),
78
79
  },
79
80
  (err, result) => {
80
81
  if (err) {
@@ -130,9 +131,6 @@ function updateProgress() {
130
131
  });
131
132
  progress = null;
132
133
  uploadQueue = {};
133
- // setTimeout(() => {
134
- // console.log(chalk.green('assets upload completed'));
135
- // }, 0);
136
134
  }
137
135
 
138
136
  return `[${completed}/${total}]`;
@@ -151,9 +149,6 @@ async function checkUpload() {
151
149
  await getStat(file[0]);
152
150
  setStorage(file[0], true);
153
151
  updateProgress();
154
- // console.log(
155
- // chalk.blue(`${publicPath}${path.relative(output, file[0])} ${updateProgress()}`),
156
- // );
157
152
  } catch (error) {
158
153
  try {
159
154
  await doUpload(file[0]);
@@ -162,9 +157,6 @@ async function checkUpload() {
162
157
  console.error(`Failed to upload ${file[0]}:`, uploadError);
163
158
  }
164
159
  updateProgress();
165
- // console.log(
166
- // chalk.green(`${publicPath}${path.relative(output, file[0])} ${updateProgress()}`),
167
- // );
168
160
  }
169
161
  uploadQueue[file[0]] = 'completed';
170
162
  }),
@@ -181,7 +173,7 @@ function addToUploadQueue(assets) {
181
173
  }
182
174
 
183
175
  assets.forEach((asset) => {
184
- const file = path.resolve(output, asset);
176
+ const file = path.resolve(DIST_DIR, asset);
185
177
  if (uploadQueue[file] === undefined) {
186
178
  uploadQueue[file] = false;
187
179
  }
@@ -9,56 +9,32 @@ const autoprefixer = require('autoprefixer');
9
9
  const postcssPresetEnv = require('postcss-preset-env');
10
10
 
11
11
  const WeappPlugin = require('./plugin');
12
- const getResourceAccept = require('./config/getResourceAccept');
13
12
  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');
13
+ const { getConfig } = require('./config/getConfig');
14
+ const { getEntries } = require('./config/getEntries');
15
+ const { getAppConfig } = require('./config/getAppConfig');
20
16
 
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();
17
+ const { isSubpackage } = require('./utils/isSubpackage');
18
+ const { compatiblePath } = require('./utils/compatiblePath');
19
+ const { ENV, PATTERNS, ASSET_EXT_PATTERN, SRC_DIR, DIST_DIR, ASSETS_DIR } = require('./config/constants');
31
20
 
32
21
  const defaultCopyFiles = ['project.config.json', 'sitemap.json'];
33
22
 
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
23
  module.exports = (options, { analyzer, quiet } = {}) => {
48
- // eslint-disable-next-line
49
- const assetsName = (resourcePath, resourceQuery) => {
50
- if (/node_modules/g.test(resourcePath)) {
24
+ const appConfig = getAppConfig();
25
+ const { alias, publicPath = 'auto', copyFiles = [] } = getConfig();
26
+ const entries = getEntries();
27
+
28
+ const assetsName = (resourcePath) => {
29
+ if (PATTERNS.NODE_MODULES.test(resourcePath)) {
51
30
  return path.relative(path.resolve(process.cwd(), 'node_modules'), resourcePath);
52
31
  }
53
32
  return '[path][name].[ext]';
54
33
  };
34
+
55
35
  const plugins = [
56
36
  new MiniCssExtractPlugin({
57
37
  filename: '[name].wxss',
58
- // filename(asset) {
59
- // console.log(asset.chunk.name);
60
- // return '[name].wxss';
61
- // },
62
38
  }),
63
39
  new webpack.DefinePlugin({
64
40
  'process.env.BUILD_ENV': JSON.stringify(getBuildEnv()),
@@ -67,7 +43,6 @@ module.exports = (options, { analyzer, quiet } = {}) => {
67
43
  ];
68
44
 
69
45
  const getResource = (module) => {
70
- // eslint-disable-next-line
71
46
  let resource = module.resource || module._identifier;
72
47
 
73
48
  if (!resource) {
@@ -80,22 +55,20 @@ module.exports = (options, { analyzer, quiet } = {}) => {
80
55
  return resource;
81
56
  };
82
57
 
83
- // chunk
84
58
  const cacheGroups = {
85
59
  vendors: {
86
60
  minChunks: 1,
87
- // test: /\/node_modules\//,
88
61
  test(module) {
89
62
  const resource = getResource(module);
90
63
 
91
64
  if (resource === false) {
92
65
  return true;
93
66
  }
94
- if (/\.wxss/g.test(resource)) {
67
+ if (PATTERNS.WXSS_EXT.test(resource)) {
95
68
  return false;
96
69
  }
97
70
 
98
- if (/node_modules/.test(resource)) {
71
+ if (PATTERNS.NODE_MODULES.test(resource)) {
99
72
  return true;
100
73
  }
101
74
 
@@ -106,24 +79,23 @@ module.exports = (options, { analyzer, quiet } = {}) => {
106
79
  },
107
80
  vendors_wxss: {
108
81
  minChunks: 2,
109
- test: /node_modules/,
82
+ test: PATTERNS.NODE_MODULES,
110
83
  name: 'vendors_wxss',
111
84
  reuseExistingChunk: true,
112
85
  },
113
86
  commons: {
114
87
  minChunks: 2,
115
- // test: /^((?!.*(\/node_modules\/)).)*$/,
116
88
  test(module) {
117
89
  const resource = getResource(module);
118
90
 
119
91
  if (resource === false) {
120
92
  return false;
121
93
  }
122
- if (/node_modules/.test(resource)) {
94
+ if (PATTERNS.NODE_MODULES.test(resource)) {
123
95
  return false;
124
96
  }
125
97
 
126
- if (isSubpackage(path.relative(context, resource))) {
98
+ if (isSubpackage(path.relative(SRC_DIR, resource))) {
127
99
  return false;
128
100
  }
129
101
  return true;
@@ -143,14 +115,14 @@ module.exports = (options, { analyzer, quiet } = {}) => {
143
115
  if (resource === false) {
144
116
  return false;
145
117
  }
146
- if (!/\/$/g.test(root)) {
118
+ if (!/\/$/.test(root)) {
147
119
  root = `${root}/`;
148
120
  }
149
121
 
150
- if (/node_modules/.test(resource)) {
122
+ if (PATTERNS.NODE_MODULES.test(resource)) {
151
123
  return false;
152
124
  }
153
- return compatiblePath(path.relative(context, resource)).indexOf(root) === 0;
125
+ return compatiblePath(path.relative(SRC_DIR, resource)).indexOf(root) === 0;
154
126
  },
155
127
  minChunks: 2,
156
128
  reuseExistingChunk: true,
@@ -174,12 +146,7 @@ module.exports = (options, { analyzer, quiet } = {}) => {
174
146
  postcssOptions: {
175
147
  plugins: [
176
148
  autoprefixer,
177
- [
178
- postcssPresetEnv,
179
- {
180
- // Options
181
- },
182
- ],
149
+ postcssPresetEnv,
183
150
  ],
184
151
  },
185
152
  },
@@ -196,9 +163,6 @@ module.exports = (options, { analyzer, quiet } = {}) => {
196
163
  new webpack.ProgressPlugin({
197
164
  activeModules: false,
198
165
  entries: true,
199
- // handler(percentage, message, ...args) {
200
- // // custom logic
201
- // },
202
166
  modules: true,
203
167
  modulesCount: 5000,
204
168
  profile: false,
@@ -208,20 +172,20 @@ module.exports = (options, { analyzer, quiet } = {}) => {
208
172
  }),
209
173
  );
210
174
  }
211
- // 配置copy plugin
175
+
212
176
  const patterns = [];
213
177
  defaultCopyFiles.forEach((file) => {
214
- if (fse.existsSync(path.resolve(context, file))) {
178
+ if (fse.existsSync(path.resolve(SRC_DIR, file))) {
215
179
  patterns.push({
216
- from: path.resolve(context, file),
217
- to: path.resolve(output, file),
180
+ from: path.resolve(SRC_DIR, file),
181
+ to: path.resolve(DIST_DIR, file),
218
182
  });
219
183
  }
220
184
  });
221
185
  copyFiles.forEach((file) => {
222
186
  patterns.push({
223
- from: path.resolve(context, file.from),
224
- to: path.resolve(output, file.to),
187
+ from: path.resolve(SRC_DIR, file.from),
188
+ to: path.resolve(DIST_DIR, file.to),
225
189
  });
226
190
  });
227
191
 
@@ -237,35 +201,35 @@ module.exports = (options, { analyzer, quiet } = {}) => {
237
201
  return merge(
238
202
  {
239
203
  mode: ENV.PROD,
240
- entry: entrys,
241
- context,
204
+ entry: entries,
205
+ context: SRC_DIR,
242
206
  stats: {
243
207
  errorDetails: true,
244
208
  },
245
209
  output: {
246
- path: output,
210
+ path: DIST_DIR,
247
211
  publicPath,
248
212
  },
249
213
  plugins,
250
214
  resolve: {
251
215
  alias,
252
216
  preferRelative: true,
217
+ symlinks: false,
253
218
  },
254
219
  module: {
255
220
  rules: [
256
221
  {
257
- test: getResourceAccept(),
222
+ test: ASSET_EXT_PATTERN,
258
223
  use: [
259
224
  {
260
225
  loader: 'url-loader',
261
226
  options: {
262
227
  limit: 0,
263
- // true 需要 .default, false 不需要 .defualt
264
228
  esModule: false,
265
229
  fallback: {
266
230
  loader: 'file-loader',
267
231
  options: {
268
- name: `${assets}/[name].[hash].[ext]`,
232
+ name: `${ASSETS_DIR}/[name].[hash].[ext]`,
269
233
  },
270
234
  },
271
235
  },
@@ -274,22 +238,21 @@ module.exports = (options, { analyzer, quiet } = {}) => {
274
238
  },
275
239
  {
276
240
  test: /\.(wxss|css)$/i,
277
- include: /node_modules/,
241
+ include: PATTERNS.NODE_MODULES,
278
242
  use: [...getCssLoader()],
279
243
  },
280
244
  {
281
- // test: /\.less$/i,
282
245
  test: /\.(less|wxss|css)$/i,
283
- exclude: /node_modules/,
246
+ exclude: PATTERNS.NODE_MODULES,
284
247
  use: [
285
248
  ...getCssLoader({
286
249
  use: [
287
250
  {
288
- loader: 'less-loader', // compiles Less to CSS
251
+ loader: 'less-loader',
289
252
  options: {
290
253
  lessOptions() {
291
254
  return {
292
- paths: [context],
255
+ paths: [SRC_DIR],
293
256
  };
294
257
  },
295
258
  },
@@ -350,7 +313,7 @@ module.exports = (options, { analyzer, quiet } = {}) => {
350
313
  [
351
314
  '@babel/preset-env',
352
315
  {
353
- targets: 'defaults',
316
+ targets: { chrome: 53 },
354
317
  modules: 'commonjs',
355
318
  },
356
319
  ],
@@ -370,12 +333,13 @@ module.exports = (options, { analyzer, quiet } = {}) => {
370
333
  name: 'runtime',
371
334
  },
372
335
  splitChunks: {
373
- minSize: 0,
336
+ minSize: 10 * 1024,
374
337
  chunks: 'all',
375
338
  cacheGroups,
376
339
  },
377
340
  },
341
+ cache: false,
378
342
  },
379
343
  options,
380
344
  );
381
- };
345
+ };
package/index.js CHANGED
@@ -12,7 +12,7 @@ const fs = require('fs');
12
12
  const dev = require('./build/dev');
13
13
  const prod = require('./build/prod');
14
14
  const { setBuildEnv } = require('./build/utils/buildEnv');
15
- const ENV = require('./build/config/env');
15
+ const { ENV } = require('./build/config/constants');
16
16
 
17
17
  const { version } = JSON.parse(
18
18
  fs.readFileSync(path.resolve(__dirname, './package.json')).toString(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hw-weapp-compiler",
3
- "version": "1.0.0",
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,11 +38,10 @@
37
38
  "postcss-loader": "^5.2.0",
38
39
  "postcss-preset-env": "^9.6.0",
39
40
  "progress": "^2.0.3",
40
- "style-loader": "^2.0.0",
41
+ "terser-webpack-plugin": "^5.6.1",
41
42
  "throttle-debounce": "^3.0.1",
42
43
  "uglify-js": "^3.14.2",
43
44
  "url-loader": "^4.1.1",
44
- "weapp-compiler-hw": "^0.0.1",
45
45
  "webpack": "^5.61.0",
46
46
  "webpack-bundle-analyzer": "^4.4.0",
47
47
  "webpack-merge": "^5.7.3",
@@ -1,7 +0,0 @@
1
- const ENV = {
2
- DEV: 'development',
3
- PROD: 'production',
4
- SIMULATION: 'simulation',
5
- UNKOWN: 'unkown',
6
- };
7
- module.exports = ENV;
@@ -1 +0,0 @@
1
- module.exports = () => 'assets';
@@ -1,5 +0,0 @@
1
- const path = require('path');
2
-
3
- const context = path.resolve(process.cwd(), 'src');
4
-
5
- module.exports = () => context;
@@ -1,5 +0,0 @@
1
- module.exports = () => ({
2
- DEV: 'development',
3
- PROD: 'production',
4
- SIMULATION: 'simulation',
5
- });
@@ -1,7 +0,0 @@
1
- const path = require('path');
2
-
3
- function getOutput() {
4
- return path.resolve(process.cwd(), 'dist');
5
- }
6
-
7
- module.exports = getOutput;
@@ -1 +0,0 @@
1
- module.exports = () => /\.(png|jpg|gif|jpeg|svg|ttf|woff|eot|woff2|otf|mp3|mp4|wav|json|html)$/i;
@@ -1,11 +0,0 @@
1
- const fs = require('fs-extra');
2
- const path = require('path');
3
- const getOutput = require('../config/getOutput');
4
- const ENV = require('../config/env');
5
-
6
- module.exports = (config) => {
7
- fs.writeJSONSync(path.join(getOutput(), 'weapp.env.json'), {
8
- env: ENV.DEV,
9
- ...config,
10
- });
11
- };