-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
279 lines (257 loc) · 7.72 KB
/
webpack.config.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const path = require('path')
const process = require('process')
const webpack = require('webpack')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = () => {
const syntaxes = process.env.SYNTAXES
? process.env.SYNTAXES.split(',').map(item => item.trim())
: [
'text',
'c_cpp',
'csharp',
'golang',
'java',
'javascript',
'php',
'perl',
'python',
'ruby',
'rust',
'css',
'html',
'objectivec',
'swift',
'clojure',
'lisp',
'haskell',
'scala',
'scheme',
'actionscript',
'ada',
'apache_conf',
'asciidoc',
'assembly_x86',
'batchfile',
'cobol',
'coffee',
'd',
'dart',
'diff',
'dockerfile',
'dot',
'ejs',
'elixir',
'elm',
'erlang',
'fortran',
'gitignore',
'glsl',
'gobstones',
'graphqlschema',
'groovy',
'haml',
'handlebars',
'haxe',
'hjson',
'ini',
'jade',
'json',
'jsp',
'jsx',
'julia',
'kotlin',
'latex',
'less',
'livescript',
'lua',
'makefile',
'markdown',
'matlab',
'mel',
'mysql',
'nix',
'nsis',
'ocaml',
'pascal',
'pgsql',
'powershell',
'prolog',
'protobuf',
'r',
'rdoc',
'rst',
'sass',
'scad',
'scss',
'sh',
'sjs',
'smarty',
'sql',
'stylus',
'svg',
'tcl',
'tex',
'textile',
'toml',
'tsx',
'twig',
'typescript',
'vala',
'vbscript',
'verilog',
'vhdl',
'xml',
'yaml',
'django']
const assetsPath = process.env.ASSETS_PATH == null
? '/'
: process.env.ASSETS_PATH
const conf = {
// Assume we are targeting production environments by default; the value
// could be overridden via '--mode' CLI argument.
mode: 'production',
// Expose source map even for production because XSnippet is an Open Source
// project and we have no intentions to hide its internals while being able
// to debug production is pretty valuable.
devtool: 'source-map',
devServer: {
historyApiFallback: true,
proxy: {
'/_api': {
target: 'http://localhost:8000',
pathRewrite: { '^/_api': '' },
headers: { Host: 'localhost:8000/_api' },
},
'/_web-backend': {
target: 'http://localhost:8001',
pathRewrite: { '^/_web-backend': '' },
},
},
},
entry: {
app: path.resolve(__dirname, 'src', 'index.tsx'),
},
// Use [chunkhash] in order to invalidate browsers cache on new deployments.
output: {
filename: '[name].[chunkhash].js',
chunkFilename: '[name].[chunkhash].js',
path: path.resolve(__dirname, 'dist'),
publicPath: assetsPath,
},
module: {
rules: [
// Transpile ES2015+ and ts to ES5.
{
test: /\.(t|j)sx?$/,
include: path.resolve(__dirname, 'src'),
use: ['babel-loader'],
},
// Transpile Stylus down to CSS, resolving url() inside and enabling
// CSS modules, so CSS classes can't be accessed within JSX sources.
{
test: /\.(styl|css)$/,
use: [
{ loader: MiniCssExtractPlugin.loader },
{
loader: 'css-loader',
options: {
modules: false,
minimize: true,
// Enable source maps if they are specified in devtool
// option. God-Knows-Why css-loader doesn't check devtool
// value in order to initialize its sourceMap value, hence
// this line.
sourceMap: true,
},
},
{ loader: 'stylus-loader' },
],
},
// Just copy these files to output "As Is", if they are imported from
// JSX sources or encountered inside CSS.
{
test: /\.(png|svg|jpg|woff|svg|ttf|woff2|eot)$/,
include: path.resolve(__dirname, 'src'),
use: ['file-loader'],
},
{
test: /\.(jpg|png|gif|svg)$/,
loader: 'image-webpack-loader',
},
],
},
optimization: {
// Enable split chunks logic for everything, including entry chunks,
// because we want vendors to be separated in either case. The key idea
// here is that vendors are rarely changed; therefore, they are good
// candidates to be cached on clients.
splitChunks: {
chunks: 'all',
cacheGroups: {
// AceEditor's modes (aka syntaxes) are pretty heavy, and since they
// are not essential, we better download them asynchronously when
// the app is loaded. First step to accomplish this goal is to
// create a separate bundle by defining a new cache group.
syntaxes: {
test: /[\\/]brace[\\/]mode[\\/]/,
name: 'syntaxes',
priority: -5,
},
},
},
},
plugins: [
// Worker is a sort of background linter integrated in AceEditor that
// can show errors for some syntaxes (e.g. JavaScript or XML). It's
// pretty heavy (~1Mb) and we have no plans to use it, so we just
// aggressively strip this code out of build.
new webpack.IgnorePlugin(/worker/, /brace/),
// Webpack, when meets dynamic imports with variables, heuristically
// figures out what needs to be bundle and bundles everything it can
// reach to. In our case we have a clear understanding which syntaxes
// we want to distribute within the application, so let's strip
// everything else out.
//
// https://webpack.js.org/api/module-methods/#import-<Paste>
new webpack.IgnorePlugin(
new RegExp(`/(?!(?:${syntaxes.join('|')}).js$).*js$`),
/brace[\\/]mode/,
),
// Each time we change something, a new version of bundled assets is
// produced. Since we use hash in filenames in order to invalidate cache
// on change, we end up having multiple outdated copies in output
// directory. Let's cleanup it before produce a fresh build.
new CleanWebpackPlugin([path.resolve(__dirname, 'dist')]),
// Propagate (and set) environment variables down to the application. We
// use them to configure application behaviour. Please note, 'null' here
// means 'unset'.
new webpack.EnvironmentPlugin({
API_BASE_URI: null,
RAW_SNIPPET_URI_FORMAT: null,
RUNTIME_CONF_URI: `${assetsPath}conf.json`,
SYNTAXES: syntaxes,
}),
// Similar to JavaScript, we use [chunkhash] in order to invalidate
// browsers cache on new deployments.
new MiniCssExtractPlugin({
filename: '[name].[chunkhash].css',
}),
// Generate index.html based on passed template, populating it with
// produced JavaScript bundles.
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src', 'index.html'),
favicon: path.resolve(__dirname, 'src', 'assets', 'icons', 'favicon.ico'),
}),
],
// Enable importing .js & .jsx & .ts & .tsx files without specifying their extensions.
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
node: {
net: 'empty',
},
}
return conf
}