|
| 1 | +const path = require('path') |
| 2 | +const chalk = require('chalk') |
| 3 | +const webpack = require('webpack') |
| 4 | +const { cosmiconfig } = require('cosmiconfig') |
| 5 | +const { CleanWebpackPlugin } = require('clean-webpack-plugin') |
| 6 | +const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin') |
| 7 | +const splitRe = /,/ |
| 8 | +const CONFIG_NAME = 'Views.json' |
| 9 | +const CUSTOM_CONFIG_NAME = 'multiple-page' |
| 10 | + |
| 11 | +function getEntryConfig (config, key) { |
| 12 | + const entryKeys = Object.keys(config) |
| 13 | + const allEntryConfig = entryKeys.map(entryKey => { |
| 14 | + const { entry, output, description, chunkName, extract } = config[entryKey] || {} |
| 15 | + |
| 16 | + return { |
| 17 | + key: entryKey, |
| 18 | + extract, |
| 19 | + entry, |
| 20 | + output, |
| 21 | + description, |
| 22 | + chunkName |
| 23 | + } |
| 24 | + }) |
| 25 | + |
| 26 | + // 判断用户传入的 key 是否有效 |
| 27 | + if (splitRe.test(key)) { |
| 28 | + return allEntryConfig.filter(ety => key.split(splitRe).indexOf(ety.key) > -1) |
| 29 | + } else { |
| 30 | + if (entryKeys.indexOf(key) > -1) { |
| 31 | + return allEntryConfig.filter(ety => ety.key === key) |
| 32 | + } else if (key === 'all') { |
| 33 | + return allEntryConfig |
| 34 | + } else { |
| 35 | + return [] |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function normalizedConfig (entryFileJsonPath) { |
| 41 | + const entryConfig = require(entryFileJsonPath) |
| 42 | + const { entry: entries, basePath, destBasePath } = entryConfig |
| 43 | + const entryKeys = Object.keys(entries) |
| 44 | + |
| 45 | + return entryKeys.reduce((obj, key) => { |
| 46 | + const currentEntry = entries[key] |
| 47 | + // 设置当前页面对应 webpack 配置的 entry |
| 48 | + const currentEntryFilePath = path.join(basePath, currentEntry.srcPath, currentEntry.name + '.js') |
| 49 | + const entry = path.resolve(process.cwd(), currentEntryFilePath) |
| 50 | + const outputFilePath = path.join(destBasePath, currentEntry.srcPath) |
| 51 | + const output = path.resolve(process.cwd(), outputFilePath) |
| 52 | + |
| 53 | + obj[key] = { |
| 54 | + entry, |
| 55 | + output, |
| 56 | + extract: currentEntry.extract, |
| 57 | + description: currentEntry.description, |
| 58 | + chunkName: currentEntry.name |
| 59 | + } |
| 60 | + |
| 61 | + return obj |
| 62 | + }, {}) |
| 63 | +} |
| 64 | + |
| 65 | +async function getEntryFiles (key) { |
| 66 | + const explorer = cosmiconfig(CUSTOM_CONFIG_NAME) |
| 67 | + const entryFileJsonPath = process.env.VIEW_JSON || path.resolve(process.cwd(), CONFIG_NAME) |
| 68 | + const result = await explorer.search() |
| 69 | + let entryConfigs = [] |
| 70 | + |
| 71 | + // 命令行没有传入 key 时直接退出操作 |
| 72 | + if (!key || !key.toString().trim()) { |
| 73 | + console.log(chalk.red('输入的key为空')) |
| 74 | + process.exit(1) |
| 75 | + } |
| 76 | + |
| 77 | + if (result && !result.isEmpty && result.config) { |
| 78 | + entryConfigs = getEntryConfig(result.config, key) |
| 79 | + } else if (entryFileJsonPath) { |
| 80 | + const config = normalizedConfig(entryFileJsonPath, key) |
| 81 | + entryConfigs = getEntryConfig(config, key) |
| 82 | + } else { |
| 83 | + console.log(chalk.red(`找不到配置文件, 请指定 \`${CUSTOM_CONFIG_NAME}.config.js\` 或 \`View.json\` 文件进行配置`)) |
| 84 | + process.exit(1) |
| 85 | + } |
| 86 | + |
| 87 | + return entryConfigs |
| 88 | +} |
| 89 | + |
| 90 | +// [{ entry: '', output: '', description: '', chunkName: '' }, { }] |
| 91 | +async function resolveEntryConfig (mode, key, { immediate = true, api, options }) { |
| 92 | + const entryConfigs = await getEntryFiles(key) |
| 93 | + const entryWebpackConfigs = [] |
| 94 | + const entryDescriptions = new Set() |
| 95 | + |
| 96 | + if (Array.isArray(entryConfigs) && !entryConfigs.length) { |
| 97 | + console.log(chalk.yellow('请指定正确的 key !')) |
| 98 | + process.exit(1) |
| 99 | + } |
| 100 | + |
| 101 | + entryConfigs.forEach(entryConfig => { |
| 102 | + // override vue's original configuration |
| 103 | + options.css.extract = Boolean(entryConfig.extract) |
| 104 | + const webpackConfigChain = api.resolveChainableWebpackConfig() |
| 105 | + |
| 106 | + pruneDefaultWebpackConfig(webpackConfigChain) |
| 107 | + |
| 108 | + if (mode === 'build') { |
| 109 | + // 设置 mode 为 production |
| 110 | + webpackConfigChain.mode('production') |
| 111 | + // 关闭 watch 模式 |
| 112 | + webpackConfigChain.watch(false) |
| 113 | + |
| 114 | + webpackConfigChain |
| 115 | + .plugin('optimize-css') |
| 116 | + .use(OptimizeCSSAssetsPlugin) |
| 117 | + } |
| 118 | + |
| 119 | + if (mode === 'watch') { |
| 120 | + // 是否开启watch模式 |
| 121 | + webpackConfigChain.watch(true) |
| 122 | + } |
| 123 | + // set entry config |
| 124 | + webpackConfigChain |
| 125 | + .entry(entryConfig.chunkName) |
| 126 | + .add(entryConfig.entry) |
| 127 | + |
| 128 | + // set output config |
| 129 | + webpackConfigChain |
| 130 | + .output |
| 131 | + .path(entryConfig.output) |
| 132 | + .filename('[name].js') |
| 133 | + .chunkFilename('[name].js') |
| 134 | + |
| 135 | + // clean dist path |
| 136 | + webpackConfigChain |
| 137 | + .plugin('clean') |
| 138 | + .use(CleanWebpackPlugin) |
| 139 | + |
| 140 | + // save per entry file webpack config |
| 141 | + entryWebpackConfigs.push(webpackConfigChain.toConfig()) |
| 142 | + entryDescriptions.add(entryConfig.description) |
| 143 | + |
| 144 | + // clean entry config & dist config |
| 145 | + webpackConfigChain.entryPoints.store.delete(entryConfig.output) |
| 146 | + webpackConfigChain.output.clear() |
| 147 | + }) |
| 148 | + |
| 149 | + if (entryDescriptions.size) { |
| 150 | + [...entryDescriptions].forEach(description => { |
| 151 | + console.log(chalk.green(description)) |
| 152 | + }) |
| 153 | + } |
| 154 | + |
| 155 | + if (immediate) { |
| 156 | + webpack(entryWebpackConfigs, (err, stats) => { |
| 157 | + if (err || stats.hasErrors()) { |
| 158 | + console.log(chalk.red('webpack build error!')) |
| 159 | + // process.exit(1) |
| 160 | + } |
| 161 | + }) |
| 162 | + } |
| 163 | + |
| 164 | + return entryWebpackConfigs |
| 165 | +} |
| 166 | + |
| 167 | +function pruneDefaultWebpackConfig (webpackConfigChain) { |
| 168 | + // 删除默认入口 |
| 169 | + webpackConfigChain.entryPoints.store.delete('app') |
| 170 | + // 删除 copy-webpack-plugin 插件,避免打包时将 public 文件夹打包到输出路径 |
| 171 | + webpackConfigChain.plugins.delete('copy') |
| 172 | + // 删除 html-webpack-plugin 插件 |
| 173 | + webpackConfigChain.plugins.delete('html') |
| 174 | + // 删除热加载插件 |
| 175 | + webpackConfigChain.plugins.delete('hmr') |
| 176 | + // 删除 preload 和 prefetch 插件 |
| 177 | + webpackConfigChain.plugins.delete('preload') |
| 178 | + webpackConfigChain.plugins.delete('prefetch') |
| 179 | + // 禁止任何异步加载的 chunk |
| 180 | + webpackConfigChain |
| 181 | + .plugin('LimitChunkCountPlugin') |
| 182 | + .use(webpack.optimize.LimitChunkCountPlugin, [{ |
| 183 | + maxChunks: 1 |
| 184 | + }]) |
| 185 | +} |
| 186 | + |
| 187 | +module.exports = { |
| 188 | + getEntryFiles, |
| 189 | + resolveEntryConfig, |
| 190 | + pruneDefaultWebpackConfig |
| 191 | +} |
0 commit comments