• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const path = require('path');
17const fs = require('fs');
18const webpack = require('webpack');
19const archiver = require('archiver');
20const packageJson = require('./package.json');
21const { copyESLibs } = require('./scripts/copylibs');
22
23class PackPlugin {
24
25  apply(compiler) {
26    compiler.hooks.done.tap('PackPlugin', (stats) => {
27      const bundleName = 'api-collector.js';
28      const bundlejsPath = path.resolve(__dirname, 'dist', 'build', bundleName);
29      if (!fs.existsSync(bundlejsPath)) {
30        console.error(`${bundleName} not found`);
31        return;
32      }
33      copyESLibs();
34      const libsPath = path.resolve(__dirname, 'libs');
35      const readme = path.resolve(__dirname, 'reademe.md');
36      const outputName = path.resolve(__dirname, 'dist', `apiCollector-${packageJson.version}.zip`);
37      const outputZipStream = fs.createWriteStream(outputName);
38      const archive = archiver('zip');
39      archive.pipe(outputZipStream);
40      archive.file(bundlejsPath, { name: bundleName });
41      archive.file(readme, { name: 'README.md' });
42      archive.directory(libsPath, 'libs');
43      archive.finalize();
44    });
45  }
46}
47
48module.exports = {
49  entry: './src/entry/main.js',
50  mode: 'none',
51  target: 'node',
52  output: {
53    path: path.resolve(__dirname, 'dist', 'build'),
54    filename: 'api-collector.js',
55  },
56  // webpack v4+ 优先使用 package.json 中的 module 字段,json5 配置了 module 字段并且
57  // 通过 default 导出,因此在打包之后会出现 JSON5.parse 找不到(实际上为JSON5.default.parse)
58  // 优先选择 main 字段
59  resolve: {
60    mainFields: ['main', 'module', 'browser'],
61  },
62  plugins: [
63    new webpack.DefinePlugin({
64      'process.env.bundleMode': true,
65    }),
66    new PackPlugin()
67  ],
68};