• 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        }
78      }
79    });
80  }
81}
82
83function buildMultiPlatform() {
84  const outPath = path.normalize(path.join(__dirname, '/', 'dist'));
85  const serverSrc = path.normalize(path.join(__dirname, '/server/main.go'));
86  for (const platform of supportPlatform) {
87    const generateFile = platform === 'windows' ?
88      path.normalize(path.join(outPath, '/', `main.exe`)) :
89      path.normalize(path.join(outPath, '/', `main_${platform}`));
90    setEnv = '';
91    if (os.type() === 'Windows_NT') {
92      setEnv += `SET CGO_ENABLED=0&&SET GOOS=${platform}&&SET GOARCH=amd64&&`;
93    } else {
94      setEnv += `CGO_ENABLED=0 GOOS=${platform} GOARCH=amd64`;
95    }
96    const buildCmd = `${setEnv} go build -o ${generateFile} ${serverSrc}`;
97    console.log(`compile ${platform} server ...`);
98    childProcess.execSync(buildCmd);
99  }
100}
101
102const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : 'style-loader';
103//compile server
104((flag) => {
105  if (!flag) {
106    return;
107  }
108  console.log('start compile server');
109  let outPath = path.normalize(path.join(__dirname, '/', 'dist'));
110  let binPath = path.normalize(path.join(__dirname, '/', 'bin'));
111  clearDirectory(outPath);
112  cpFile(binPath, outPath);
113  const protoPath = './src/trace/proto/';
114  runCommand(`pbjs -t static-module -w commonjs -o ${protoPath}SphBaseData.js  ${protoPath}SphBaseData.proto`);
115  buildMultiPlatform();
116})(true);
117const config = {
118  entry: './src/index.ts',
119  output: {
120    path: path.resolve(__dirname, 'dist'),
121    filename: '[name].[chunkhash].bundle.js',
122    clean: false,
123  },
124  devServer: {
125    open: true,
126    host: 'localhost',
127  },
128  plugins: [
129    new HtmlWebpackPlugin({
130      template: 'index.html',
131    }),
132    new CleanWebpackPlugin({
133      verbose: true,
134      cleanOnceBeforeBuildPatterns: ['!main'],
135    }),
136    new CopyWebpackPlugin({
137      patterns: [
138        {
139          from: './src/figures',
140          to: 'figures',
141        },
142        {
143          from: './src/img',
144          to: 'img',
145        },
146        {
147          from: './src/doc',
148          to: 'doc',
149        },
150        {
151          from: './src/config',
152          to: 'config',
153        },
154        {
155          from: './src/base-ui/icon.svg',
156          to: 'base-ui/icon.svg',
157        },
158        {
159          from: './src/trace/config/custom_temp_config.json',
160          to: 'trace/config/custom_temp_config.json',
161        },
162        {
163          from: './third-party/sql-wasm.js',
164          to: 'sql-wasm.js',
165        },
166        {
167          from: './third-party/sql-wasm.wasm',
168          to: 'sql-wasm.wasm',
169        },
170        {
171          from: './server/version.txt',
172          to: 'version.txt',
173        },
174        {
175          from: './server/wasm.json',
176          to: 'wasm.json',
177        },
178        {
179          from: './server/server-config.json',
180          to: 'server-config.json',
181        },
182      ],
183    }),
184    // Add your plugins here
185    // Learn more about plugins from https://webpack.js.org/configuration/plugins/
186  ],
187  module: {
188    rules: [
189      {
190        test: /\.(ts|tsx)$/i,
191        loader: 'ts-loader',
192        exclude: ['/node_modules/'],
193      },
194      {
195        test: /\.css$/i,
196        use: [stylesHandler, 'css-loader', 'postcss-loader'],
197      },
198      {
199        test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
200        type: 'asset',
201      },
202
203      // Add your rules for custom modules here
204      // Learn more about loaders from https://webpack.js.org/loaders/
205    ],
206  },
207  resolve: {
208    extensions: ['.tsx', '.ts', '.jsx', '.js', '...'],
209    fallback: {
210      fs: false,
211      tls: false,
212      net: false,
213      zlib: false,
214      http: false,
215      https: false,
216      stream: false,
217      crypto: false,
218      path: false, //if you want to use this module also don't forget npm i crypto-browserify
219    },
220  },
221};
222
223module.exports = () => {
224  if (isProduction) {
225    config.mode = 'production';
226
227    config.plugins.push(new MiniCssExtractPlugin());
228  } else {
229    config.mode = 'development';
230  }
231  return config;
232};
233