161847f8eSopenharmony_ci/*
261847f8eSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
361847f8eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
461847f8eSopenharmony_ci * you may not use this file except in compliance with the License.
561847f8eSopenharmony_ci * You may obtain a copy of the License at
661847f8eSopenharmony_ci *
761847f8eSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
861847f8eSopenharmony_ci *
961847f8eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1061847f8eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1161847f8eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1261847f8eSopenharmony_ci * See the License for the specific language governing permissions and
1361847f8eSopenharmony_ci * limitations under the License.
1461847f8eSopenharmony_ci */
1561847f8eSopenharmony_ci
1661847f8eSopenharmony_ciconst path = require('path');
1761847f8eSopenharmony_ciconst fs = require('fs');
1861847f8eSopenharmony_ciconst webpack = require('webpack');
1961847f8eSopenharmony_ciconst archiver = require('archiver');
2061847f8eSopenharmony_ciconst packageJson = require('./package.json');
2161847f8eSopenharmony_ciconst { copyESLibs } = require('./scripts/copylibs');
2261847f8eSopenharmony_ci
2361847f8eSopenharmony_ciclass PackPlugin {
2461847f8eSopenharmony_ci
2561847f8eSopenharmony_ci  apply(compiler) {
2661847f8eSopenharmony_ci    compiler.hooks.done.tap('PackPlugin', (stats) => {
2761847f8eSopenharmony_ci      const bundleName = 'api-collector.js';
2861847f8eSopenharmony_ci      const bundlejsPath = path.resolve(__dirname, 'dist', 'build', bundleName);
2961847f8eSopenharmony_ci      if (!fs.existsSync(bundlejsPath)) {
3061847f8eSopenharmony_ci        console.error(`${bundleName} not found`);
3161847f8eSopenharmony_ci        return;
3261847f8eSopenharmony_ci      }
3361847f8eSopenharmony_ci      copyESLibs();
3461847f8eSopenharmony_ci      const libsPath = path.resolve(__dirname, 'libs');
3561847f8eSopenharmony_ci      const readme = path.resolve(__dirname, 'reademe.md');
3661847f8eSopenharmony_ci      const outputName = path.resolve(__dirname, 'dist', `apiCollector-${packageJson.version}.zip`);
3761847f8eSopenharmony_ci      const outputZipStream = fs.createWriteStream(outputName);
3861847f8eSopenharmony_ci      const archive = archiver('zip');
3961847f8eSopenharmony_ci      archive.pipe(outputZipStream);
4061847f8eSopenharmony_ci      archive.file(bundlejsPath, { name: bundleName });
4161847f8eSopenharmony_ci      archive.file(readme, { name: 'README.md' });
4261847f8eSopenharmony_ci      archive.directory(libsPath, 'libs');
4361847f8eSopenharmony_ci      archive.finalize();
4461847f8eSopenharmony_ci    });
4561847f8eSopenharmony_ci  }
4661847f8eSopenharmony_ci}
4761847f8eSopenharmony_ci
4861847f8eSopenharmony_cimodule.exports = {
4961847f8eSopenharmony_ci  entry: './src/entry/main.js',
5061847f8eSopenharmony_ci  mode: 'none',
5161847f8eSopenharmony_ci  target: 'node',
5261847f8eSopenharmony_ci  output: {
5361847f8eSopenharmony_ci    path: path.resolve(__dirname, 'dist', 'build'),
5461847f8eSopenharmony_ci    filename: 'api-collector.js',
5561847f8eSopenharmony_ci  },
5661847f8eSopenharmony_ci  // webpack v4+ 优先使用 package.json 中的 module 字段,json5 配置了 module 字段并且
5761847f8eSopenharmony_ci  // 通过 default 导出,因此在打包之后会出现 JSON5.parse 找不到(实际上为JSON5.default.parse)
5861847f8eSopenharmony_ci  // 优先选择 main 字段
5961847f8eSopenharmony_ci  resolve: {
6061847f8eSopenharmony_ci    mainFields: ['main', 'module', 'browser'],
6161847f8eSopenharmony_ci  },
6261847f8eSopenharmony_ci  plugins: [
6361847f8eSopenharmony_ci    new webpack.DefinePlugin({
6461847f8eSopenharmony_ci      'process.env.bundleMode': true,
6561847f8eSopenharmony_ci    }),
6661847f8eSopenharmony_ci    new PackPlugin()
6761847f8eSopenharmony_ci  ],
6861847f8eSopenharmony_ci};