11'use strict' ;
22
3- const { _, tryRequire } = require ( '@micro-app/shared-utils' ) ;
3+ const { _, tryRequire, fs } = require ( '@micro-app/shared-utils' ) ;
4+ const createService = require ( '../utils/createService' ) ;
5+ const CONSTANTS = require ( '../constants' ) ;
46
57// 以 vue-cli 配置为主
6- module . exports = function chainDefault ( api , vueConfig , options , webpackConfig ) {
7- const webpackConfigAlias = webpackConfig . resolve . alias || { } ;
8+ module . exports = function chainDefault ( api , vueConfig , options ) {
89
910 [ // string
1011 'publicPath' ,
@@ -28,9 +29,8 @@ module.exports = function chainDefault(api, vueConfig, options, webpackConfig) {
2829 }
2930 } ) ;
3031
32+ // 补充
3133 api . chainWebpack ( webpackChain => {
32- const outputDir = api . resolve ( options . outputDir ) ;
33-
3434 const nodeModulesPaths = options . nodeModulesPaths || [ ] ;
3535 webpackChain . resolve
3636 . set ( 'symlinks' , true )
@@ -44,30 +44,55 @@ module.exports = function chainDefault(api, vueConfig, options, webpackConfig) {
4444 . merge ( nodeModulesPaths )
4545 . end ( ) ;
4646
47- const alias = Object . assign ( { } , options . resolveAlias || { } , webpackConfigAlias ) ;
47+ const alias = Object . assign ( { } , options . resolveAlias || { } ) ;
4848 // alias
4949 webpackChain . resolve
5050 . alias
5151 . merge ( alias )
5252 . end ( ) ;
5353
54- // const entry = options.entry || {};
55- // // entry
56- // Object.keys(entry).forEach(key => {
57- // webpackChain.entry(key).merge(entry[key]);
58- // });
59-
60- const CopyWebpackPlugin = tryRequire ( 'copy-webpack-plugin' ) ;
61- if ( CopyWebpackPlugin ) {
62- const staticPaths = options . staticPaths || [ ] ;
63- // 有的时候找不到原来的 CopyWebpackPlugin,不知道为什么
64- webpackChain . plugin ( 'copyEx' ) . use ( CopyWebpackPlugin , [
65- staticPaths . map ( staticPath => {
66- return { from : staticPath , to : outputDir , ignore : [ '.*' ] } ;
67- } ) ,
68- ] ) ;
54+ // copy static
55+ const staticPaths = ( options . staticPaths || [ ] ) . filter ( item => fs . existsSync ( item ) ) ;
56+ if ( staticPaths . length ) {
57+ const CopyWebpackPlugin = tryRequire ( 'copy-webpack-plugin' ) ;
58+ if ( CopyWebpackPlugin ) {
59+ webpackChain
60+ . plugin ( 'copy' )
61+ . use ( CopyWebpackPlugin , [ staticPaths . map ( publicDir => {
62+ return {
63+ from : publicDir ,
64+ toType : 'dir' ,
65+ ignore : [ '.*' ] ,
66+ } ;
67+ } ) ] ) ;
68+ } else {
69+ api . logger . warn ( '[webpack]' , 'Not Found "copy-webpack-plugin"' ) ;
70+ }
6971 }
7072
7173 return webpackChain ;
7274 } ) ;
75+
76+ // webpack 所有配置合入
77+ api . chainWebpack ( webpackChain => {
78+ const service = createService ( ) ;
79+
80+ // 注册插件
81+ service . registerPlugin ( {
82+ id : 'vue-cli-plugin:plugin-command-return-webpack-chain' ,
83+ [ CONSTANTS . builtIn ] : true ,
84+ apply ( _api ) {
85+ _api . registerCommand ( 'return-webpack-chain' , {
86+ description : 'return config of webpack-chain.' ,
87+ usage : 'micro-app return-webpack-chain' ,
88+ } , ( ) => {
89+ _api . createChainWebpackConfigInstance ( webpackChain ) ;
90+ return _api . resolveChainableWebpackConfig ( ) ;
91+ } ) ;
92+ } ,
93+ } ) ;
94+
95+ // 同步扩充 webpack-chain config
96+ return service . runSync ( 'return-webpack-chain' , { _ : [ ] , target : CONSTANTS . skipTarget } ) ;
97+ } ) ;
7398} ;
0 commit comments