1
1
/* global process:false, __dirname:false */
2
2
3
3
const webpack = require ( 'webpack' ) ;
4
- const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
5
- const CopyWebpackPlugin = require ( 'copy-webpack-plugin' ) ;
6
- const ExtractTextPlugin = require ( "extract-text-webpack -plugin" ) ;
7
- const ZopfliPlugin = require ( "zopfli -webpack-plugin" ) ;
4
+ const HtmlPlugin = require ( 'html-webpack-plugin' ) ;
5
+ const CopyPlugin = require ( 'copy-webpack-plugin' ) ;
6
+ const MiniCssExtractPlugin = require ( "mini-css-extract -plugin" ) ;
7
+ const CompressionPlugin = require ( "compression -webpack-plugin" ) ;
8
8
const glob = require ( 'glob' ) ;
9
9
const basename = require ( 'basename' ) ;
10
10
@@ -18,99 +18,92 @@ const allThemeNames = glob.sync('./node_modules/brace/theme/*.js').map(basename)
18
18
const allKeybindings = allKeybindingNames . concat ( [ 'ace' ] ) . sort ( ) ;
19
19
const allThemes = allThemeNames ;
20
20
21
- module . exports = {
22
- entry : {
23
- app : [ './index.tsx' , './index.scss' ] ,
24
- } ,
21
+ // The name is nicer to debug with, but changing names breaks long-term-caching
22
+ const developmentFilenameTemplate = '[name]-[chunkhash]' ;
23
+ const productionFilenameTemplate = '[chunkhash]' ;
25
24
26
- output : {
27
- publicPath : 'assets/' ,
28
- path : `${ __dirname } /build/assets` ,
29
- filename : '[name]-[chunkhash].js' ,
30
- chunkFilename : '[name]-[chunkhash].js' ,
31
- } ,
25
+ module . exports = function ( _ , argv ) {
26
+ console . log ( argv . mode ) ;
27
+ const isProduction = argv . mode === 'production' ;
28
+ const filenameTemplate =
29
+ isProduction ?
30
+ productionFilenameTemplate :
31
+ developmentFilenameTemplate ;
32
32
33
- resolve : {
34
- extensions : [ '.js' , '.jsx' , '.ts' , '.tsx' ] ,
35
- } ,
33
+ return {
34
+ entry : {
35
+ app : [ './index.tsx' , './index.scss' ] ,
36
+ } ,
36
37
37
- module : {
38
- rules : [
39
- {
40
- test : [ / \. j s $ / , / \. j s x $ / ] ,
41
- exclude : / n o d e _ m o d u l e s / ,
42
- use : 'babel-loader' ,
43
- } ,
44
- {
45
- test : [ / \. t s $ / , / \. t s x $ / ] ,
46
- exclude : / n o d e _ m o d u l e s / ,
47
- use : [ 'babel-loader' , 'ts-loader' ] ,
48
- } ,
49
- {
50
- test : / \. s c s s $ / ,
51
- use : ExtractTextPlugin . extract ( {
52
- fallback : 'style-loader' ,
53
- use : [ "css-loader" , "postcss-loader" , "sass-loader" ]
54
- } ) ,
55
- } ,
56
- {
57
- test : / \. s v g $ / ,
58
- use : {
59
- loader : 'svg-url-loader' ,
60
- options : {
61
- noquotes : true ,
62
- } ,
63
- } ,
64
- } ,
65
- ] ,
66
- } ,
38
+ output : {
39
+ publicPath : 'assets/' ,
40
+ path : `${ __dirname } /build/assets` ,
41
+ filename : `${ filenameTemplate } .js` ,
42
+ chunkFilename : `${ filenameTemplate } .js` ,
43
+ } ,
67
44
68
- plugins : [
69
- new webpack . EnvironmentPlugin ( { NODE_ENV : 'development' } ) ,
70
- new webpack . DefinePlugin ( {
71
- ACE_KEYBINDINGS : JSON . stringify ( allKeybindings ) ,
72
- ACE_THEMES : JSON . stringify ( allThemes ) ,
73
- } ) ,
74
- new HtmlWebpackPlugin ( {
75
- title : "Rust Playground" ,
76
- template : 'index.ejs' ,
77
- filename : '../index.html' ,
78
- chunksSortMode : 'dependency' ,
79
- } ) ,
80
- new CopyWebpackPlugin ( [
81
- { from : 'robots.txt' , to : '..' } ,
82
- ] ) ,
83
- new ExtractTextPlugin ( "styles-[chunkhash].css" ) ,
84
- new webpack . optimize . CommonsChunkPlugin ( {
85
- name : "vendor" ,
86
- minChunks : module => {
87
- const { context } = module ;
88
- if ( ! context ) { return false ; }
45
+ resolve : {
46
+ extensions : [ '.js' , '.jsx' , '.ts' , '.tsx' ] ,
47
+ } ,
89
48
90
- // Ignore files that are not from a third-party package
91
- if ( context . indexOf ( "node_modules" ) === - 1 ) { return false ; }
49
+ module : {
50
+ rules : [
51
+ {
52
+ test : [ / \. j s $ / , / \. j s x $ / ] ,
53
+ exclude : / n o d e _ m o d u l e s / ,
54
+ use : 'babel-loader' ,
55
+ } ,
56
+ {
57
+ test : [ / \. t s $ / , / \. t s x $ / ] ,
58
+ exclude : / n o d e _ m o d u l e s / ,
59
+ use : [ 'babel-loader' , 'ts-loader' ] ,
60
+ } ,
61
+ {
62
+ test : / \. s c s s $ / ,
63
+ use : [
64
+ MiniCssExtractPlugin . loader ,
65
+ "css-loader" ,
66
+ "postcss-loader" ,
67
+ "sass-loader" ,
68
+ ] ,
69
+ } ,
70
+ {
71
+ test : / \. s v g $ / ,
72
+ use : {
73
+ loader : 'svg-url-loader' ,
74
+ options : {
75
+ noquotes : true ,
76
+ } ,
77
+ } ,
78
+ } ,
79
+ ] ,
80
+ } ,
92
81
93
- // Ignore development dependencies
94
- const isDevDependency = depName => context . indexOf ( depName ) !== - 1 ;
95
- if ( devDependencies . some ( isDevDependency ) ) { return false ; }
82
+ plugins : [
83
+ new webpack . DefinePlugin ( {
84
+ ACE_KEYBINDINGS : JSON . stringify ( allKeybindings ) ,
85
+ ACE_THEMES : JSON . stringify ( allThemes ) ,
86
+ } ) ,
87
+ new HtmlPlugin ( {
88
+ title : "Rust Playground" ,
89
+ template : 'index.ejs' ,
90
+ filename : '../index.html' ,
91
+ chunksSortMode : 'dependency' ,
92
+ } ) ,
93
+ new CopyPlugin ( [
94
+ { from : 'robots.txt' , to : '..' } ,
95
+ ] ) ,
96
+ new MiniCssExtractPlugin ( {
97
+ filename : `${ filenameTemplate } .css` ,
98
+ chunkFilename : `${ filenameTemplate } .css` ,
99
+ } ) ,
100
+ ...( isProduction ? [ new CompressionPlugin ( ) ] : [ ] ) ,
101
+ ] ,
96
102
97
- return true ;
103
+ optimization : {
104
+ splitChunks : {
105
+ chunks : "all" ,
98
106
} ,
99
- } ) ,
100
- new webpack . optimize . CommonsChunkPlugin ( {
101
- name : "manifest" ,
102
- minChunks : Infinity
103
- } ) ,
104
- ] ,
107
+ } ,
108
+ } ;
105
109
} ;
106
-
107
- if ( process . env . NODE_ENV === 'production' ) {
108
- module . exports . plugins . push (
109
- new webpack . optimize . UglifyJsPlugin ( {
110
- compress : {
111
- warnings : false ,
112
- } ,
113
- } ) ,
114
- new ZopfliPlugin ( ) ,
115
- ) ;
116
- }
0 commit comments