11const path = require ( 'path' )
22const os = require ( 'os' )
3+ const webpack = require ( 'webpack' )
4+ const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' )
35const { BundleAnalyzerPlugin } = require ( 'webpack-bundle-analyzer' )
6+ const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' )
47const HtmlWebpackPlugin = require ( 'html-webpack-plugin' )
5- const webpack = require ( 'webpack' )
68const VueLoaderPlugin = require ( 'vue-loader/lib/plugin' )
7- const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' )
89const FriendlyErrorsWebpackPlugin = require ( 'friendly-errors-webpack-plugin' )
10+ const CopyWebpackPlugin = require ( 'copy-webpack-plugin' )
911const isProduction = process . env . NODE_ENV === 'production'
10- const projectPath = __dirname . replace ( `build${ path . sep } webpack-config` , '' )
11-
12- function getAccessUrlArray ( ) {
13- const result = [ ]
14- const netInterfaces = Object . entries ( os . networkInterfaces ( ) )
15- for ( let netInterface of netInterfaces ) {
16- const interfaceInfo = netInterface [ 1 ]
17- interfaceInfo . forEach ( ipObj => {
18- if ( ipObj . family === 'IPv4' ) {
19- result . push ( ` - Access URL: http://${ ipObj . address } :${ process . env . port } /index.html` )
20- }
21- } )
22- }
23- return result
24- }
25-
26- const compileMessages = ( ) => {
27- const messages = [ ]
28- if ( ! isProduction ) {
29- return getAccessUrlArray ( ) . concat ( messages )
30- }
31- return messages
32- }
12+ const projectPath = process . cwd ( )
3313
3414const plugins = [
15+ new CleanWebpackPlugin ( {
16+ verbose : true ,
17+ cleanStaleWebpackAssets : false ,
18+ cleanOnceBeforeBuildPatterns : [ '**/*' ] ,
19+ } ) ,
3520 new FriendlyErrorsWebpackPlugin ( {
3621 compilationSuccessInfo : {
3722 messages : compileMessages ( ) ,
38- notes : [ ]
23+ notes : [ ] ,
3924 } ,
4025 clearConsole : true ,
4126 } ) ,
27+ new webpack . DllReferencePlugin ( {
28+ context : projectPath ,
29+ manifest : require ( path . join (
30+ projectPath ,
31+ 'public/vendor/manifest.json'
32+ ) ) ,
33+ } ) ,
4234 new webpack . IgnorePlugin ( {
4335 checkResource : ( resourcePath ) => {
44- if ( / m o m e n t \/ l o c a l e \/ (? ! z h - c n ) / . test ( resourcePath ) ) {
36+ if ( / m o m e n t \/ l o c a l e \/ (? ! z h - c n ) / . test ( resourcePath ) ) {
4537 return true
4638 }
4739 } ,
@@ -52,20 +44,47 @@ const plugins = [
5244 chunkFilename : 'css/[name].[hash].css' ,
5345 } ) ,
5446 new HtmlWebpackPlugin ( {
55- favicon : ` ${ projectPath } public/favicon.ico` ,
47+ favicon : path . join ( projectPath , ' public/favicon.ico' ) ,
5648 minify : {
5749 removeRedundantAttributes : true ,
5850 collapseWhitespace : true ,
5951 removeAttributeQuotes : true ,
6052 removeComments : true ,
61- collapseBooleanAttributes : true
53+ collapseBooleanAttributes : true ,
6254 } ,
6355 template : path . resolve ( projectPath , 'public/index.html' ) ,
6456 } ) ,
57+ new CopyWebpackPlugin ( [
58+ { from : 'public/vendor' , to : 'vendor' , context : projectPath } ,
59+ ] ) ,
6560]
6661
6762if ( process . env . isAnalyze ) {
6863 plugins . push ( new BundleAnalyzerPlugin ( ) )
6964}
7065
66+ function getAccessUrlArray ( ) {
67+ const result = [ ]
68+ const netInterfaces = Object . entries ( os . networkInterfaces ( ) )
69+ for ( let netInterface of netInterfaces ) {
70+ const interfaceInfo = netInterface [ 1 ]
71+ interfaceInfo . forEach ( ( ipObj ) => {
72+ if ( ipObj . family === 'IPv4' ) {
73+ result . push (
74+ ` - Access URL: http://${ ipObj . address } :${ process . env . port } /index.html`
75+ )
76+ }
77+ } )
78+ }
79+ return result
80+ }
81+
82+ function compileMessages ( ) {
83+ const messages = [ ]
84+ if ( ! isProduction ) {
85+ return getAccessUrlArray ( ) . concat ( messages )
86+ }
87+ return messages
88+ }
89+
7190module . exports = plugins
0 commit comments