1
+ const webpack = require ( 'webpack' ) ;
2
+ const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
3
+ process . env . NODE_ENV = "development" ;
4
+ const CopyPlugin = require ( "copy-webpack-plugin" ) ;
5
+ const { join } = require ( "path" ) ;
6
+
7
+
8
+ function getCompilerConfig ( ) {
9
+ const config = {
10
+ optimization : {
11
+ minimize : false ,
12
+ } ,
13
+ entry : {
14
+ options : join ( __dirname , "ui/options.js" ) ,
15
+ agent : join ( __dirname , "src/agent.js" ) ,
16
+ background : join ( __dirname , "src/background.js" ) ,
17
+ } ,
18
+ output : {
19
+ filename : '[name].js' ,
20
+ path : join ( __dirname , "chrome-extension" ) ,
21
+ chunkFilename : '[name].bundle.js' ,
22
+ } ,
23
+ module : {
24
+ rules : [
25
+ {
26
+ test : / \. ( j s | j s x ) $ / ,
27
+ exclude : / n o d e _ m o d u l e s / ,
28
+ use : {
29
+ loader : 'babel-loader' ,
30
+ options : {
31
+ "sourceType" : "unambiguous" ,
32
+ plugins : [
33
+ "@babel/plugin-proposal-class-properties" ,
34
+ "@babel/plugin-transform-react-jsx" ,
35
+ ]
36
+ }
37
+ }
38
+ } ,
39
+ {
40
+ test : / \. s c s s $ / ,
41
+ use : [
42
+ {
43
+ loader : "style-loader" // creates style nodes from JS strings
44
+ } ,
45
+ {
46
+ loader : "css-loader" // translates CSS into CommonJS
47
+ } ,
48
+ {
49
+ loader : "sass-loader" // compiles Sass to CSS
50
+ }
51
+ ]
52
+ }
53
+ ]
54
+ } ,
55
+ resolve : {
56
+ extensions : [ '*' , '.js' , '.jsx' ]
57
+ } ,
58
+ devtool : 'inline-source-map' ,
59
+ // devServer: {
60
+ // contentBase: './dist',
61
+ // hot: true,
62
+ // },
63
+ plugins : [
64
+
65
+ new HtmlWebpackPlugin ( {
66
+ chunks : [ "options" ] ,
67
+ template : "ui/options.ejs" ,
68
+ filename : 'options.html'
69
+ } ) ,
70
+ new CopyPlugin ( {
71
+ patterns : [
72
+ { from : "src/manifest.json" }
73
+ ] ,
74
+ } ) ,
75
+ new webpack . SourceMapDevToolPlugin ( { } )
76
+ ]
77
+ }
78
+ return config
79
+ }
80
+
81
+
82
+ const compilerDone = ( err , stats ) => {
83
+ const { errors, missingDependencies } = stats . compilation ;
84
+ if ( errors . length ) {
85
+ return errors . forEach ( error => console . log ( `got an error when compiling: ${ error . message } ` , error . stack ) )
86
+ }
87
+ console . log ( `app build ${ stats . hash } completed` )
88
+ }
89
+
90
+ module . exports = ( watch ) => {
91
+ const compiler = webpack ( getCompilerConfig ( ) ) ;
92
+ if ( watch ) {
93
+ console . log ( "In dev mode. Starting watcher" )
94
+ compiler . watch ( {
95
+ aggregateTimeout : 300 ,
96
+ poll : undefined
97
+ } , compilerDone )
98
+ } else {
99
+ compiler . run ( compilerDone )
100
+ }
101
+ }
0 commit comments