|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +module.exports = function subModuleWebpack(api, opts) { |
| 4 | + |
| 5 | + api.assertVersion('>=0.3.26'); |
| 6 | + |
| 7 | + const registerMethods = require('./methods'); |
| 8 | + registerMethods(api); |
| 9 | + |
| 10 | + // TODO 只有开启 --sub-module 时才处理 |
| 11 | + if (!api.context.subModule) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + // 解析 subModule 参数,只查询自己模块的 |
| 16 | + const selfConfig = api.selfConfig || {}; |
| 17 | + const originalConfig = selfConfig.originalConfig || {}; |
| 18 | + |
| 19 | + const subModule = originalConfig.subModule; |
| 20 | + if (!subModule) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + // 增加校验配置 |
| 25 | + api.validateSchema(require('./configSchema'), originalConfig); |
| 26 | + |
| 27 | + const configParser = require('./configParser'); |
| 28 | + const _configParser = configParser(selfConfig); |
| 29 | + |
| 30 | + // 直接在 config 上设置, 扩增 config 配置 |
| 31 | + const config = api.config; |
| 32 | + config.subModule = config.subModule || {}; |
| 33 | + |
| 34 | + Object.assign(config.subModule, subModule, { |
| 35 | + prefix: _configParser.prefix(), |
| 36 | + namespace: _configParser.namespace(), |
| 37 | + entry: _configParser.entry(), |
| 38 | + fileName: _configParser.fileName(), |
| 39 | + }); |
| 40 | + |
| 41 | + // change config.pages |
| 42 | + const entryPoints = config.subModule.entry; |
| 43 | + config.pages = { |
| 44 | + ...Object.keys(entryPoints).reduce((obj, key) => { |
| 45 | + obj[key] = { |
| 46 | + entry: entryPoints[key], |
| 47 | + }; |
| 48 | + return obj; |
| 49 | + }, {}), |
| 50 | + }; |
| 51 | + |
| 52 | + // externals, 注入全局 window |
| 53 | + |
| 54 | + |
| 55 | + // 生成 manifest |
| 56 | + api.modifyChainWebpackConfig(webpackChain => { |
| 57 | + const otherOptions = Object.keys(config.subModule).reduce((obj, key) => { |
| 58 | + if (![ 'prefix', 'namespace', 'entry' ].includes(key)) { |
| 59 | + obj[key] = config.subModule[key]; |
| 60 | + } |
| 61 | + return obj; |
| 62 | + }, {}); |
| 63 | + |
| 64 | + const ManifestPlugin = require('webpack-manifest-plugin'); |
| 65 | + webpackChain.plugin('assets-manifest') |
| 66 | + .use(ManifestPlugin, [{ |
| 67 | + generate(seed, files, entrypoints) { |
| 68 | + return files.reduce((manifest, descriptor) => { |
| 69 | + const { name, path, isInitial, isChunk } = descriptor; |
| 70 | + if (descriptor.name.toLowerCase().endsWith('.map')) { |
| 71 | + manifest.map = { ...(manifest.map || {}), [name]: path }; |
| 72 | + } else if (isInitial) { |
| 73 | + manifest.entry = { ...(manifest.entry || {}), [name]: path }; |
| 74 | + } else if (isChunk) { |
| 75 | + manifest.assets = { ...(manifest.assets || {}), [name]: path }; |
| 76 | + } else { |
| 77 | + manifest.static = { ...(manifest.static || {}), [name]: path }; |
| 78 | + } |
| 79 | + return manifest; |
| 80 | + }, seed); |
| 81 | + }, |
| 82 | + ...otherOptions, |
| 83 | + }]) |
| 84 | + .end(); |
| 85 | + |
| 86 | + return webpackChain; |
| 87 | + }); |
| 88 | + |
| 89 | + api.modifyWebpackConfig(webpackConfig => { |
| 90 | + const namespace = config.subModule.namespace; |
| 91 | + const prefix = config.subModule.prefix; |
| 92 | + |
| 93 | + // chunkFilename |
| 94 | + const chunkFilename = webpackConfig.output.chunkFilename; |
| 95 | + if (chunkFilename && chunkFilename.includes('[name]')) { |
| 96 | + webpackConfig.output.chunkFilename = chunkFilename.replace('[name]', `${prefix}_${namespace}-[name]`); |
| 97 | + } else { |
| 98 | + webpackConfig.output.chunkFilename = `js/${prefix}_${namespace}-[name].[contenthash:8].js`; |
| 99 | + } |
| 100 | + |
| 101 | + // TODO 提供方法对外补充一些修改 |
| 102 | + return webpackConfig; |
| 103 | + }); |
| 104 | +}; |
| 105 | + |
| 106 | +module.exports.configuration = { |
| 107 | + description: 'webpack 适配增强 config 配置信息', |
| 108 | +}; |
0 commit comments