|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs-extra'); |
| 3 | +const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 4 | +const webpack = require('webpack'); |
| 5 | +const { merge } = require('webpack-merge'); |
| 6 | +const VueLoaderPlugin = require('vue-loader/lib/plugin'); |
| 7 | +const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin'); |
| 8 | +const MiniCssExtractPlugin = require('mini-css-extract-plugin'); |
| 9 | + |
| 10 | +const { resolve } = path; |
| 11 | +const cwd = process.cwd(); |
| 12 | + |
| 13 | +const BASIC_NMS = [ |
| 14 | + resolve(__dirname, '../node_modules'), |
| 15 | + resolve(cwd, './node_modules'), |
| 16 | + resolve(cwd, '../../node_modules') |
| 17 | +]; |
| 18 | +const NMS = [ |
| 19 | + ...BASIC_NMS, |
| 20 | + // 增加编译速度 |
| 21 | + ...module.paths |
| 22 | +]; |
| 23 | + |
| 24 | +const resolvePackage = (source) => { |
| 25 | + let $path = NMS.find(i => fs.pathExistsSync(resolve(i, source))); |
| 26 | + |
| 27 | + if (!$path) { |
| 28 | + throw new Error(`@wya/doc: 未找到${source}`); |
| 29 | + } |
| 30 | + |
| 31 | + return resolve($path, source); |
| 32 | +}; |
| 33 | +const resolveClient = (source) => { |
| 34 | + return resolve(__dirname, '../client', source || ''); |
| 35 | +}; |
| 36 | + |
| 37 | +class Config { |
| 38 | + constructor(type, parent) { |
| 39 | + |
| 40 | + this.$parent = parent; |
| 41 | + |
| 42 | + this.result = type === 'webpack' || type === true |
| 43 | + ? this.generateDefault() |
| 44 | + : this.generateServer(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * 针对@wya/vc有做特殊处理 |
| 49 | + */ |
| 50 | + generateDefault() { |
| 51 | + const { webpackAssist, tempDir } = this.$parent.$parent; |
| 52 | + const { port, host } = this.$parent; |
| 53 | + const { entry, htmls, openPage } = webpackAssist; |
| 54 | + |
| 55 | + const exclude = new RegExp(`(${BASIC_NMS.join('|')})`); |
| 56 | + |
| 57 | + let origin = `http://${host}:${port}`; |
| 58 | + let messages = [`Dev Server: ${origin}`]; |
| 59 | + if (Object.keys(openPage).length > 0) { |
| 60 | + for (let item in openPage) { |
| 61 | + messages.push(`${item}: ${origin}${openPage[item]}`); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const defaultOptions = { |
| 66 | + mode: 'development', |
| 67 | + devtool: 'cheap-module-eval-source-map', |
| 68 | + entry, |
| 69 | + output: { |
| 70 | + path: resolve(__dirname, '../dist'), |
| 71 | + filename: `js/[name].bundle.js`, |
| 72 | + chunkFilename: `js/[name].chunk.js`, |
| 73 | + sourceMapFilename: `js/[name].bundle.map`, |
| 74 | + publicPath: '/', |
| 75 | + }, |
| 76 | + resolve: { |
| 77 | + modules: NMS, |
| 78 | + extensions: ['.vue', '.js', '.json', '.md', '.css', '.scss'], |
| 79 | + symlinks: true, |
| 80 | + alias: { |
| 81 | + 'vue$': resolvePackage('vue/dist/vue.esm.js'), |
| 82 | + 'babel-runtime': resolvePackage('@babel/runtime') |
| 83 | + } |
| 84 | + }, |
| 85 | + module: { |
| 86 | + rules: [ |
| 87 | + { |
| 88 | + test: /\.js$/, |
| 89 | + exclude, |
| 90 | + use: { |
| 91 | + loader: resolvePackage('babel-loader'), |
| 92 | + options: { |
| 93 | + babelrc: false, |
| 94 | + configFile: false, |
| 95 | + compact: false, |
| 96 | + cacheDirectory: true, |
| 97 | + presets: [ |
| 98 | + resolvePackage('@babel/preset-env') |
| 99 | + ], |
| 100 | + plugins: [ |
| 101 | + resolvePackage('@babel/plugin-proposal-export-namespace-from'), |
| 102 | + resolvePackage('@babel/plugin-proposal-export-default-from'), |
| 103 | + resolvePackage('@babel/plugin-proposal-function-bind'), |
| 104 | + resolvePackage('@babel/plugin-syntax-dynamic-import'), |
| 105 | + resolvePackage('@babel/plugin-transform-modules-commonjs'), |
| 106 | + resolvePackage('@babel/plugin-syntax-jsx'), |
| 107 | + resolvePackage("@babel/plugin-transform-runtime"), |
| 108 | + resolvePackage('babel-plugin-transform-vue-jsx'), |
| 109 | + [ |
| 110 | + resolvePackage('@babel/plugin-proposal-decorators'), |
| 111 | + { |
| 112 | + "legacy": true |
| 113 | + } |
| 114 | + ], |
| 115 | + [ |
| 116 | + resolvePackage('@babel/plugin-proposal-class-properties'), |
| 117 | + { |
| 118 | + "loose": true |
| 119 | + } |
| 120 | + ] |
| 121 | + ] |
| 122 | + } |
| 123 | + } |
| 124 | + }, |
| 125 | + { |
| 126 | + test: /\.vue$/, |
| 127 | + exclude, |
| 128 | + use: [ |
| 129 | + { |
| 130 | + loader: resolvePackage('vue-loader'), |
| 131 | + }, |
| 132 | + { |
| 133 | + loader: resolvePackage('@wya/vc-loader'), |
| 134 | + } |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + test: /\.(scss|css)$/, |
| 139 | + use: [ |
| 140 | + resolvePackage('vue-style-loader'), |
| 141 | + resolvePackage('css-loader'), |
| 142 | + resolvePackage('sass-loader'), |
| 143 | + { |
| 144 | + loader: resolvePackage('sass-resources-loader'), |
| 145 | + options: { |
| 146 | + resources: [ |
| 147 | + resolvePackage("@wya/sass/lib/mixins/bem.scss") |
| 148 | + ] |
| 149 | + } |
| 150 | + } |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + test: /\.(png|jpg|gif|eot|ttf|woff|woff2|svg)$/, |
| 155 | + loader: resolvePackage('url-loader') |
| 156 | + } |
| 157 | + ] |
| 158 | + }, |
| 159 | + plugins: [ |
| 160 | + new VueLoaderPlugin(), |
| 161 | + new FriendlyErrorsPlugin({ |
| 162 | + compilationSuccessInfo: { |
| 163 | + messages, |
| 164 | + notes: [`Success!`] |
| 165 | + } |
| 166 | + }), |
| 167 | + ...htmls |
| 168 | + ] |
| 169 | + }; |
| 170 | + |
| 171 | + return merge(defaultOptions); |
| 172 | + } |
| 173 | + |
| 174 | + generateServer() { |
| 175 | + const { port, host } = this.$parent; |
| 176 | + |
| 177 | + return merge( |
| 178 | + { |
| 179 | + hot: true, |
| 180 | + quiet: true, |
| 181 | + historyApiFallback: true, |
| 182 | + publicPath: '/', |
| 183 | + port, |
| 184 | + host |
| 185 | + } |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | +} |
| 190 | +module.exports = { |
| 191 | + get(type = 'webpack', parent) { |
| 192 | + return new Config(type, parent).result; |
| 193 | + } |
| 194 | +}; |
0 commit comments