Commit 55f622751198869410be344573251457f9797e0a

Authored by 田强
1 parent 46286d5c

refactor: build & start

Too many changes to show.

To preserve performance only 7 of 9 files are displayed.

1 #!/usr/bin/env node 1 #!/usr/bin/env node
2 -  
3 -const path = require('path');  
4 -const fs = require('fs');  
5 const execSync = require('child_process').execSync; 2 const execSync = require('child_process').execSync;
6 const chalk = require('chalk'); 3 const chalk = require('chalk');
7 -const getConfigContent = require('../config/defaultConfig.js');  
8 4
9 -const umircCwdFilePath = path.resolve(process.cwd(), '.umirc.js'); 5 +const defaultEnv = ['dev', 'prod']
  6 +const env = process.argv.slice(2).join(' ') || 'prod'
  7 +
  8 +if (!defaultEnv.includes(env)) {
  9 + console.log(chalk.red('\n 命令不存在'));
  10 + process.exit(1);
  11 +}
10 12
11 -async function writeFile() {  
12 - const content = await getConfigContent();  
13 - fs.writeFile(umircCwdFilePath, content, (err) => {  
14 - if (err) {  
15 - console.log(chalk.red(`文件写入失败, ${err}`));  
16 - process.exit(1);  
17 - } else {  
18 - execSync(`npx cross-env ${process.argv.slice(2).join(' ')} umi build`, { stdio: 'inherit' });  
19 - fs.unlinkSync(umircCwdFilePath);  
20 - }  
21 - }); 13 +if (env === 'prod') {
  14 + execSync(`npx cross-env NODE_ENV=production webpack build --config ./config/webpack.prod.js`, { stdio: 'inherit' });
22 } 15 }
23 16
24 -writeFile(); 17 +if (env === 'dev') {
  18 + execSync(`npx cross-env NODE_ENV=development webpack build --config ./config/webpack.dev.js`, { stdio: 'inherit' });
  19 +}
1 #!/usr/bin/env node 1 #!/usr/bin/env node
2 2
3 -const path = require('path');  
4 -const fs = require('fs');  
5 const execSync = require('child_process').execSync; 3 const execSync = require('child_process').execSync;
6 -const chalk = require('chalk');  
7 -const getConfigContent = require('../config/defaultConfig.js');  
8 -const { findUsablePort } = require('../utils');  
9 4
10 -const umircCwdFilePath = path.resolve(process.cwd(), '.umirc.js');  
11 -  
12 -async function writeFile() {  
13 - const port = await findUsablePort(process.env.PORT || 8000);  
14 - const content = getConfigContent(port);  
15 -  
16 - fs.writeFile(umircCwdFilePath, content, (err) => {  
17 - if (err) {  
18 - console.log(chalk.red(`文件写入失败, ${err}`));  
19 - process.exit(1);  
20 - } else {  
21 - execSync(  
22 - `npx cross-env ${process.argv  
23 - .slice(2)  
24 - .join(' ')} SOCKET_SERVER=http://localhost:${port} umi dev`,  
25 - { stdio: 'inherit' },  
26 - );  
27 - fs.unlinkSync(umircCwdFilePath);  
28 - }  
29 - });  
30 -}  
31 -  
32 -writeFile(); 5 +execSync(`npx cross-env NODE_ENV=development vite --force --config ./config/vite.config.js`, { stdio: 'inherit' });
1 -const getConfigContent = (port) => {  
2 - return `  
3 - import { defineConfig } from 'umi';  
4 - import path from 'path';  
5 - import fs from 'fs';  
6 - import MiniCssExtractPlugin from 'mini-css-extract-plugin'  
7 - import routes from './src/routes'  
8 - import pkg from './package.json'  
9 - let userConfig = {};  
10 - let terserOptions = {};  
11 - let publicPath = '/' + pkg.name + '/'  
12 -  
13 - if (fs.existsSync(path.resolve(process.cwd(), '.qx.config.js'))) {  
14 - userConfig = require(path.resolve(process.cwd(), '.qx.config.js'))  
15 - }  
16 -  
17 - if (process.env.NODE_ENV === 'development') {  
18 - publicPath = '//localhost:' + ${port} + '/'  
19 - }  
20 -  
21 - if (process.env.UMI_APP_ENV === 'prod') {  
22 - terserOptions = {  
23 - compress: {  
24 - pure_funcs: ['console.log', 'debugger'],  
25 - },  
26 - };  
27 - } else {  
28 - terserOptions = {  
29 - compress: {  
30 - drop_debugger: true,  
31 - },  
32 - };  
33 - }  
34 -  
35 - export default defineConfig({  
36 - esbuild: {},  
37 - devServer: {  
38 - port: ${port},  
39 - 'Access-Control-Allow-Origin': true  
40 - },  
41 - runtimePublicPath: true,  
42 - publicPath,  
43 - mountElementId: pkg.name,  
44 - webpack5: {  
45 - lazyCompilation: {},  
46 - },  
47 - nodeModulesTransform: {  
48 - type: 'none',  
49 - },  
50 - history: {  
51 - type: 'hash',  
52 - },  
53 - alias: {  
54 - '@/src': 'src',  
55 - },  
56 - hash: true,  
57 - title: false,  
58 - lessLoader: {  
59 - globalVars: {  
60 - theme: 'true;@import "~@/variable.less"',  
61 - },  
62 - },  
63 - antd: {  
64 - config: {},  
65 - },  
66 - fastRefresh: {},  
67 - qiankun: {  
68 - slave: {},  
69 - },  
70 - dynamicImport: {  
71 - loading: '@/components/qx-page-loading',  
72 - },  
73 - routes,  
74 - terserOptions,  
75 - chainWebpack: function (memo, { env, webpack, createCSSRule }) {  
76 - memo.resolve.alias.set('@', path.resolve(__dirname, './src'));  
77 - memo.resolve.alias.set('@@', path.resolve(__dirname, './src/.umi'));  
78 - memo.optimization.delete('noEmitOnErrors');  
79 - memo.plugins.delete('optimize-css');  
80 - // if (env === 'production') {  
81 - memo.merge({  
82 - plugins: env === 'production'? [new MiniCssExtractPlugin()] : [],  
83 - optimization: {  
84 - emitOnErrors: true,  
85 - splitChunks: {  
86 - chunks: 'async',  
87 - minSize: 30000,  
88 - minChunks: 3,  
89 - automaticNameDelimiter: '.',  
90 - cacheGroups: {  
91 - antdesigns: {  
92 - name: 'antdesigns',  
93 - chunks: 'all',  
94 - test: /(@antd|antd|@ant-design)/,  
95 - priority: 10  
96 - },  
97 - x6: {  
98 - name: 'x6',  
99 - chunks: 'all',  
100 - test: /(@antv\\/x6|@antv\\/x6-react-components|@antv\\/x6-react-shape)/,  
101 - priority: 20,  
102 - },  
103 - qxwidgets: {  
104 - name: 'qxwidgets',  
105 - chunks: 'all',  
106 - test: /\\/src\\/packages\\/qx-form-generator/,  
107 - priority: 10,  
108 - },  
109 - formrender: {  
110 - name: 'formrender',  
111 - chunks: 'all',  
112 - test: /@qx\\/form-render/,  
113 - priority: 10,  
114 - },  
115 - qx: {  
116 - name: 'qx',  
117 - chunks: 'all',  
118 - test: /(@qx\\/view-render|@qx\\/form-design|@qx\\/hooks|@qx\\/utils|@qx\\/icon-btn)/,  
119 - priority: 20,  
120 - },  
121 - echarts: {  
122 - name: 'echarts',  
123 - chunks: 'all',  
124 - test: /(echarts|echarts-for-react)/,  
125 - priority: 20,  
126 - },  
127 - // styles: {  
128 - // name: "styles",  
129 - // type: "css/mini-extract",  
130 - // chunks: "all",  
131 - // enforce: true,  
132 - // },  
133 - vendors: {  
134 - name: 'vendors',  
135 - chunks: 'all',  
136 - test: /[\\/]node_modules[\\/]/,  
137 - priority: 10,  
138 - },  
139 - },  
140 - },  
141 - },  
142 - externals: function ({ context, request }, callback) {  
143 - if (env === 'production') {  
144 - if (/^react$/.test(request)) {  
145 - return callback(null, 'React', 'react');  
146 - }  
147 -  
148 - if (/^react-dom$/.test(request)) {  
149 - return callback(null, 'ReactDOM', 'react-dom');  
150 - }  
151 - }  
152 -  
153 - if (/antd.*.(css|less)$/.test(path.resolve(context, request))) {  
154 - return callback(null, 'antd-style');  
155 - }  
156 -  
157 - callback();  
158 - },  
159 - });  
160 - // }  
161 - },  
162 - ...userConfig,  
163 - });  
164 - `;  
165 -};  
166 -  
167 -module.exports = getConfigContent;  
  1 +const { defineConfig } = require('vite');
  2 +const process = require('process');
  3 +import { viteExternalsPlugin } from 'vite-plugin-externals';
  4 +const path = require('path');
  5 +const { cwd } = require('process');
  6 +
  7 +module.exports = defineConfig({
  8 + server: {
  9 + proxy: path.resolve(cwd(), './proxy'),
  10 + },
  11 + define: {
  12 + // By default, Vite doesn't include shims for NodeJS/
  13 + // necessary for segment analytics lib to work
  14 + global: {},
  15 + 'process.env': process.env,
  16 + },
  17 + resolve: {
  18 + extensions: ['.json', '.jsx', '.js', '.ts', '.tsx'],
  19 + alias: {
  20 + '@': path.resolve(cwd, './src/'),
  21 + '@/src': path.resolve(cwd, './src/'),
  22 + '~@qx': path.resolve(cwd, './node_modules/@qx'),
  23 + },
  24 + },
  25 + css: {
  26 + preprocessorOptions: {
  27 + less: {
  28 + javascriptEnabled: true,
  29 + additionalData: `@import '@/styles/variable.less';`,
  30 + },
  31 + },
  32 + },
  33 + plugins: [
  34 + new viteExternalsPlugin({
  35 + react: 'React',
  36 + 'react-dom': 'ReactDOM',
  37 + dayjs: 'dayjs',
  38 + antd: 'antd',
  39 + }),
  40 + ],
  41 +});
  1 +const CopyPlugin = require('copy-webpack-plugin');
  2 +const HtmlWebpackPlugin = require('html-webpack-plugin');
  3 +const WebpackBar = require('webpackbar');
  4 +const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5 +const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
  6 +
  7 +const process = require('process');
  8 +const path = require('path');
  9 +
  10 +const cwd = process.cwd();
  11 +
  12 +module.exports = {
  13 + output: {
  14 + filename: './js/[name]-[contenthash].js',
  15 + libraryTarget: 'umd',
  16 + clean: true,
  17 + },
  18 + entry: path.resolve(cwd, './src/index.tsx'),
  19 + module: {
  20 + rules: [
  21 + {
  22 + test: /\.(js|jsx|ts|tsx)$/,
  23 + exclude: /node_modules/,
  24 + use: [
  25 + 'thread-loader',
  26 + {
  27 + loader: 'babel-loader',
  28 + options: {
  29 + presets: [
  30 + '@babel/preset-env',
  31 + '@babel/preset-react',
  32 + '@babel/preset-typescript',
  33 + ],
  34 + },
  35 + },
  36 + ],
  37 + },
  38 + {
  39 + test: /\.(png|jpeg|jpg)(\?[a-z0-9=&.]+)?$/,
  40 + use: {
  41 + loader: 'file-loader',
  42 + options: {
  43 + name: './public/imgs/[name].[ext]',
  44 + },
  45 + },
  46 + },
  47 + {
  48 + test: /\.(ttf|eot|woff(2)?)(\?[a-z0-9=&.]+)?$/,
  49 + use: {
  50 + loader: 'file-loader',
  51 + options: {
  52 + name: './public/fonts/[name].[ext]',
  53 + },
  54 + },
  55 + },
  56 + {
  57 + test: /\.less$/i,
  58 + use: [
  59 + MiniCssExtractPlugin.loader,
  60 + 'css-loader',
  61 + {
  62 + loader: 'less-loader',
  63 + options: {
  64 + additionalData: `@import '@/styles/variable.less';`,
  65 + },
  66 + },
  67 + ],
  68 + },
  69 +
  70 + {
  71 + test: /\.css$/i,
  72 + use: [MiniCssExtractPlugin.loader, 'css-loader'],
  73 + },
  74 + {
  75 + test: /\.svg$/,
  76 + use: ['@svgr/webpack', 'url-loader'],
  77 + },
  78 + ],
  79 + },
  80 + resolve: {
  81 + extensions: ['.json', '.jsx', '.js', '.ts', '.tsx'],
  82 + alias: {
  83 + '@': path.resolve(cwd, './src/'),
  84 + '@/src': path.resolve(cwd, './src/'),
  85 + },
  86 + },
  87 + plugins: [
  88 + new HtmlWebpackPlugin({
  89 + template: path.resolve(cwd, './src/public/index.html'),
  90 + }),
  91 + new CopyPlugin({
  92 + patterns: [
  93 + {
  94 + from: path.resolve(cwd, './static'),
  95 + to: 'static',
  96 + },
  97 + ],
  98 + }),
  99 + new WebpackBar(),
  100 + new NodePolyfillPlugin()
  101 + ],
  102 +};
  1 +const { merge } = require('webpack-merge');
  2 +const baseConfig = require('./webpack.base');
  3 +const path = require('path');
  4 +const { cwd } = require('process');
  5 +
  6 +module.exports = merge(baseConfig, {
  7 + mode: 'development',
  8 + devServer: {
  9 + hot: false,
  10 + headers: {
  11 + 'Access-Control-Allow-Origin': '*',
  12 + },
  13 + proxy: path.resolve(cwd(), './proxy'),
  14 + },
  15 + externals: {},
  16 +});
  1 +const { merge } = require('webpack-merge');
  2 +const baseConfig = require('./webpack.base');
  3 +const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');
  4 +const TerserPlugin = require('terser-webpack-plugin');
  5 +const CompressionWebpackPlugin = require('compression-webpack-plugin');
  6 +const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  7 +
  8 +module.exports = merge(baseConfig, {
  9 + mode: 'production',
  10 + externals: {
  11 + react: 'React',
  12 + 'react-dom': 'ReactDOM',
  13 + 'dayjs': 'dayjs',
  14 + 'antd': 'antd',
  15 + },
  16 + optimization: {
  17 + minimize: true,
  18 + minimizer: [
  19 + new CompressionWebpackPlugin(),
  20 + new TerserPlugin({
  21 + extractComments: false,
  22 + test: /\.js(\?.*)?$/i,
  23 + exclude: /node_modules/,
  24 + }),
  25 + new MiniCssExtractPlugin({
  26 + filename: './styles/[name]-[contenthash].css',
  27 + }),
  28 + new CssMinimizerWebpackPlugin(),
  29 + ],
  30 + },
  31 +});