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