-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathwebpack.config.js
More file actions
198 lines (186 loc) · 4.47 KB
/
webpack.config.js
File metadata and controls
198 lines (186 loc) · 4.47 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const ForkTsCheckerNotifierWebpackPlugin = require('fork-ts-checker-notifier-webpack-plugin')
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
const ProgressBarPlugin = require('progress-bar-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const WebpackNotifierPlugin = require('webpack-notifier')
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const { NODE_ENV, PORT } = process.env
const isDevelopment = NODE_ENV === 'development'
const isStaging = NODE_ENV === 'staging'
const isProduction = NODE_ENV === 'production'
const isDeploy = isStaging || isProduction
const config = {
devtool: 'cheap-module-eval-source-map',
entry: {
webpack: [
'webpack-hot-middleware/client?reload=true',
'./src/client/apps/webpack/client.js'
],
...getEntrypoints()
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'public/assets'),
publicPath: '/assets',
sourceMapFilename: '[file].map?[contenthash]'
},
module: {
rules: [
{
test: /\.coffee$/,
include: /src/,
loader: 'coffee-loader'
},
{
test: /\.jade$/,
include: /src/,
loader: 'jade-loader'
},
{
test: /\.(js|ts)x?$/,
include: /src/,
use: [
{
loader: 'babel-loader',
query: {
cacheDirectory: true
}
}
]
},
{
test: /\.json$/,
include: /src/,
loader: 'json-loader'
},
{
test: /\.css$/,
include: /src/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' }
]
},
{
test: /\.styl$/,
include: /src/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
{ loader: 'stylus-loader' }
]
}
]
},
plugins: [
new ForkTsCheckerWebpackPlugin({
formatter: 'codeframe',
formatterOptions: 'highlightCode',
tslint: false,
checkSyntacticErrors: true,
watch: ['./src']
}),
new ForkTsCheckerNotifierWebpackPlugin({
excludeWarnings: true,
skipFirstNotification: true
}),
new FriendlyErrorsWebpackPlugin({
clearConsole: false,
compilationSuccessInfo: {
messages: [`[Positron] Listening on http://localhost:${PORT} \n`]
}
}),
new ProgressBarPlugin(),
new WebpackNotifierPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(NODE_ENV)
}
}),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery',
'window.jQuery': 'jquery'
})
],
resolve: {
extensions: [
'.mjs',
'.coffee',
'.js',
'.jsx',
'.json',
'.styl',
'.ts',
'.tsx'
],
modules: [
'node_modules',
'src'
],
symlinks: false
}
}
// Development
if (isDevelopment) {
config.plugins.push(new webpack.HotModuleReplacementPlugin())
// Staging
} else if (isDeploy) {
config.devtool = 'source-map'
// Prod
if (isProduction) {
config.plugins.push(
new UglifyJsPlugin({
cache: true,
sourceMap: true,
uglifyOptions: {
compress: {
warnings: false
}
}
})
)
}
}
// Helpers
function getEntrypoints () {
return findAssets('src/client/assets')
}
function findAssets (basePath) {
const files = fs.readdirSync(path.join(process.cwd(), basePath))
// Filter out .styl files
const validAssets = (file) => {
const whitelist = [ '.js', '.coffee' ]
const isValid = whitelist.some(extension => extension === path.extname(file))
return isValid
}
/**
* Construct key/value pairs representing Webpack entrypoints; e.g.,
* { desktop: [ path/to/subapp.asset.js ] }
*/
const assets = files
.filter(validAssets)
.reduce((assetMap, file) => {
const fileName = path.basename(file, path.extname(file))
const asset = {
[fileName]: [
path.join(__dirname, basePath, file)
]
}
if (isDevelopment) {
asset[fileName].unshift(
'webpack-hot-middleware/client?reload=true'
)
}
return {
...assetMap,
...asset
}
}, {})
return assets
}
module.exports = config