|
| 1 | +// tslint:disable:no-var-requires |
| 2 | +const path = require('path'); |
| 3 | +const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 4 | +const MiniCssExtractPlugin = require('mini-css-extract-plugin'); |
| 5 | +const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); |
| 6 | +const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin'); |
| 7 | +const CopyPlugin = require('copy-webpack-plugin'); |
| 8 | +const webpack = require('webpack'); |
| 9 | +const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin'); |
| 10 | +const merge = require('webpack-merge'); |
| 11 | + |
| 12 | +const utils = require('./utils'); |
| 13 | + |
| 14 | +const tsConfigPath = path.join(__dirname, '../../../tsconfig.json'); |
| 15 | +const port = process.env.IDE_FRONT_PORT || 8080; |
| 16 | + |
| 17 | +console.log('front port', port); |
| 18 | + |
| 19 | +const styleLoader = process.env.NODE_ENV === 'production' |
| 20 | + ? MiniCssExtractPlugin.loader |
| 21 | + : require.resolve('style-loader'); |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {{ webpackConfig: any, tsconfigPath?: string, env: string, outputPath: string, template: string }} config |
| 25 | + */ |
| 26 | +exports.createWebpackConfig = function(config) { |
| 27 | + const absOutputPath = path.resolve(config.outputPath || 'dist'); |
| 28 | + |
| 29 | + const baseConfig = { |
| 30 | + mode: config.env || 'development', |
| 31 | + devtool: 'inline-cheap-source-map', |
| 32 | + output: { |
| 33 | + path: absOutputPath, |
| 34 | + filename: '[name].js', |
| 35 | + chunkFilename: '[name].js', |
| 36 | + }, |
| 37 | + resolve: { |
| 38 | + extensions: ['.ts', '.tsx', '.js', '.json', '.less'], |
| 39 | + plugins: [ |
| 40 | + new TsconfigPathsPlugin({ |
| 41 | + ...(tsConfigPath ? { configFile: tsConfigPath } : {}) |
| 42 | + }) |
| 43 | + ] |
| 44 | + }, |
| 45 | + node: { |
| 46 | + net: "empty", |
| 47 | + child_process: "empty", |
| 48 | + path: "empty", |
| 49 | + url: false, |
| 50 | + fs: "empty", |
| 51 | + process: "mock" |
| 52 | + }, |
| 53 | + optimization: { |
| 54 | + nodeEnv: config.env, |
| 55 | + }, |
| 56 | + module: { |
| 57 | + // https://github.com/webpack/webpack/issues/196#issuecomment-397606728 |
| 58 | + exprContextCritical: false, |
| 59 | + rules: [ |
| 60 | + { |
| 61 | + test: /\.(js|mjs|jsx|ts|tsx)$/, |
| 62 | + use: [ |
| 63 | + { |
| 64 | + loader: 'babel-loader', |
| 65 | + options: { |
| 66 | + babelrc: false, |
| 67 | + cacheDirectory: process.env.BABEL_CACHE !== 'none' |
| 68 | + ? path.resolve('.cache/babel-loader') |
| 69 | + : false, |
| 70 | + presets: [ |
| 71 | + [require('@babel/preset-env').default, { |
| 72 | + exclude: [ |
| 73 | + 'transform-typeof-symbol', |
| 74 | + 'transform-unicode-regex', |
| 75 | + 'transform-sticky-regex', |
| 76 | + 'transform-new-target', |
| 77 | + 'transform-modules-umd', |
| 78 | + 'transform-modules-systemjs', |
| 79 | + 'transform-modules-amd', |
| 80 | + 'transform-literals', |
| 81 | + ] |
| 82 | + }], |
| 83 | + [require('@babel/preset-react').default], |
| 84 | + [require('@babel/preset-typescript'), { |
| 85 | + allowNamespaces: true, |
| 86 | + }] |
| 87 | + ], |
| 88 | + plugins: [ |
| 89 | + require('@babel/plugin-syntax-top-level-await').default, |
| 90 | + [ |
| 91 | + require('@babel/plugin-transform-destructuring').default, |
| 92 | + { loose: false }, |
| 93 | + ], |
| 94 | + [ |
| 95 | + require('@babel/plugin-proposal-decorators').default, |
| 96 | + { legacy: true } |
| 97 | + ], |
| 98 | + [ |
| 99 | + require('@babel/plugin-proposal-class-properties').default, |
| 100 | + { loose: true }, |
| 101 | + ], |
| 102 | + require('@babel/plugin-proposal-export-default-from').default, |
| 103 | + [ |
| 104 | + require('@babel/plugin-proposal-pipeline-operator').default, |
| 105 | + { |
| 106 | + proposal: 'minimal', |
| 107 | + }, |
| 108 | + ], |
| 109 | + require('@babel/plugin-proposal-do-expressions').default, |
| 110 | + require('@babel/plugin-proposal-function-bind').default, |
| 111 | + require('@babel/plugin-proposal-logical-assignment-operators').default, |
| 112 | + ] |
| 113 | + } |
| 114 | + } |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + test: /\.css$/, |
| 119 | + use: [styleLoader, 'css-loader'], |
| 120 | + }, |
| 121 | + { |
| 122 | + test: /\.module.less$/, |
| 123 | + use: [ |
| 124 | + styleLoader, |
| 125 | + { |
| 126 | + loader: 'css-loader', |
| 127 | + options: { |
| 128 | + importLoaders: 1, |
| 129 | + sourceMap: true, |
| 130 | + modules: true, |
| 131 | + localIdentName: '[local]___[hash:base64:5]' |
| 132 | + } |
| 133 | + }, |
| 134 | + { |
| 135 | + loader: 'less-loader', |
| 136 | + options: { |
| 137 | + lessOptions: { |
| 138 | + javascriptEnabled: true, |
| 139 | + }, |
| 140 | + } |
| 141 | + } |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + test: /^((?!\.module).)*less$/, |
| 146 | + use: [ |
| 147 | + styleLoader, |
| 148 | + { |
| 149 | + loader: 'css-loader', |
| 150 | + options: { |
| 151 | + importLoaders: 1, |
| 152 | + } |
| 153 | + }, |
| 154 | + { |
| 155 | + loader: 'less-loader', |
| 156 | + options: { |
| 157 | + lessOptions: { |
| 158 | + javascriptEnabled: true, |
| 159 | + }, |
| 160 | + } |
| 161 | + } |
| 162 | + ], |
| 163 | + }, |
| 164 | + { |
| 165 | + test: /\.(png|jpe?g|gif|webp|ico)(\?.*)?$/, |
| 166 | + use: 'url-loader', |
| 167 | + options: { |
| 168 | + limit: config.inlineLimit || 10000, |
| 169 | + name: '[name].[ext]', |
| 170 | + // require 图片的时候不用加 .default |
| 171 | + esModule: false, |
| 172 | + fallback: { |
| 173 | + loader: 'file-loader', |
| 174 | + options: { |
| 175 | + name: '[name].[ext]', |
| 176 | + esModule: false, |
| 177 | + }, |
| 178 | + } |
| 179 | + } |
| 180 | + }, |
| 181 | + { |
| 182 | + test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/, |
| 183 | + use: [{ |
| 184 | + loader: 'file-loader', |
| 185 | + options: { |
| 186 | + name: '[name].[ext]', |
| 187 | + esModule: false, |
| 188 | + } |
| 189 | + }] |
| 190 | + }, |
| 191 | + { |
| 192 | + test: /\.(txt|text|md)$/, |
| 193 | + use: 'raw-loader' |
| 194 | + } |
| 195 | + ], |
| 196 | + }, |
| 197 | + plugins: [ |
| 198 | + new HtmlWebpackPlugin({ |
| 199 | + template: config.template, |
| 200 | + }), |
| 201 | + new MiniCssExtractPlugin({ |
| 202 | + filename: '[name].[chunkhash:8].css', |
| 203 | + chunkFilename: '[id].css', |
| 204 | + }), |
| 205 | + new webpack.DefinePlugin(config.define || {}), |
| 206 | + new FriendlyErrorsWebpackPlugin({ |
| 207 | + compilationSuccessInfo: { |
| 208 | + messages: [`Your application is running here: http://localhost:${port}`], |
| 209 | + }, |
| 210 | + onErrors: utils.createNotifierCallback(), |
| 211 | + clearConsole: true, |
| 212 | + }), |
| 213 | + new CopyPlugin(...( |
| 214 | + config.copy |
| 215 | + ? config.copy.map((from) => ({ |
| 216 | + from: path.resolve(from), |
| 217 | + to: absOutputPath, |
| 218 | + })) |
| 219 | + : [] |
| 220 | + )), |
| 221 | + new ForkTsCheckerWebpackPlugin({ |
| 222 | + checkSyntacticErrors: true, |
| 223 | + tsconfig: tsConfigPath, |
| 224 | + reportFiles: ['packages/**/*.{ts,tsx}'] |
| 225 | + }), |
| 226 | + ], |
| 227 | + } |
| 228 | + |
| 229 | + return merge(baseConfig, config.webpackConfig) |
| 230 | +} |
0 commit comments