• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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// Generated using webpack-cli https://github.com/webpack/webpack-cli
16
17const path = require('path');
18const { CleanWebpackPlugin } = require('clean-webpack-plugin');
19const HtmlWebpackPlugin = require('html-webpack-plugin');
20const MiniCssExtractPlugin = require('mini-css-extract-plugin');
21const isProduction = process.env.NODE_ENV === 'production';
22const CopyWebpackPlugin = require('copy-webpack-plugin');
23const os = require('os');
24const childProcess = require('child_process');
25const { exec } = require('child_process');
26const fs = require('fs');
27
28const supportPlatform = ['windows', 'linux', 'darwin'];
29
30function runCommand(command) {
31  return new Promise((resolve, reject) => {
32    exec(command, (error, stdout, stderr) => {
33      if (error) {
34        reject(error);
35      } else {
36        resolve(stdout);
37      }
38    });
39  });
40}
41
42function cpFile(sourcePath, targetPath) {
43  fs.readdir(sourcePath, (err, files) => {
44    if (err) {
45      console.error('无法读取目录', err);
46      return;
47    }
48    files.forEach((file) => {
49      const source = `${sourcePath}/${file}`;
50      const target = `${targetPath}/${file}`;
51      if (fs.lstatSync(source).isFile()) {
52        fs.copyFile(source, target, (err) => {
53          if (err) {
54            console.error('无法复制文件', err);
55            return;
56          }
57        });
58      }
59    });
60  });
61}
62
63function clearDirectory(directoryPath) {
64  const isDirectoryExists = fs.existsSync(directoryPath);
65
66  if (!isDirectoryExists) {
67    fs.mkdirSync(directoryPath);
68  } else {
69    fs.readdirSync(directoryPath).forEach((file) => {
70      const filePath = path.join(directoryPath, file);
71      if (fs.lstatSync(filePath).isDirectory()) {
72        return;
73      } else {
74        try {
75          fs.unlinkSync(filePath); // 删除文件
76        } catch {
77          console.log(`can't del file ${filePath}`);
78        }
79      }
80    });
81  }
82}
83
84function buildMultiPlatform() {
85  const outPath = path.normalize(path.join(__dirname, '/', 'dist'));
86  const serverSrc = path.normalize(path.join(__dirname, '/server/main.go'));
87  for (const platform of supportPlatform) {
88    const generateFile = platform === 'windows' ?
89        path.normalize(path.join(outPath, '/', `main.exe`)) :
90        path.normalize(path.join(outPath, '/', `main_${platform}`));
91    const setEnv = `go env -w CGO_ENABLED=0 && go env -w GOOS=${platform} && go env -w GOARCH=amd64`;
92    const buildCmd = `${setEnv} && go build -o ${generateFile} ${serverSrc}`;
93    console.log(`compile ${platform} server ...`);
94    childProcess.execSync(buildCmd);
95  }
96}
97
98const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : 'style-loader';
99//compile server
100((flag) => {
101  if (!flag) {
102    return;
103  }
104  console.log('start compile server');
105  let outPath = path.normalize(path.join(__dirname, '/', 'dist'));
106  let binPath = path.normalize(path.join(__dirname, '/', 'bin'));
107  clearDirectory(outPath);
108  cpFile(binPath, outPath);
109  const protoPath = './src/trace/proto/';
110  runCommand(`pbjs -t static-module -w commonjs -o ${protoPath}SphBaseData.js  ${protoPath}SphBaseData.proto`);
111  buildMultiPlatform();
112})(true);
113const config = {
114  entry: './src/index.ts',
115  output: {
116    path: path.resolve(__dirname, 'dist'),
117    filename: '[name].[chunkhash].bundle.js',
118    clean: false,
119  },
120  devServer: {
121    open: true,
122    host: 'localhost',
123  },
124  plugins: [
125    new HtmlWebpackPlugin({
126      template: 'index.html',
127    }),
128    new CleanWebpackPlugin({
129      verbose: true,
130      cleanOnceBeforeBuildPatterns: ['!main'],
131    }),
132    new CopyWebpackPlugin({
133      patterns: [
134        {
135          from: './src/figures',
136          to: 'figures',
137        },
138        {
139          from: './src/img',
140          to: 'img',
141        },
142        {
143          from: './src/doc',
144          to: 'doc',
145        },
146        {
147          from: './src/base-ui/icon.svg',
148          to: 'base-ui/icon.svg',
149        },
150        {
151          from: './src/trace/config/custom_temp_config.json',
152          to: 'trace/config/custom_temp_config.json',
153        },
154        {
155          from: './third-party/sql-wasm.js',
156          to: 'sql-wasm.js',
157        },
158        {
159          from: './third-party/sql-wasm.wasm',
160          to: 'sql-wasm.wasm',
161        },
162        {
163          from: './server/version.txt',
164          to: 'version.txt',
165        },
166        {
167          from: './server/wasm.json',
168          to: 'wasm.json',
169        },
170        {
171          from: './server/server-config.txt',
172          to: 'server-config.txt',
173        },
174      ],
175    }),
176    // Add your plugins here
177    // Learn more about plugins from https://webpack.js.org/configuration/plugins/
178  ],
179  module: {
180    rules: [
181      {
182        test: /\.(ts|tsx)$/i,
183        loader: 'ts-loader',
184        exclude: ['/node_modules/'],
185      },
186      {
187        test: /\.css$/i,
188        use: [stylesHandler, 'css-loader', 'postcss-loader'],
189      },
190      {
191        test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
192        type: 'asset',
193      },
194
195      // Add your rules for custom modules here
196      // Learn more about loaders from https://webpack.js.org/loaders/
197    ],
198  },
199  resolve: {
200    extensions: ['.tsx', '.ts', '.jsx', '.js', '...'],
201    fallback: {
202      fs: false,
203      tls: false,
204      net: false,
205      zlib: false,
206      http: false,
207      https: false,
208      stream: false,
209      crypto: false,
210      path: false, //if you want to use this module also don't forget npm i crypto-browserify
211    },
212  },
213};
214
215module.exports = () => {
216  if (isProduction) {
217    config.mode = 'production';
218
219    config.plugins.push(new MiniCssExtractPlugin());
220  } else {
221    config.mode = 'development';
222  }
223  return config;
224};
225