forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
184 lines (175 loc) · 5.32 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
// @ts-check
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const webpack = require('webpack')
const logger = require('gulplog')
const mode = process.env.NODE_ENV === 'production' ? 'production' : 'development'
logger.info('Using mode', mode)
const devtool = mode === 'production' ? 'source-map' : 'cheap-module-eval-source-map'
const rootDir = path.resolve(__dirname, '..')
const nodeModulesPath = path.resolve(__dirname, '..', 'node_modules')
const monacoEditorPaths = [path.resolve(nodeModulesPath, 'monaco-editor')]
const isEnterpriseBuild = !!process.env.ENTERPRISE
const enterpriseDir = path.resolve(__dirname, 'src', 'enterprise')
/** @type {import('webpack').Configuration} */
const config = {
context: __dirname, // needed when running `gulp webpackDevServer` from the root dir
mode,
optimization: {
minimize: mode === 'production',
minimizer: [
new TerserPlugin({
sourceMap: true,
terserOptions: {
compress: {
// // Don't inline functions, which causes name collisions with uglify-es:
// https://github.com/mishoo/UglifyJS2/issues/2842
inline: 1,
},
},
}),
],
namedModules: false,
...(mode === 'development'
? {
removeAvailableModules: false,
removeEmptyChunks: false,
splitChunks: false,
}
: {}),
},
entry: {
// Enterprise vs. OSS builds use different entrypoints. The enterprise entrypoint imports a
// strict superset of the OSS entrypoint.
app: [
'react-hot-loader/patch',
isEnterpriseBuild ? path.join(enterpriseDir, 'main.tsx') : path.join(__dirname, 'src', 'main.tsx'),
],
'editor.worker': 'monaco-editor/esm/vs/editor/editor.worker.js',
'json.worker': 'monaco-editor/esm/vs/language/json/json.worker',
},
output: {
path: path.join(rootDir, 'ui', 'assets'),
filename: 'scripts/[name].bundle.js',
chunkFilename: 'scripts/[id]-[contenthash].chunk.js',
publicPath: '/.assets/',
globalObject: 'self',
pathinfo: false,
},
devtool,
plugins: [
// Needed for React
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(mode),
},
}),
new MiniCssExtractPlugin({ filename: 'styles/[name].bundle.css' }),
new OptimizeCssAssetsPlugin(),
new MonacoWebpackPlugin({
languages: ['json'],
features: [
'bracketMatching',
'clipboard',
'coreCommands',
'cursorUndo',
'find',
'format',
'hover',
'inPlaceReplace',
'iPadShowKeyboard',
'links',
'suggest',
],
}),
new webpack.IgnorePlugin(/\.flow$/, /.*/),
],
resolve: {
extensions: ['.mjs', '.ts', '.tsx', '.js'],
mainFields: ['es2015', 'module', 'browser', 'main'],
alias: {
// react-visibility-sensor's main field points to a UMD bundle instead of ESM
// https://github.com/joshwnj/react-visibility-sensor/issues/148
'react-visibility-sensor': path.resolve(
__dirname,
'../node_modules/react-visibility-sensor/visibility-sensor.js'
),
},
},
module: {
rules: [
// Run hot-loading-related Babel plugins on our application code only (because they'd be
// slow to run on all JavaScript code).
{
test: /\.[jt]sx?$/,
include: path.join(__dirname, 'src'),
use: [
...(mode === 'production' ? ['thread-loader'] : []),
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
plugins: [
'react-hot-loader/babel',
[
'@sourcegraph/babel-plugin-transform-react-hot-loader-wrapper',
{
modulePattern: 'web/src/.*\\.tsx$',
componentNamePattern: '(Page|Area)$',
},
],
],
},
},
],
},
{
test: /\.[jt]sx?$/,
exclude: path.join(__dirname, 'src'),
use: [
...(mode === 'production' ? ['thread-loader'] : []),
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
configFile: path.join(__dirname, 'babel.config.js'),
},
},
],
},
{
test: /\.(sass|scss)$/,
use: [
mode === 'production' ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
config: {
path: __dirname,
},
},
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: [nodeModulesPath],
},
},
},
],
},
{
// CSS rule for monaco-editor and other external plain CSS (skip SASS and PostCSS for build perf)
test: /\.css$/,
include: monacoEditorPaths,
use: ['style-loader', 'css-loader'],
},
],
},
}
module.exports = config