|
| 1 | +// const args = api.parseArgv(); |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const HOOK_KEY_MAP = { |
| 6 | + init: 'onServerInit', |
| 7 | + before: 'beforeServerEntry', |
| 8 | + after: 'afterServerEntry', |
| 9 | + willDone: 'onServerInitWillDone', |
| 10 | + done: 'onServerInitDone', |
| 11 | +}; |
| 12 | + |
| 13 | +module.exports = function unifiedExtend(api, opts) { |
| 14 | + |
| 15 | + api.assertVersion('>=0.3.0'); |
| 16 | + |
| 17 | + // TODO 补充基本配置 |
| 18 | + api.modifyChainWebpackPluginConfig(webpackChain => { |
| 19 | + |
| 20 | + // entry |
| 21 | + const entry = createTempConfigEntry(api); |
| 22 | + Object.keys(entry).forEach(key => { |
| 23 | + webpackChain.entry(key).merge(entry[key]); |
| 24 | + }); |
| 25 | + |
| 26 | + // const options = api.serverConfig || {}; |
| 27 | + |
| 28 | + // webpackChain |
| 29 | + // .context(api.root) |
| 30 | + // .output |
| 31 | + // .filename(outputFilename) |
| 32 | + // .chunkFilename(outputFilename) |
| 33 | + // .end(); |
| 34 | + |
| 35 | + return webpackChain; |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +module.exports.configuration = { |
| 40 | + description: 'webpack config for production', |
| 41 | + mode: 'production', |
| 42 | +}; |
| 43 | + |
| 44 | + |
| 45 | +function createTempConfigEntry(api) { |
| 46 | + const { fs, dedent, hash, path } = require('@micro-app/shared-utils'); |
| 47 | + const tempDir = api.tempDir; |
| 48 | + const pluginsDir = path.resolve(tempDir, 'plugins'); |
| 49 | + fs.ensureDirSync(pluginsDir); |
| 50 | + const allplugins = api.service.plugins; |
| 51 | + const filterPlugins = []; |
| 52 | + |
| 53 | + const builtInFlag = Symbol.for('built-in'); |
| 54 | + allplugins.forEach(plugin => { |
| 55 | + const id = plugin.id; |
| 56 | + const flag = plugin[builtInFlag]; |
| 57 | + if (id.startsWith('built-in:') || flag) { |
| 58 | + return; |
| 59 | + } |
| 60 | + const link = plugin.link; |
| 61 | + if (!filterPlugins.some(item => (item.id === id && item.link === link))) { |
| 62 | + filterPlugins.push(plugin); |
| 63 | + } |
| 64 | + }); |
| 65 | + const aliasPlugins = filterPlugins.map(plugin => { |
| 66 | + const id = plugin.id; |
| 67 | + const link = plugin.link; |
| 68 | + const aliasKey = hash(`${id}_${link}`); |
| 69 | + return { |
| 70 | + id, link, aliasKey, |
| 71 | + }; |
| 72 | + }); |
| 73 | + // TODO __dirname 引用有问题,需要优化。server 参数需要优化 |
| 74 | + const entryTexts = [ ]; |
| 75 | + aliasPlugins.forEach(({ id, link, aliasKey, opts = {} }) => { |
| 76 | + const tempIndex = path.resolve(pluginsDir, `${aliasKey}.js`); |
| 77 | + // 创建临时文件 |
| 78 | + fs.writeFileSync(tempIndex, dedent` |
| 79 | + 'use strict'; |
| 80 | + module.exports = require('${link}'); |
| 81 | + `); |
| 82 | + // entryTexts.push(`{id:"${id}", link: '${link}'}`); |
| 83 | + entryTexts.push(`{id:"${id}", link: path.resolve(__dirname, '${aliasKey}.js'), opts: ${JSON.stringify(opts)}}`); |
| 84 | + }); |
| 85 | + |
| 86 | + // hooks, entrys |
| 87 | + // TODO 需要迁移至适配兼容模块 |
| 88 | + const tempServerHooksAndEntrysText = createTempPlugin(api); |
| 89 | + if (tempServerHooksAndEntrysText) { |
| 90 | + const aliasKey = 'tempServerHooksAndEntrysText'; |
| 91 | + const tempIndex = path.resolve(pluginsDir, `${aliasKey}.js`); |
| 92 | + // 创建临时文件 |
| 93 | + fs.writeFileSync(tempIndex, tempServerHooksAndEntrysText); |
| 94 | + entryTexts.push(`{id:"temp:serverHooksAndEntrys", link: path.resolve(__dirname, '${aliasKey}.js')}`); |
| 95 | + } |
| 96 | + |
| 97 | + const plugins = `[${entryTexts.join(',\n')}]`; |
| 98 | + const entryIndex = writeConfigFile(pluginsDir, { |
| 99 | + version: api.version, |
| 100 | + plugins, server: api.serverConfig || {}, |
| 101 | + }); |
| 102 | + return { |
| 103 | + ...aliasPlugins.reduce((obj, { link, aliasKey }) => { |
| 104 | + if (aliasKey) { |
| 105 | + obj[aliasKey] = [ link ]; |
| 106 | + } |
| 107 | + return obj; |
| 108 | + }, {}), |
| 109 | + 'micro-app.config': [ entryIndex ], |
| 110 | + }; |
| 111 | +} |
| 112 | + |
| 113 | +function createTempPlugin(api) { |
| 114 | + const { dedent } = require('@micro-app/shared-utils'); |
| 115 | + const serverConfig = api.serverConfig || {}; |
| 116 | + const texts = []; |
| 117 | + (serverConfig.hooks || []).forEach((item, index) => { |
| 118 | + texts.push(convertHook(item, index)); |
| 119 | + }); |
| 120 | + (serverConfig.entrys || []).forEach((item, index) => { |
| 121 | + texts.push(convertEntry(item, index)); |
| 122 | + }); |
| 123 | + |
| 124 | + return dedent` |
| 125 | + 'use strict'; |
| 126 | + module.exports = function(api) { |
| 127 | + ${texts.join('\n')} |
| 128 | + }; |
| 129 | + `; |
| 130 | +} |
| 131 | + |
| 132 | +function convertHook({ link, key, info, options }, index) { |
| 133 | + const { dedent } = require('@micro-app/shared-utils'); |
| 134 | + const hookName = HOOK_KEY_MAP[key]; |
| 135 | + const name = info.name || ''; |
| 136 | + return dedent` |
| 137 | + const hook_${name}_${key}_${index} = require('${link}'); |
| 138 | + if (hook_${name}_${key}_${index} && typeof hook_${name}_${key}_${index} === 'function') { |
| 139 | + api.${hookName}(params => { |
| 140 | + const info = JSON.parse(${JSON.stringify(info)}); |
| 141 | + const options = JSON.parse(${JSON.stringify(options)}); |
| 142 | + return hook_${name}_${key}_${index}(params.app, options, info); |
| 143 | + }); |
| 144 | + } |
| 145 | + `; |
| 146 | +} |
| 147 | + |
| 148 | +function convertEntry({ info, options, link }, index) { |
| 149 | + const { dedent } = require('@micro-app/shared-utils'); |
| 150 | + const hookName = 'onServerEntry'; |
| 151 | + const name = info.name || ''; |
| 152 | + return dedent` |
| 153 | + const entry_${name}_${index} = require('${link}'); |
| 154 | + if (entry_${name}_${index} && typeof entry_${name}_${index} === 'function') { |
| 155 | + api.${hookName}(params => { |
| 156 | + const info = JSON.parse(${JSON.stringify(info)}); |
| 157 | + const options = JSON.parse(${JSON.stringify(options)}); |
| 158 | + return entry_${name}_${index}(params.app, options, info); |
| 159 | + }); |
| 160 | + } |
| 161 | + `; |
| 162 | +} |
| 163 | + |
| 164 | +function writeConfigFile(root, { version, plugins, server }) { |
| 165 | + const { fs, dedent, path } = require('@micro-app/shared-utils'); |
| 166 | + const entryIndex = path.resolve(root, 'main.js'); |
| 167 | + console.warn('entryIndex: ', entryIndex); |
| 168 | + fs.writeFileSync(entryIndex, dedent` |
| 169 | + 'use strict'; |
| 170 | + const path = require('path'); |
| 171 | + module.exports = { |
| 172 | + name: 'temp', |
| 173 | + description: '模拟配置文件1', |
| 174 | + version: ${JSON.stringify(version)}, |
| 175 | + plugins: ${plugins}, |
| 176 | + server: ${JSON.stringify(server || {})}, |
| 177 | + };`); |
| 178 | + return entryIndex; |
| 179 | +} |
0 commit comments