@@ -13,76 +13,84 @@ const pkg = require('./package.json');
1313module . exports = ( { production = false } = { } ) => {
1414 process . env . NODE_ENV = production ? 'production' : 'development' ;
1515
16- const firebaseConfig = JSON . stringify ( {
17- apiKey : process . env . FIREBASE_API_KEY ,
18- messagingSenderId : process . env . FIREBASE_MESSAGING_SENDER_ID ,
19- databaseURL : process . env . FIREBASE_DATABASE_URL
20- } ) ;
16+ const configs = {
17+ build : path . resolve ( __dirname , './build' ) ,
18+ src : path . resolve ( __dirname , './src' ) ,
19+ sourceMap : production ? 'cheap-module-source-map' : false ,
20+ chunkName : production ? '[name].[chunkhash].js' : '[name].js'
21+ } ;
22+
23+ const defined = {
24+ 'process.env.NODE_ENV' : JSON . stringify ( process . env . NODE_ENV ) ,
25+ FIREBASE_CONFIG : JSON . stringify ( {
26+ apiKey : process . env . FIREBASE_API_KEY ,
27+ messagingSenderId : process . env . FIREBASE_MESSAGING_SENDER_ID ,
28+ databaseURL : process . env . FIREBASE_DATABASE_URL
29+ } )
30+ } ;
31+
32+ const replaceDefined = ( content , from ) => {
33+ return content . toString ( ) . replace ( / F I R E B A S E _ C O N F I G / , defined . FIREBASE_CONFIG ) ;
34+ } ;
2135
22- const sourceMap = production ? 'cheap-module-source-map' : false ;
23- const chunkName = production ? '[name].[chunkhash].js' : '[name].js' ;
36+ const minify = production ? {
37+ removeComments : true ,
38+ collapseWhitespace : true ,
39+ removeRedundantAttributes : true ,
40+ useShortDoctype : true ,
41+ removeEmptyAttributes : true ,
42+ removeStyleLinkTypeAttributes : true ,
43+ keepClosingSlash : true ,
44+ minifyJS : true
45+ } : { } ;
2446
2547 const webpackConfig = {
2648 entry : {
2749 main : [ './src/main.js' ] ,
2850 vendor : [ 'react' , 'react-dom' , 'react-router' , 'material-ui' , 'firebase' ]
2951 } ,
3052 output : {
31- path : path . resolve ( __dirname , './ build' ) ,
32- filename : chunkName ,
33- chunkFilename : chunkName
53+ path : configs . build ,
54+ filename : configs . chunkName ,
55+ chunkFilename : configs . chunkName
3456 } ,
3557 module : {
3658 loaders : [ {
3759 test : / \. ( j s | j s x ) $ / ,
38- include : path . resolve ( __dirname , './ src' ) ,
60+ include : configs . src ,
3961 loaders : 'babel-loader' ,
4062 options : {
4163 presets : [ [ 'es2015' , { modules : false } ] , "react-app" ] ,
4264 plugins : [ 'syntax-dynamic-import' ]
4365 }
4466 } ]
4567 } ,
46- devtool : sourceMap ,
68+ devtool : configs . sourceMap ,
4769 plugins : [
4870 new optimize . CommonsChunkPlugin ( {
4971 name : [ 'vendor' , 'manifest' ]
5072 } ) ,
51- new DefinePlugin ( {
52- FIREBASE_CONFIG : firebaseConfig ,
53- 'process.env.NODE_ENV' : JSON . stringify ( process . env . NODE_ENV )
54- } ) ,
73+ new DefinePlugin ( defined ) ,
5574 new HtmlWebpackPlugin ( Object . assign ( {
5675 template : './public/index.html' ,
5776 favicon : './public/favicon.ico' ,
58- } , production ? {
59- minify : {
60- removeComments : true ,
61- collapseWhitespace : true ,
62- removeRedundantAttributes : true ,
63- useShortDoctype : true ,
64- removeEmptyAttributes : true ,
65- removeStyleLinkTypeAttributes : true ,
66- keepClosingSlash : true ,
67- minifyJS : true
68- }
69- } : { } ) ) ,
77+ } , {
78+ minify
79+ } ) ) ,
7080 new PreloadWebpackPlugin ( ) ,
7181 new CopyWebpackPlugin ( [ {
7282 context : './public' ,
7383 from : '*.*'
7484 } , {
7585 from : './src/firebase-messaging-sw.js' ,
7686 to : 'firebase-messaging-sw.js' ,
77- transform : ( content , from ) => {
78- return content . toString ( ) . replace ( / F I R E B A S E _ C O N F I G / , firebaseConfig ) ;
79- }
87+ transform : replaceDefined
8088 } ] ) ,
8189 new SWPrecacheWebpackPlugin ( {
8290 cacheId : `${ pkg . name } -${ pkg . version } ` ,
8391 stripPrefix : './build' ,
8492 staticFileGlobs : [
85- path . join ( path . resolve ( __dirname , './ build' ) , '**/*' )
93+ path . join ( configs . build , '**/*' )
8694 ] ,
8795 runtimeCaching : [ {
8896 urlPattern : / h t t p s : \/ \/ .+ .f i r e b a s e i o .c o m / ,
0 commit comments