-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
151 lines (144 loc) · 4.13 KB
/
webpack.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as path from 'path'
import * as webpack from 'webpack'
import {config} from 'dotenv'
// webpack plugins
/* tslint:disable-next-line: no-var-requires */
const CleanWebpackPlugin = require('clean-webpack-plugin')
/* tslint:disable-next-line: no-var-requires */
const CopyWebpackPlugin = require('copy-webpack-plugin')
// postcss plugins
/* tslint:disable-next-line: no-var-requires */
const autoprefixer = require('autoprefixer')
const isProd = (): boolean => {
return process.env.NODE_ENV === 'production'
}
const env: any = config().parsed
const envKeys = Object.keys(env).reduce((prev: any, next: any) => {
prev[`process.env.${next}`] = JSON.stringify(env[next])
return prev
}, {})
const buildConfig: webpack.Configuration = {
entry: {
background_script: path.join(__dirname, 'src/background_script/index.ts'),
content_script: path.join(__dirname, 'src/content_script/index.ts'),
options: path.join(__dirname, 'src/options/index.tsx'),
popup: path.join(__dirname, 'src/popup/index.tsx')
},
// tslint:disable-next-line:no-object-literal-type-assertion
module: {
rules: [
// compile ts
{
exclude: /node_modules/,
loader: 'ts-loader',
test: /\.tsx?$/
},
// css loader
// source maps are generated only for dev builds
// css compression is only used for prod builds
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { sourceMap: !isProd() } },
{
loader: 'css-loader',
options: {
localIdentName: isProd() ? '[hash:base64]' : '[path][name]__[local]__[hash:base64:6]',
minimize: isProd(),
modules: true,
sourceMap: !isProd()
}
},
{
loader: 'postcss-loader',
options: {
plugins: () => [
autoprefixer({
browsers: ['>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9']
})
],
sourceMap: !isProd()
}
}
]
},
// file loader for media assets
{
exclude: [/\.(html?)$/, /\.(ts|tsx|js|jsx)$/, /\.css$/, /\.json$/],
loader: 'file-loader',
query: {
name: '[hash].[ext]',
outputPath: 'media/',
publicPath: 'build/'
}
}
]
} as webpack.NewModule,
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist/build')
},
plugins: [
new webpack.DefinePlugin(envKeys),
// pack common chunks
new webpack.optimize.CommonsChunkPlugin({
chunks: ['background_script', 'content_script'],
minChunks: 2,
name: 'common_for_scripts'
}),
new webpack.optimize.CommonsChunkPlugin({
chunks: ['popup', 'options'],
minChunks: 2,
name: 'common_for_ui'
}),
// exclude locale files in moment
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// copy files in public to dist
new CopyWebpackPlugin([
{
context: 'public',
from: {
dot: false,
glob: '**/*'
},
to: path.join(__dirname, 'dist/')
}
])
],
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx']
}
}
if (isProd()) {
// Production build tweaks
buildConfig.devtool = false
buildConfig.plugins = (buildConfig.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify('production') }
}),
// clean output files
new CleanWebpackPlugin(['dist']),
// minify
new webpack.optimize.UglifyJsPlugin()
])
} else {
// Development build tweaks
const buildConfigModule = buildConfig.module as webpack.NewModule
buildConfigModule.rules = (buildConfigModule.rules || []).concat([
// tslint
{
enforce: 'pre',
exclude: /node_modules/,
loader: 'tslint-loader',
test: /\.tsx?$/
}
])
buildConfig.plugins = (buildConfig.plugins || []).concat([
// exclude source mapping for vendor libs
new webpack.SourceMapDevToolPlugin({
exclude: /^vendor.*.\.js$/,
filename: '[file].map'
})
])
}
export default buildConfig