11const path = require ( "path" )
22const TerserPlugin = require ( "terser-webpack-plugin" )
33const MiniCssExtractPlugin = require ( "mini-css-extract-plugin" )
4- let isProduction = process . env . NODE_ENV === "production"
54const { CleanWebpackPlugin } = require ( "clean-webpack-plugin" )
65
7- module . exports = {
8- entry : {
9- "CoCreate-lazy-loader" : "./src/index.js" ,
10- } ,
11- output : {
12- path : path . resolve ( __dirname , "dist" ) ,
13- filename : isProduction ? "[name].min.js" : "[name].js" ,
14- libraryTarget : "umd" ,
15- libraryExport : "default" ,
16- library : [ "CoCreate" , "lazy-loader" ] ,
17- globalObject : "this" ,
18- } ,
6+ module . exports = ( env , argv ) => {
7+ let isProduction = false
8+ if ( argv . mode === 'production' )
9+ isProduction = true
1910
20- plugins : [
21- new CleanWebpackPlugin ( ) ,
22- new MiniCssExtractPlugin ( {
23- filename : "[name].css" ,
24- } ) ,
25- ] ,
26- // Default mode for Webpack is production.
27- // Depending on mode Webpack will apply different things
28- // on final bundle. For now we don't need production's JavaScript
29- // minifying and other thing so let's set mode to development
30- mode : isProduction ? "production" : "development" ,
31- module : {
32- rules : [
33- {
34- test : / .j s $ / ,
35- exclude : / ( n o d e _ m o d u l e s ) / ,
36- use : {
37- loader : "babel-loader" ,
38- options : {
39- plugins : [ "@babel/plugin-transform-modules-commonjs" ] ,
40- } ,
11+ const config = {
12+ entry : {
13+ "CoCreate-lazy-loader" : "./src/index.js" ,
4114 } ,
42- } ,
43- {
44- test : / .c s s $ / i,
45- use : [
46- { loader : "style-loader" , options : { injectType : "linkTag" } } ,
47- "file-loader" ,
15+ output : {
16+ path : path . resolve ( __dirname , "dist" ) ,
17+ filename : isProduction ? "[name].min.js" : "[name].js" ,
18+ libraryTarget : "umd" ,
19+ libraryExport : "default" ,
20+ library : [ "CoCreate" , "lazy-loader" ] ,
21+ globalObject : "this" ,
22+ } ,
23+
24+ plugins : [
25+ new CleanWebpackPlugin ( ) ,
26+ new MiniCssExtractPlugin ( {
27+ filename : "[name].css" ,
28+ } ) ,
4829 ] ,
49- } ,
50- ] ,
51- } ,
30+ // Default mode for Webpack is production.
31+ // Depending on mode Webpack will apply different things
32+ // on final bundle. For now we don't need production's JavaScript
33+ // minifying and other thing so let's set mode to development
34+ mode : isProduction ? "production" : "development" ,
35+ module : {
36+ rules : [
37+ {
38+ test : / .j s $ / ,
39+ exclude : / ( n o d e _ m o d u l e s ) / ,
40+ use : {
41+ loader : "babel-loader" ,
42+ options : {
43+ plugins : [ "@babel/plugin-transform-modules-commonjs" ] ,
44+ } ,
45+ } ,
46+ } ,
47+ {
48+ test : / .c s s $ / i,
49+ use : [
50+ { loader : "style-loader" , options : { injectType : "linkTag" } } ,
51+ "file-loader" ,
52+ ] ,
53+ } ,
54+ ] ,
55+ } ,
5256
53- // add source map
54- ...( isProduction ? { } : { devtool : "eval-source-map" } ) ,
57+ // add source map
58+ ...( isProduction ? { } : { devtool : "eval-source-map" } ) ,
5559
56- optimization : {
57- minimize : true ,
58- minimizer : [
59- new TerserPlugin ( {
60- extractComments : true ,
61- // cache: true,
62- parallel : true ,
63- // sourceMap: true, // Must be set to true if using source-maps in production
64- terserOptions : {
65- // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
66- // extractComments: 'all',
67- compress : {
68- drop_console : true ,
69- } ,
70- } ,
71- } ) ,
72- ] ,
73- splitChunks : {
74- chunks : "all" ,
75- minSize : 200 ,
76- // maxSize: 99999,
77- //minChunks: 1,
60+ optimization : {
61+ minimize : true ,
62+ minimizer : [
63+ new TerserPlugin ( {
64+ extractComments : true ,
65+ // cache: true,
66+ parallel : true ,
67+ // sourceMap: true, // Must be set to true if using source-maps in production
68+ terserOptions : {
69+ // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
70+ // extractComments: 'all',
71+ compress : {
72+ drop_console : true ,
73+ } ,
74+ } ,
75+ } ) ,
76+ ] ,
77+ splitChunks : {
78+ chunks : "all" ,
79+ minSize : 200 ,
80+ // maxSize: 99999,
81+ //minChunks: 1,
7882
79- cacheGroups : {
80- defaultVendors : false ,
81- } ,
82- } ,
83- } ,
84- }
83+ cacheGroups : {
84+ defaultVendors : false ,
85+ } ,
86+ } ,
87+ } ,
88+ }
89+ return config
90+ }
0 commit comments