Skip to content

Commit 915b22f

Browse files
committed
update webpack loaders/plugins
1 parent 012ac51 commit 915b22f

5 files changed

Lines changed: 109 additions & 85 deletions

File tree

packages/gatsby/cache-dir/develop-static-entry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export default (pagePath, callback) => {
104104
body: ``,
105105
headComponents: headComponents.concat([
106106
<script key={`io`} src="/socket.io/socket.io.js" />,
107+
<link key="styles" rel="stylesheet" href="/commons.css" />,
107108
]),
108109
htmlAttributes,
109110
bodyAttributes,

packages/gatsby/cache-dir/static-entry.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ export default (pagePath, callback) => {
301301
return { rel: `preload`, name: chunk }
302302
})
303303

304-
namedChunkGroups[s].assets.forEach(asset =>
305-
chunks.push({ rel: `preload`, name: asset })
306-
)
304+
namedChunkGroups[s].assets.forEach(asset => {
305+
chunks.push({ rel: `preload`, name: asset.name })
306+
})
307307

308308
const childAssets = namedChunkGroups[s].childAssets
309309
for (const rel in childAssets) {

packages/gatsby/src/utils/webpack-plugins.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ import webpack, { Plugin } from "webpack"
22

33
const plugin = (
44
name: string,
5-
optimize?: boolean
6-
): ((...args: any) => Plugin) => {
5+
optimize?: boolean,
6+
deprecationReason?: string
7+
): ((...args: any) => Plugin) => (...args: any): Plugin => {
8+
if (deprecationReason) {
9+
// TODO add proper deprecation function to reporter
10+
console.warn(`[deprecated]: ${deprecationReason}`)
11+
}
12+
713
const WebpackPlugin = (optimize ? webpack.optimize : webpack)[name]
8-
return (...args: any): Plugin => new WebpackPlugin(...args)
14+
return new WebpackPlugin(...args)
915
}
1016

1117
export const builtinPlugins = {
@@ -26,7 +32,11 @@ export const builtinPlugins = {
2632
extendedAPI: plugin(`ExtendedAPIPlugin`),
2733
externals: plugin(`ExternalsPlugin`),
2834
jsonpTemplate: plugin(`JsonpTemplatePlugin`),
29-
libraryTemplate: plugin(`LibraryTemplatePlugin`),
35+
libraryTemplate: plugin(
36+
`LibraryTemplatePlugin`,
37+
false,
38+
`plugins.libraryTemplate is deprecated and has been replaced by compilation.outputOptions.library or compilation.addEntry + passing a library option`
39+
),
3040
loaderTarget: plugin(`LoaderTargetPlugin`),
3141
memoryOutputFile: plugin(`MemoryOutputFileSystem`),
3242
progress: plugin(`ProgressPlugin`),
@@ -45,7 +55,11 @@ export const builtinPlugins = {
4555
moduleFilenameH: plugin(`ModuleFilenameHelpers`),
4656

4757
aggressiveMerging: plugin(`AggressiveMergingPlugin`, true),
48-
aggressiveSplitting: plugin(`AggressiveSplittingPlugin`, true),
58+
aggressiveSplitting: plugin(
59+
`AggressiveSplittingPlugin`,
60+
true,
61+
`plugins.aggressiveSplitting is deprecated in favor of plugins.splitChunks.`
62+
),
4963
splitChunks: plugin(`SplitChunks`, true),
5064
chunkModuleIdRange: plugin(`ChunkModuleIdRangePlugin`, true),
5165
dedupe: plugin(`DedupePlugin`, true),

packages/gatsby/src/utils/webpack-utils.ts

Lines changed: 77 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import autoprefixer from "autoprefixer"
66
import flexbugs from "postcss-flexbugs-fixes"
77
import TerserPlugin from "terser-webpack-plugin"
88
import MiniCssExtractPlugin from "mini-css-extract-plugin"
9-
import OptimizeCssAssetsPlugin from "optimize-css-assets-webpack-plugin"
9+
// import OptimizeCssAssetsPlugin from "optimize-css-assets-webpack-plugin"
10+
import CssMinimizerPlugin from "css-minimizer-webpack-plugin"
1011
import ReactRefreshWebpackPlugin from "@pmmmwh/react-refresh-webpack-plugin"
1112
import { getBrowsersList } from "./browserslist"
12-
import semver from "semver"
13+
// import semver from "semver"
1314

1415
import { GatsbyWebpackStatsExtractor } from "./gatsby-webpack-stats-extractor"
1516
import { GatsbyWebpackEslintGraphqlSchemaReload } from "./gatsby-webpack-eslint-graphql-schema-reload-plugin"
@@ -40,17 +41,40 @@ type PluginFactory = (...args: any) => Plugin
4041

4142
type BuiltinPlugins = typeof builtinPlugins
4243

44+
type CSSModulesOptions =
45+
| boolean
46+
| string
47+
| {
48+
mode?: "local" | "global" | "pure" | Function
49+
auto?: boolean
50+
exportGlobals?: boolean
51+
localIdentName?: string
52+
localIdentContext?: string
53+
localIdentHashPrefix?: string
54+
namedExport?: boolean
55+
exportLocalsConvention?:
56+
| "asIs"
57+
| "camelCaseOnly"
58+
| "camelCase"
59+
| "dashes"
60+
| "dashesOnly"
61+
exportOnlyLocals?: boolean
62+
}
63+
4364
/**
4465
* Utils that produce webpack `loader` objects
4566
*/
4667
interface ILoaderUtils {
47-
json: LoaderResolver
4868
yaml: LoaderResolver
49-
null: LoaderResolver
50-
raw: LoaderResolver
51-
5269
style: LoaderResolver
53-
css: LoaderResolver
70+
css: LoaderResolver<{
71+
url?: boolean | Function
72+
import?: boolean | Function
73+
modules?: CSSModulesOptions
74+
sourceMap?: boolean
75+
importLoaders?: number
76+
esModule?: boolean
77+
}>
5478
postcss: LoaderResolver<{
5579
browsers?: Array<string>
5680
overrideBrowserslist?: Array<string>
@@ -161,16 +185,13 @@ export const createWebpackUtils = (
161185
return rule
162186
}
163187

164-
let ident = 0
165-
166188
const loaders: ILoaderUtils = {
167189
json: (options = {}) => {
168190
return {
169191
options,
170192
loader: require.resolve(`json-loader`),
171193
}
172194
},
173-
174195
yaml: (options = {}) => {
175196
return {
176197
options,
@@ -200,48 +221,42 @@ export const createWebpackUtils = (
200221
},
201222

202223
miniCssExtract: (options = {}) => {
203-
if (PRODUCTION) {
204-
// production always uses MiniCssExtractPlugin
205-
return {
206-
loader: MiniCssExtractPlugin.loader,
207-
options,
208-
}
209-
} else if (process.env.GATSBY_EXPERIMENTAL_DEV_SSR) {
210-
// develop with ssr also uses MiniCssExtractPlugin
211-
return {
212-
loader: MiniCssExtractPlugin.loader,
213-
options: {
214-
...options,
215-
// enable hmr for browser bundle, ssr bundle doesn't need it
216-
hmr: stage === `develop`,
217-
},
218-
}
219-
} else {
220-
// develop without ssr is using style-loader
221-
return {
222-
loader: require.resolve(`style-loader`),
223-
options,
224-
}
224+
// production always uses MiniCssExtractPlugin
225+
return {
226+
loader: MiniCssExtractPlugin.loader,
227+
options,
225228
}
226229
},
227230

228231
css: (options = {}) => {
232+
let modulesOptions: CSSModulesOptions = false
233+
if (options.modules) {
234+
modulesOptions = {
235+
auto: false,
236+
localIdentName: `[name]--[local]--[hash:base64:5]`,
237+
exportLocalsConvention: `dashesOnly`,
238+
exportOnlyLocals: isSSR,
239+
}
240+
241+
if (typeof options.modules === `object`) {
242+
modulesOptions = {
243+
...modulesOptions,
244+
...options.modules,
245+
}
246+
}
247+
}
248+
229249
return {
230-
loader: isSSR
231-
? require.resolve(`css-loader/locals`)
232-
: require.resolve(`css-loader`),
250+
loader: require.resolve(`css-loader`),
233251
options: {
234252
sourceMap: !PRODUCTION,
235-
camelCase: `dashesOnly`,
236-
// https://github.com/webpack-contrib/css-loader/issues/406
237-
localIdentName: `[name]--[local]--[hash:base64:5]`,
238-
...options,
253+
modules: modulesOptions,
239254
},
240255
}
241256
},
242257

243258
postcss: (options = {}) => {
244-
let {
259+
const {
245260
plugins,
246261
overrideBrowserslist = supportedBrowsers,
247262
...postcssOpts
@@ -250,23 +265,33 @@ export const createWebpackUtils = (
250265
return {
251266
loader: require.resolve(`postcss-loader`),
252267
options: {
253-
ident: `postcss-${++ident}`,
268+
execute: false,
254269
sourceMap: !PRODUCTION,
255-
plugins: (loader: Loader): Array<postcss.Plugin<any>> => {
256-
plugins =
257-
(typeof plugins === `function` ? plugins(loader) : plugins) || []
270+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
271+
postcssOptions: (loaderContext: any) => {
272+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
273+
let postCSSPlugins: Array<postcss.Plugin<any>> = []
274+
if (plugins) {
275+
postCSSPlugins =
276+
typeof plugins === `function` ? plugins(loaderContext) : plugins
277+
}
258278

259279
const autoprefixerPlugin = autoprefixer({
260280
overrideBrowserslist,
261281
flexbox: `no-2009`,
262-
...((plugins.find(
282+
...((postCSSPlugins.find(
263283
plugin => plugin.postcssPlugin === `autoprefixer`
264284
) as autoprefixer.Autoprefixer)?.options ?? {}),
265285
})
266286

267-
return [flexbugs, autoprefixerPlugin, ...plugins]
287+
postCSSPlugins.unshift(autoprefixerPlugin)
288+
postCSSPlugins.unshift(flexbugs)
289+
290+
return {
291+
plugins: postCSSPlugins,
292+
...postcssOpts,
293+
}
268294
},
269-
...postcssOpts,
270295
},
271296
}
272297
},
@@ -334,20 +359,6 @@ export const createWebpackUtils = (
334359
loader: require.resolve(`eslint-loader`),
335360
}
336361
},
337-
338-
imports: (options = {}) => {
339-
return {
340-
options,
341-
loader: require.resolve(`imports-loader`),
342-
}
343-
},
344-
345-
exports: (options = {}) => {
346-
return {
347-
options,
348-
loader: require.resolve(`exports-loader`),
349-
}
350-
},
351362
}
352363

353364
/**
@@ -499,7 +510,8 @@ export const createWebpackUtils = (
499510
rules.yaml = (): RuleSetRule => {
500511
return {
501512
test: /\.ya?ml$/,
502-
use: [loaders.json(), loaders.yaml()],
513+
type: `json`,
514+
use: [loaders.yaml()],
503515
}
504516
}
505517

@@ -552,13 +564,10 @@ export const createWebpackUtils = (
552564
const css: IRuleUtils["css"] = (options = {}): RuleSetRule => {
553565
const { browsers, ...restOptions } = options
554566
const use = [
567+
loaders.miniCssExtract(),
555568
loaders.css({ ...restOptions, importLoaders: 1 }),
556569
loaders.postcss({ browsers }),
557570
]
558-
if (!isSSR)
559-
use.unshift(
560-
loaders.miniCssExtract({ hmr: !PRODUCTION && !restOptions.modules })
561-
)
562571

563572
return {
564573
use,
@@ -614,10 +623,7 @@ export const createWebpackUtils = (
614623
...options
615624
}: { terserOptions?: TerserPlugin.TerserPluginOptions } = {}): Plugin =>
616625
new TerserPlugin({
617-
// TODO add proper cache keys
618-
cache: path.join(program.directory, `.cache`, `webpack`, `terser`),
619626
exclude: /\.min\.js/,
620-
sourceMap: true,
621627
terserOptions: {
622628
ie8: false,
623629
mangle: {
@@ -639,7 +645,7 @@ export const createWebpackUtils = (
639645

640646
plugins.minifyCss = (
641647
options = {
642-
cssProcessorPluginOptions: {
648+
minimizerOptions: {
643649
preset: [
644650
`default`,
645651
{
@@ -702,7 +708,7 @@ export const createWebpackUtils = (
702708
],
703709
},
704710
}
705-
): OptimizeCssAssetsPlugin => new OptimizeCssAssetsPlugin(options)
711+
): CssMinimizerPlugin => new CssMinimizerPlugin(options)
706712

707713
plugins.fastRefresh = (): Plugin =>
708714
new ReactRefreshWebpackPlugin({

packages/gatsby/src/utils/webpack.config.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,16 @@ module.exports = async (
232232
new StaticQueryMapper(store),
233233
])
234234
.filter(Boolean)
235+
236+
configPlugins.push(
237+
plugins.extractText({
238+
filename: `[name].css`,
239+
chunkFilename: `[id].css`,
240+
})
241+
)
242+
235243
if (process.env.GATSBY_EXPERIMENTAL_DEV_SSR) {
236-
// Don't use the default mini-css-extract-plugin setup as that
237-
// breaks hmr.
238-
configPlugins.push(
239-
plugins.extractText({ filename: `[name].css` }),
240-
plugins.extractStats()
241-
)
244+
configPlugins.push(plugins.extractStats())
242245
}
243246
break
244247
case `build-javascript`: {

0 commit comments

Comments
 (0)