11'use strict' ;
22
33const { _, tryRequire, fs, logger } = require ( '@micro-app/shared-utils' ) ;
4- const silentService = require ( '../utils/silentService' ) ;
5- const { BUILT_IN } = require ( '../constants' ) ;
64
75// 以 vue-cli 配置为主
8- module . exports = function chainDefault ( api , vueConfig , options ) {
6+ module . exports = function chainDefault ( api , vueConfig , _mapi ) {
7+ const options = _mapi . config || { } ;
98
109 [ // string
1110 'publicPath' ,
@@ -31,6 +30,9 @@ module.exports = function chainDefault(api, vueConfig, options) {
3130 }
3231 } ) ;
3332
33+ // global save vueConfig
34+ _mapi . setState ( 'vueConfig' , vueConfig ) ;
35+
3436 // 补充
3537 api . chainWebpack ( webpackChain => {
3638 const nodeModulesPaths = options . nodeModulesPaths || [ ] ;
@@ -72,44 +74,14 @@ module.exports = function chainDefault(api, vueConfig, options) {
7274 }
7375 }
7476
75- return webpackChain ;
77+ return _mapi . resolveChainableWebpackConfig ( webpackChain ) ;
7678 } ) ;
7779
78- let isOnce = false ;
7980 // webpack 所有配置合入
80- api . chainWebpack ( webpackChain => {
81- return silentService ( service => {
82- // 注册插件
83- service . registerPlugin ( {
84- id : 'vue-cli-plugin:plugin-command-return-webpack-chain' ,
85- [ BUILT_IN ] : true ,
86- apply ( _api ) {
87- _api . registerCommand ( 'return-webpack-chain' , {
88- description : 'return config of webpack-chain.' ,
89- usage : 'micro-app return-webpack-chain' ,
90- } , ( ) => {
91- // global save vueConfig
92- _api . setState ( 'vueConfig' , vueConfig ) ;
93- _api . resolveChainableWebpackConfig ( webpackChain ) ;
94-
95- if ( ! isOnce ) {
96- isOnce = true ;
97- const _service = api . service ;
98- // 覆盖逻辑
99- const originaFn = _service . resolveWebpackConfig ;
100- _service . resolveWebpackConfig = function ( chainableConfig ) {
101- const webpackConfig = originaFn . apply ( _service , chainableConfig ) ;
102- const finalWebpackConfig = _api . applyPluginHooks ( 'modifyWebpackConfig' , webpackConfig ) ;
103- _api . setState ( 'webpackConfig' , finalWebpackConfig ) ;
104- return finalWebpackConfig ;
105- } ;
106- }
107- } ) ;
108- } ,
109- } ) ;
110-
111- // 同步扩充 webpack-chain config
112- return service . runSync ( 'return-webpack-chain' ) ;
113- } ) ;
114- } ) ;
81+ const _service = api . service ;
82+ // 覆盖逻辑
83+ const originaFn = _service . resolveWebpackConfig ;
84+ _service . resolveWebpackConfig = function ( chainableConfig ) {
85+ return _mapi . resolveWebpackConfig ( originaFn . apply ( _service , chainableConfig ) ) ;
86+ } ;
11587} ;
0 commit comments