This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwebpack.config.js
250 lines (214 loc) · 6.37 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
/* @flow */
/* eslint-disable github/no-flowfixme */
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const {getGraphQLProjectConfig} = require('graphql-config')
const {EnvironmentPlugin, optimize} = require('webpack')
const BabelMinifyPlugin = require('babel-minify-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const RelayCompilerWebpackPlugin = require('relay-compiler-webpack-plugin')
/*::
type Options = {|
commonChunkName?: string,
entries?: string[],
graphqlProxyPath?: string,
historyApiFallback?: boolean,
maxAssetSize?: number,
maxEntrypointSize?: number,
outputPath?: string,
srcRoot?: string,
staticRoot?: string,
template?: string,
|}
*/
/*::
type InternalOptions = {|
commonChunkName: string,
entries: string[],
graphqlProxyPath: string,
historyApiFallback: boolean,
maxAssetSize: number,
maxEntrypointSize: number,
outputPath: string,
srcRoot: string,
staticRoot: string,
template: string
|}
*/
const defaultOptions /*: InternalOptions */ = {
commonChunkName: 'common',
entries: ['index'],
graphqlProxyPath: '/graphql',
historyApiFallback: true,
maxAssetSize: 200000, // 200 kB
maxEntrypointSize: 500000, // 500 kB
outputPath: './dist',
srcRoot: './src',
staticRoot: './public',
template: path.resolve(__dirname, 'index.html')
}
module.exports = (env /*: string */ = 'development', options /*: Options */) => {
// $FlowFixMe: Forcibly cast Options to InternalOptions type after initializing default values.
const opts /*: InternalOptions */ = Object.assign({}, defaultOptions, options)
const cwd = process.cwd()
const config = {}
if (env === 'production') {
config.performance = {
hints: 'error',
maxAssetSize: opts.maxAssetSize,
maxEntrypointSize: opts.maxEntrypointSize
}
} else {
config.performance = false
}
config.entry = {}
for (const name of opts.entries) {
config.entry[name] = path.resolve(cwd, opts.srcRoot, `${name}.js`)
}
const rootIndexPath = path.resolve(cwd, './index.js')
if (fs.existsSync(rootIndexPath)) {
config.entry.index = rootIndexPath
}
config.output = {}
config.output.filename = '[name].bundle.js'
config.output.path = path.resolve(cwd, opts.outputPath)
if (opts.historyApiFallback) {
config.output.publicPath = '/'
}
// TODO: Fix source-map option in production environment
config.devtool = env === 'production' ? false /* 'source-map' */ : 'inline-source-map'
config.devServer = {}
if (opts.historyApiFallback) {
const rewrites = opts.entries.map(entry => {
return {from: `/${entry}`, to: `/${entry}.html`}
})
config.devServer.historyApiFallback = {rewrites}
}
config.devServer.proxy = {}
config.devServer.proxy = proxyConfig(opts.graphqlProxyPath)
config.plugins = [
new EnvironmentPlugin({
GRAPHQL_CONFIG_ENDPOINT_NAME: '',
NODE_ENV: env
}),
new CleanWebpackPlugin([path.resolve(cwd, opts.outputPath)], {root: cwd}),
new ExtractTextPlugin({
filename: '[name].bundle.css'
})
]
if (opts.staticRoot && fs.existsSync(opts.staticRoot)) {
config.devServer.contentBase = opts.staticRoot
config.plugins.push(new CopyWebpackPlugin([{from: opts.staticRoot}]))
}
if (opts.entries.length > 1) {
config.plugins = config.plugins.concat([
new optimize.CommonsChunkPlugin({
name: opts.commonChunkName
})
])
}
const {config: graphqlConfig} = tryGetGraphQLProjectConfig()
if (graphqlConfig && graphqlConfig.schemaPath) {
config.plugins = config.plugins.concat([
new RelayCompilerWebpackPlugin({
schema: path.resolve(cwd, graphqlConfig.schemaPath),
src: path.resolve(cwd, opts.srcRoot),
watchman: false,
reporter: {
reportError: (caughtLocation, error) => {
error.message = chalk.red(error.message)
if (env === 'production') {
throw error
} else {
process.stdout.write(error.message)
}
}
}
})
])
}
config.plugins = config.plugins.concat(
Object.keys(config.entry).map(
entry =>
new HtmlWebpackPlugin({
filename: `${entry}.html`,
chunks: [opts.commonChunkName, entry],
template: opts.template
})
)
)
if (env === 'production') {
config.plugins = config.plugins.concat([
new BabelMinifyPlugin(),
new CompressionPlugin({
test: /\.(js|css)$/
})
])
}
const cssLoader = {
loader: 'css-loader',
options: {}
}
const cssLoaders = [cssLoader]
const postCSSConfig = path.resolve(cwd, 'postcss.config.js')
if (fs.existsSync(postCSSConfig)) {
cssLoader.options.importLoaders = 1
cssLoaders.push({
loader: 'postcss-loader'
})
}
config.module = {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.css$/,
// exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: cssLoaders
})
}
]
}
return config
}
// TODO: Investigate other ways set a default GRAPHQL_CONFIG_ENDPOINT_NAME
// for getGraphQLProjectConfig without mutating the environment.
if (!process.env['GRAPHQL_CONFIG_ENDPOINT_NAME']) {
process.env['GRAPHQL_CONFIG_ENDPOINT_NAME'] = 'production'
}
// Get webpack proxy configuration as per .graphqlconfig.
function proxyConfig(path) {
const config = {}
const {endpointsExtension} = tryGetGraphQLProjectConfig()
if (endpointsExtension) {
const graphqlEndpoint = endpointsExtension.getEndpoint()
const {url: target, headers} = graphqlEndpoint
const changeOrigin = true
const pathRewrite = {}
pathRewrite[`^${path}`] = ''
config[path] = {changeOrigin, headers, pathRewrite, target}
}
return config
}
// TODO: Find a better way to attempt to load GraphQL config without erroring
function tryGetGraphQLProjectConfig() {
try {
return getGraphQLProjectConfig()
} catch (error) {
if (error.name === 'ConfigNotFoundError') {
return {}
} else {
throw error
}
}
}