-
Notifications
You must be signed in to change notification settings - Fork 48
/
client configuration.js
231 lines (195 loc) · 7.1 KB
/
client configuration.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
import util from 'util'
import chunks_plugin from './chunks plugin'
import { clone, starts_with } from './helpers'
import
{
find_loader,
get_style_rules,
normalize_configuration_rule_loaders
}
from './loaders'
export default function client_configuration(webpack_configuration, settings, options = {})
{
const configuration = clone(webpack_configuration)
configuration.plugins = configuration.plugins || []
configuration.plugins.push
(
// Add chunk filename info plugin
//
// Writes client-side build chunks filename info
// for later use inside server-side rendering code
// (`<script src=.../>` and `<link rel="style" href=.../>` tags)
//
// Cloning Webpack configuration here
// because `webpack-dev-server` seems to alter it
// by changing the already predefined `.output.path`.
//
new chunks_plugin(clone(configuration), { silent: settings.silent, chunk_info_filename: settings.chunk_info_filename })
)
// CSS bundle filename (if specified)
const css_bundle = options.css_bundle || options.cssBundle
let css_bundle_filename = '[name]-[contenthash].css'
if (typeof css_bundle === 'string')
{
css_bundle_filename = css_bundle
}
// If it's a client-side development webpack build,
// and CSS bundle extraction is enabled,
// then extract all CSS styles into a file.
// (without removing them from the code)
if (options.development && css_bundle)
{
// Extract styles into a file
// (without removing them from the code in this case).
//
// It copies contents of each `require("style.css")`
// into one big CSS file on disk
// which will be later read on the server-side
// and inserted into `<head><style></style></head>`,
// so that in development mode there's no
// "flash of unstyled content" on page reload.
//
const extract_css_plugin = create_extract_css_plugin(css_bundle_filename, options.useMiniCssExtractPlugin)
// Normalize `modules.rules` loaders.
normalize_configuration_rule_loaders(configuration)
// Find all rules using `style-loader`
// and replace `style-loader` with `extract-text-webpack-plugin`
// or `mini-css-extract-plugin` loader.
for (const rule of get_style_rules(configuration))
{
const style_loader = find_loader(rule, 'style-loader')
const before_style_loader = rule.use.slice(0, rule.use.indexOf(style_loader))
const after_style_loader = rule.use.slice(rule.use.indexOf(style_loader) + 1)
if (before_style_loader.length > 0)
{
throw new Error('No loaders can preceed `style-loader` in a Webpack module rule.', util.inspect(rule))
}
rule.use = generate_extract_css_loaders(after_style_loader, options.development, extract_css_plugin, options.useMiniCssExtractPlugin)
}
// Add the `extract-text-webpack-plugin` or
// `mini-css-extract-plugin` to the list of plugins.
// It will extract all CSS into a file
// (without removing it from the code in this case)
configuration.plugins.push(extract_css_plugin)
}
// Use `extract-text-webpack-plugin`
// or `mini-css-extract-plugin`
// to extract all CSS into a separate file
// (in production)
if (options.development === false && css_bundle !== false)
{
// Extract styles into a file
// (removing them from the code in this case).
//
// It moves contents of each `require("style.css")`
// into one big CSS file on disk
// which will be later read on the server-side
// and inserted into `<head><style></style></head>`.
//
const extract_css_plugin = create_extract_css_plugin(css_bundle_filename, options.useMiniCssExtractPlugin)
// Normalize `modules.rules` loaders.
normalize_configuration_rule_loaders(configuration)
// Find module loaders with `style-loader`,
// and set those module loaders to `extract-text-webpack-plugin`
// or `mini-css-extract-plugin` loader
for (const rule of get_style_rules(configuration))
{
const style_loader = find_loader(rule, 'style-loader')
// const style_loader_and_before = rule.use.slice(0, rule.use.indexOf(style_loader) + 1)
const before_style_loader = rule.use.slice(0, rule.use.indexOf(style_loader))
const after_style_loader = rule.use.slice(rule.use.indexOf(style_loader) + 1)
if (before_style_loader.length > 0)
{
throw new Error('No loaders can preceed `style-loader` in a Webpack module rule.', util.inspect(rule))
}
rule.use = generate_extract_css_loaders(after_style_loader, options.development, extract_css_plugin, options.useMiniCssExtractPlugin)
}
// Add the `extract-text-webpack-plugin` or
// `mini-css-extract-plugin` to the list of plugins.
// It will extract all CSS into a file
// (removing it from the code in this case)
configuration.plugins.push(extract_css_plugin)
}
// Done
return configuration
}
/**
* Creates an instance of plugin for extracting styles in a file.
* Either `extract-text-webpack-plugin` or `mini-css-extract-plugin`.
*/
function create_extract_css_plugin(css_bundle_filename, useMiniCssExtractPlugin)
{
if (useMiniCssExtractPlugin)
{
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
return new MiniCssExtractPlugin
({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename : css_bundle_filename
})
}
const ExtractTextPlugin = require('extract-text-webpack-plugin')
// "allChunks: true" option means that the styles from all chunks
// (think "entry points") will be extracted into a single big CSS file.
//
return new ExtractTextPlugin
({
filename : css_bundle_filename,
allChunks : true
})
}
/**
* Generates rule.use loaders for extracting styles in a file.
* Either for `extract-text-webpack-plugin` or `mini-css-extract-plugin`.
*/
function generate_extract_css_loaders(after_style_loader, development, extract_css_plugin, useMiniCssExtractPlugin)
{
let extract_css_loaders
if (useMiniCssExtractPlugin)
{
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
return [{
loader: MiniCssExtractPlugin.loader
},
...after_style_loader]
}
// The first argument to the .extract() function is the name of the loader
// ("style-loader" in this case) to be applied to non-top-level-chunks in case of "allChunks: false" option.
// since in this configuration "allChunks: true" option is used, this first argument is irrelevant.
//
// `remove: false` ensures that the styles being extracted
// aren't erased from the chunk javascript file.
//
const extract_css_loader = extract_css_plugin.extract
({
remove : development ? false : true,
// `fallback` option is not really being used
// because `allChunks: true` option is used.
// fallback : before_style_loader,
use : after_style_loader
})
// Workaround for an old bug, may be obsolete now.
// https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/368
if (Array.isArray(extract_css_loader))
{
extract_css_loaders = extract_css_loader
}
else
{
extract_css_loaders =
[{
loader: extract_css_loader
}]
}
// I'm also prepending another `style-loader` here
// to re-enable adding these styles to the <head/> of the page on-the-fly.
if (development)
{
return [{
loader: 'style-loader'
},
...extract_css_loaders]
}
return extract_css_loaders
}