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/base-ui/icon.svg', 152 to: 'base-ui/icon.svg', 153 }, 154 { 155 from: './src/trace/config/custom_temp_config.json', 156 to: 'trace/config/custom_temp_config.json', 157 }, 158 { 159 from: './third-party/sql-wasm.js', 160 to: 'sql-wasm.js', 161 }, 162 { 163 from: './third-party/sql-wasm.wasm', 164 to: 'sql-wasm.wasm', 165 }, 166 { 167 from: './server/version.txt', 168 to: 'version.txt', 169 }, 170 { 171 from: './server/wasm.json', 172 to: 'wasm.json', 173 }, 174 { 175 from: './server/server-config.json', 176 to: 'server-config.json', 177 }, 178 ], 179 }), 180 // Add your plugins here 181 // Learn more about plugins from https://webpack.js.org/configuration/plugins/ 182 ], 183 module: { 184 rules: [ 185 { 186 test: /\.(ts|tsx)$/i, 187 loader: 'ts-loader', 188 exclude: ['/node_modules/'], 189 }, 190 { 191 test: /\.css$/i, 192 use: [stylesHandler, 'css-loader', 'postcss-loader'], 193 }, 194 { 195 test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i, 196 type: 'asset', 197 }, 198 199 // Add your rules for custom modules here 200 // Learn more about loaders from https://webpack.js.org/loaders/ 201 ], 202 }, 203 resolve: { 204 extensions: ['.tsx', '.ts', '.jsx', '.js', '...'], 205 fallback: { 206 fs: false, 207 tls: false, 208 net: false, 209 zlib: false, 210 http: false, 211 https: false, 212 stream: false, 213 crypto: false, 214 path: false, //if you want to use this module also don't forget npm i crypto-browserify 215 }, 216 }, 217}; 218 219module.exports = () => { 220 if (isProduction) { 221 config.mode = 'production'; 222 223 config.plugins.push(new MiniCssExtractPlugin()); 224 } else { 225 config.mode = 'development'; 226 } 227 return config; 228}; 229