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 TerserPlugin = require('terser-webpack-plugin');
19const webpack = require('webpack');
20const packageInfo = require('./package.json');
21const archiver = require('archiver');
22const { copyESLibs } = require('./scripts/copylibs');
23
24class PackPlugin {
25  apply(compiler) {
26    compiler.hooks.done.tap('PackPlugin', (stats) => {
27      const bundleName = `${packageInfo.name}.js`;
28      const bundlejsPath = path.resolve(__dirname, 'package', 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, 'README_zh.md');
36      const outputName = path.resolve(__dirname, 'package', `${packageInfo.name}-${packageInfo.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 = (env, argv) => {
49  const config = {
50    name: 'JSDoc formatter plugin',
51    target: 'node',
52    mode: 'none',
53    entry: './src/main.ts',
54    module: {
55      rules: [
56        {
57          test: /\.ts$/,
58          include: path.resolve(__dirname, 'src'),
59          exclude: [/node_modules/, /test/],
60          loader: 'ts-loader',
61          options: {
62            onlyCompileBundledFiles: true,
63          },
64        },
65        {
66          test: /build\.json$/,
67          use: [
68            {
69              loader: path.resolve(__dirname, 'loader/flavor.js'),
70            },
71          ],
72        },
73      ],
74    },
75    resolve: {
76      extensions: ['.js', '.ts', '.json'],
77    },
78    output: {
79      filename: `${packageInfo.name}.js`,
80      path: path.resolve(__dirname, './package'),
81    },
82    optimization: {
83      minimize: true,
84      minimizer: [new TerserPlugin({ extractComments: false })],
85    },
86    plugins: [
87      new webpack.BannerPlugin({
88        banner: `version:${packageInfo.version}`,
89        raw: false,
90        entryOnly: true,
91      }),
92      new PackPlugin(),
93    ],
94  };
95  return config;
96};
97