forked from getsentry/sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
299 lines (281 loc) · 8.54 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*eslint-env node*/
/*eslint no-var:0*/
var path = require('path'),
fs = require('fs'),
webpack = require('webpack'),
ExtractTextPlugin = require('extract-text-webpack-plugin'),
LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
var staticPrefix = 'src/sentry/static/sentry',
distPath = path.join(__dirname, staticPrefix, 'dist');
// this is set by setup.py sdist
if (process.env.SENTRY_STATIC_DIST_PATH) {
distPath = process.env.SENTRY_STATIC_DIST_PATH;
}
var IS_PRODUCTION = process.env.NODE_ENV === 'production';
var IS_TEST = process.env.NODE_ENV === 'TEST' || process.env.TEST_SUITE;
var REFRESH = process.env.WEBPACK_LIVERELOAD === '1';
var babelConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '.babelrc')));
babelConfig.cacheDirectory = true;
// only extract po files if we need to
if (process.env.SENTRY_EXTRACT_TRANSLATIONS === '1') {
babelConfig.plugins.push([
'babel-gettext-extractor',
{
fileName: 'build/javascript.po',
baseDirectory: path.join(__dirname, 'src/sentry'),
functionNames: {
gettext: ['msgid'],
ngettext: ['msgid', 'msgid_plural', 'count'],
gettextComponentTemplate: ['msgid'],
t: ['msgid'],
tn: ['msgid', 'msgid_plural', 'count'],
tct: ['msgid']
}
}
]);
}
var appEntry = {
app: 'app',
vendor: [
'babel-polyfill',
'bootstrap/js/dropdown',
'bootstrap/js/tab',
'bootstrap/js/tooltip',
'bootstrap/js/alert',
'crypto-js/md5',
'jed',
'jquery',
'marked',
'moment',
'moment-timezone',
'raven-js',
'react',
'react-dom',
'react-dom/server',
'react-document-title',
'react-router',
'react-bootstrap/lib/Modal',
'react-sparklines',
'reflux',
'select2',
'vendor/simple-slider/simple-slider',
'ios-device-list'
]
};
// dynamically iterate over locale files and add to `entry` appConfig
var localeCatalogPath = path.join(__dirname, 'src', 'sentry', 'locale', 'catalogs.json');
var localeCatalog = JSON.parse(fs.readFileSync(localeCatalogPath, 'utf8'));
var localeEntries = [];
localeCatalog.supported_locales.forEach(function(locale) {
if (locale === 'en') return;
// Django locale names are "zh_CN", moment's are "zh-cn"
var normalizedLocale = locale.toLowerCase().replace('_', '-');
appEntry['locale/' + normalizedLocale] = [
'moment/locale/' + normalizedLocale,
'sentry-locale/' + locale + '/LC_MESSAGES/django.po' // relative to static/sentry
];
localeEntries.push('locale/' + normalizedLocale);
});
/**
* Main Webpack config for Sentry React SPA.
*/
var appConfig = {
entry: appEntry,
context: path.join(__dirname, staticPrefix),
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
include: path.join(__dirname, staticPrefix),
exclude: /(vendor|node_modules|dist)/,
query: babelConfig
},
{
test: /\.po$/,
loader: 'po-catalog-loader',
query: {
referenceExtensions: ['.js', '.jsx'],
domain: 'sentry'
}
},
{
test: /\.json$/,
loader: 'json-loader'
},
// loader for dynamic styles imported into components (embedded as js)
{
test: /\.less$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader' + (IS_PRODUCTION ? '?minimize=true' : '')
},
{
loader: 'less-loader'
}
]
},
{
test: /\.(woff|woff2|ttf|eot|svg|png|gif|ico|jpg)($|\?)/,
loader: 'file-loader?name=' + '[name].[ext]'
}
],
noParse: [
// don't parse known, pre-built javascript files (improves webpack perf)
/dist\/jquery\.js/,
/jed\/jed\.js/,
/marked\/lib\/marked\.js/
]
},
plugins: [
new LodashModuleReplacementPlugin({
collections: true,
currying: true, // these are enabled to support lodash/fp/ features
flattening: true // used by a dependency of react-mentions
}),
new webpack.optimize.CommonsChunkPlugin({
names: localeEntries.concat(['vendor']) // 'vendor' must be last entry
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
'root.jQuery': 'jquery',
Raven: 'raven-js'
}),
new ExtractTextPlugin('[name].css'),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), // ignore moment.js locale files
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
IS_PERCY: JSON.stringify(
process.env.CI && !!process.env.PERCY_TOKEN && !!process.env.TRAVIS
)
}
}),
// restrict translation files pulled into dist/app.js to only those specified
// in locale/catalogs.json
new webpack.ContextReplacementPlugin(
/locale$/,
path.join(__dirname, 'src', 'sentry', 'locale', path.sep),
true,
new RegExp('(' + localeCatalog.supported_locales.join('|') + ')\/.*\\.po$')
)
],
resolve: {
alias: {
'sentry-locale': path.join(__dirname, 'src', 'sentry', 'locale'),
'integration-docs-platforms': IS_TEST
? path.join(__dirname, 'tests/fixtures/_platforms.json')
: path.join(__dirname, 'src/sentry/integration-docs/_platforms.json')
},
modules: [path.join(__dirname, staticPrefix), 'node_modules'],
extensions: ['.less', '.jsx', '.js', '.json']
},
output: {
path: distPath,
filename: '[name].js',
libraryTarget: 'var',
library: 'exports',
sourceMapFilename: '[name].js.map'
},
devtool: IS_PRODUCTION ? '#source-map' : '#cheap-source-map'
};
/**
* Webpack entry for password strength checker
*/
var pwConfig = {
entry: {
pwstrength: './index'
},
context: path.resolve(path.join(__dirname, staticPrefix), 'js', 'pwstrength'),
module: {},
plugins: [],
resolve: {
modules: [path.join(__dirname, staticPrefix), 'node_modules'],
extensions: ['.js']
},
output: {
path: distPath,
filename: '[name].js',
libraryTarget: 'window',
library: 'sentrypw',
sourceMapFilename: '[name].js.map'
},
devtool: IS_PRODUCTION ? '#source-map' : '#cheap-source-map'
};
/**
* Legacy CSS Webpack appConfig for Django-powered views.
* This generates a single "sentry.css" file that imports ALL component styles
* for use on Django-powered pages.
*/
var legacyCssConfig = {
entry: {
sentry: 'less/sentry.less'
},
context: path.join(__dirname, staticPrefix),
output: {
path: distPath,
filename: '[name].css'
},
plugins: [new ExtractTextPlugin('[name].css')],
resolve: {
extensions: ['.less'],
modules: [path.join(__dirname, staticPrefix), 'node_modules']
},
module: {
rules: [
{
test: /\.less$/,
include: path.join(__dirname, staticPrefix),
loader: ExtractTextPlugin.extract({
fallbackLoader: 'style-loader?sourceMap=false',
loader: 'css-loader' + (IS_PRODUCTION ? '?minimize=true' : '') + '!less-loader'
})
},
{
test: /\.(woff|woff2|ttf|eot|svg|png|gif|ico|jpg)($|\?)/,
loader: 'file-loader?name=' + '[name].[ext]'
}
]
}
};
var minificationPlugins = [
// This compression-webpack-plugin generates pre-compressed files
// ending in .gz, to be picked up and served by our internal static media
// server as well as nginx when paired with the gzip_static module.
new (require('compression-webpack-plugin'))({
algorithm: function(buffer, options, callback) {
require('zlib').gzip(buffer, callback);
},
regExp: /\.(js|map|css|svg|html|txt|ico|eot|ttf)$/
}),
// Disable annoying UglifyJS warnings that pollute Travis log output
// NOTE: This breaks -p in webpack 2. Must call webpack w/ NODE_ENV=production for minification.
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
// https://github.com/webpack/webpack/blob/951a7603d279c93c936e4b8b801a355dc3e26292/bin/convert-argv.js#L442
sourceMap: appConfig.devtool &&
(appConfig.devtool.indexOf('sourcemap') >= 0 ||
appConfig.devtool.indexOf('source-map') >= 0)
})
];
if (!IS_PRODUCTION && REFRESH) {
appConfig.plugins.push(
new (require('webpack-livereload-plugin'))({appendScriptTag: true})
);
}
if (IS_PRODUCTION) {
// NOTE: can't do plugins.push(Array) because webpack/webpack#2217
minificationPlugins.forEach(function(plugin) {
appConfig.plugins.push(plugin);
pwConfig.plugins.push(plugin);
legacyCssConfig.plugins.push(plugin);
});
}
module.exports = [appConfig, legacyCssConfig, pwConfig];