11const path = require ( 'path' )
22const os = require ( 'os' )
3+ const fs = require ( 'fs' )
34const webpack = require ( 'webpack' )
45const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' )
56const { BundleAnalyzerPlugin } = require ( 'webpack-bundle-analyzer' )
@@ -8,22 +9,17 @@ const HtmlWebpackPlugin = require('html-webpack-plugin')
89const VueLoaderPlugin = require ( 'vue-loader/lib/plugin' )
910const FriendlyErrorsWebpackPlugin = require ( 'friendly-errors-webpack-plugin' )
1011const CopyWebpackPlugin = require ( 'copy-webpack-plugin' )
11- const isProduction = process . env . NODE_ENV === 'production'
12- const projectPath = process . cwd ( )
12+ const { projectPath, envFilePath, isServe, isAnalyze } = require ( '../consts' )
1313
1414const plugins = [
15- new CleanWebpackPlugin ( {
16- verbose : true ,
17- cleanStaleWebpackAssets : false ,
18- cleanOnceBeforeBuildPatterns : [ '**/*' ] ,
19- } ) ,
2015 new FriendlyErrorsWebpackPlugin ( {
2116 compilationSuccessInfo : {
2217 messages : compileMessages ( ) ,
2318 notes : [ ] ,
2419 } ,
2520 clearConsole : true ,
2621 } ) ,
22+ new webpack . DefinePlugin ( getDefinePluginObj ( ) ) ,
2723 new webpack . DllReferencePlugin ( {
2824 context : projectPath ,
2925 manifest : require ( path . join (
@@ -32,10 +28,11 @@ const plugins = [
3228 ) ) ,
3329 } ) ,
3430 new webpack . IgnorePlugin ( {
35- checkResource : ( resourcePath ) => {
31+ checkResource : resourcePath => {
3632 if ( / m o m e n t \/ l o c a l e \/ (? ! z h - c n ) / . test ( resourcePath ) ) {
3733 return true
3834 }
35+ return false
3936 } ,
4037 } ) ,
4138 new VueLoaderPlugin ( ) ,
@@ -59,16 +56,47 @@ const plugins = [
5956 ] ) ,
6057]
6158
62- if ( process . env . isAnalyze ) {
59+ if ( ! isServe ) {
60+ plugins . unshift (
61+ new CleanWebpackPlugin ( {
62+ verbose : true ,
63+ cleanStaleWebpackAssets : false ,
64+ cleanOnceBeforeBuildPatterns : [ '**/*' ] ,
65+ } )
66+ )
67+ }
68+
69+ if ( isAnalyze ) {
6370 plugins . push ( new BundleAnalyzerPlugin ( ) )
6471}
6572
73+ function getDefinePluginObj ( ) {
74+ const obj = { }
75+ for ( let p of Object . keys ( process . env ) ) {
76+ if ( p . startsWith ( 'VUE_' ) ) {
77+ obj [ p ] = process . env [ p ]
78+ }
79+ }
80+ if ( fs . existsSync ( envFilePath ) && fs . statSync ( envFilePath ) . isFile ( ) ) {
81+ const fileContent = fs . readFileSync ( envFilePath ) . toString ( )
82+ const array = fileContent . split ( / [ \r \n ] / )
83+ for ( let i = 0 , len = array . length ; i < len ; i ++ ) {
84+ if ( array [ i ] . startsWith ( '#' ) ) {
85+ continue
86+ }
87+ const itemArray = array [ i ] . split ( '=' )
88+ obj [ itemArray [ 0 ] ] = `'${ itemArray [ 1 ] } '`
89+ }
90+ }
91+ return obj
92+ }
93+
6694function getAccessUrlArray ( ) {
6795 const result = [ ]
6896 const netInterfaces = Object . entries ( os . networkInterfaces ( ) )
6997 for ( let netInterface of netInterfaces ) {
7098 const interfaceInfo = netInterface [ 1 ]
71- interfaceInfo . forEach ( ( ipObj ) => {
99+ interfaceInfo . forEach ( ipObj => {
72100 if ( ipObj . family === 'IPv4' ) {
73101 result . push (
74102 ` - Access URL: http://${ ipObj . address } :${ process . env . port } /index.html`
@@ -81,7 +109,7 @@ function getAccessUrlArray() {
81109
82110function compileMessages ( ) {
83111 const messages = [ ]
84- if ( ! isProduction ) {
112+ if ( isServe ) {
85113 return getAccessUrlArray ( ) . concat ( messages )
86114 }
87115 return messages
0 commit comments