Skip to content

Commit 725dc36

Browse files
LekoArtspiehgatsbybot
authored
fix(gatsby): Update mini-css-extract-plugin to fix inc builds issue (#33979)
* remove obsolete option * update plugin * update netlify * clarify comment * update e2e test * types * remove resolutions * update css-loader * tests(artifacts): add css import to test site (#33982) * things work now? * wip * clean * asset tests * wrap with offline plugin * add back url loader Co-authored-by: Michal Piechowiak <[email protected]> Co-authored-by: gatsbybot <[email protected]>
1 parent 9bd1e01 commit 725dc36

File tree

17 files changed

+140
-82
lines changed

17 files changed

+140
-82
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
describe(`webpack assets`, () => {
2+
beforeEach(() => {
3+
cy.intercept("/gatsby-astronaut.png").as("static-folder-image")
4+
// Should load two files: normal and italic
5+
cy.intercept("/static/merriweather-latin-300**.woff2").as("font")
6+
cy.intercept("/static/gatsby-astronaut-**.png").as("img-import")
7+
cy.visit(`/assets/`).waitForRouteChange()
8+
})
9+
10+
// Service worker is handling requests so this one is cached by previous runs
11+
if (!Cypress.env(`TEST_PLUGIN_OFFLINE`)) {
12+
it(`should only create one font file (no duplicates with different hashes)`, () => {
13+
// Check that there is no duplicate files (should have italic as second request, not another normal font)
14+
cy.wait("@font").should(req => {
15+
expect(req.response.url).to.match(/merriweather-latin-300-/i)
16+
})
17+
cy.wait("@font").should(req => {
18+
expect(req.response.url).to.match(/merriweather-latin-300italic-/i)
19+
})
20+
})
21+
it(`should load image import`, () => {
22+
cy.wait("@img-import").should(req => {
23+
expect(req.response.statusCode).to.be.gte(200).and.lt(400)
24+
})
25+
})
26+
it(`should load file import`, () => {
27+
cy.getTestElement('assets-pdf-import').should('have.attr', 'href').and('match', /\/static\/pdf-example-.*\.pdf/i)
28+
})
29+
}
30+
31+
it(`should load static folder asset`, () => {
32+
cy.wait("@static-folder-image").should(req => {
33+
expect(req.response.statusCode).to.be.gte(200).and.lt(400)
34+
})
35+
})
36+
})

e2e-tests/production-runtime/package.json

+14-16
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
"dependencies": {
77
"babel-plugin-search-and-replace": "^1.1.0",
88
"cypress": "^6.5.0",
9-
"gatsby": "^3.0.0-next.6",
10-
"gatsby-cypress": "^1.3.0",
11-
"gatsby-plugin-image": "^1.0.0-next.5",
12-
"gatsby-plugin-less": "^5.1.0-next.2",
13-
"gatsby-plugin-manifest": "^3.0.0-next.0",
14-
"gatsby-plugin-offline": "^4.0.0-next.1",
15-
"gatsby-plugin-react-helmet": "^4.0.0-next.0",
16-
"gatsby-plugin-sass": "^4.1.0-next.2",
17-
"gatsby-plugin-sharp": "^3.0.0-next.5",
18-
"gatsby-plugin-stylus": "^3.1.0-next.2",
9+
"gatsby": "^4.1.6",
10+
"gatsby-cypress": "^2.1.0",
11+
"gatsby-plugin-image": "^2.1.3",
12+
"gatsby-plugin-less": "^6.1.0",
13+
"gatsby-plugin-manifest": "^4.1.4",
14+
"gatsby-plugin-offline": "^5.1.4",
15+
"gatsby-plugin-react-helmet": "^5.1.0",
16+
"gatsby-plugin-sass": "^5.1.1",
17+
"gatsby-plugin-sharp": "^4.1.4",
18+
"gatsby-plugin-stylus": "^4.1.0",
1919
"gatsby-seo": "^0.1.0",
20-
"gatsby-source-filesystem": "^3.3.0",
20+
"gatsby-source-filesystem": "^4.1.3",
2121
"glob": "^7.1.3",
2222
"react": "^16.9.0",
2323
"react-dom": "^16.9.0",
@@ -32,10 +32,11 @@
3232
"scripts": {
3333
"build": "cross-env CYPRESS_SUPPORT=y gatsby build",
3434
"build:offline": "cross-env TEST_PLUGIN_OFFLINE=y CYPRESS_SUPPORT=y gatsby build",
35-
"develop": "gatsby develop",
35+
"develop": "cross-env CYPRESS_SUPPORT=y gatsby develop",
3636
"format": "prettier --write '**/*.js' --ignore-path .gitignore",
3737
"serve": "gatsby serve",
3838
"start": "npm run develop",
39+
"clean": "gatsby clean",
3940
"test": "npm run build && npm run start-server-and-test && npm run test-env-vars",
4041
"test:offline": "npm run build:offline && yarn start-server-and-test:offline && npm run test-env-vars",
4142
"test-env-vars": " node __tests__/env-vars.js",
@@ -51,16 +52,13 @@
5152
"devDependencies": {
5253
"cross-env": "^5.2.0",
5354
"fs-extra": "^7.0.1",
54-
"gatsby-core-utils": "^2.12.0",
55+
"gatsby-core-utils": "^3.1.3",
5556
"is-ci": "^2.0.0",
5657
"prettier": "2.0.4",
5758
"start-server-and-test": "^1.7.1"
5859
},
5960
"repository": {
6061
"type": "git",
6162
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
62-
},
63-
"resolutions": {
64-
"graphql-config": "3.3.0"
6563
}
6664
}
Binary file not shown.
Loading

e2e-tests/production-runtime/src/index.css

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
font-size: 18px;
1616
}
1717

18+
.merriweather-300 {
19+
font-family: "Merriweather";
20+
font-weight: 300;
21+
font-size: 18px;
22+
}
23+
24+
.merriweather-300-italic {
25+
font-family: "Merriweather";
26+
font-weight: 300;
27+
font-size: 18px;
28+
font-style: italic;
29+
}
30+
1831
.dog-background-flip {
1932
background: url("//localhost:9000/dog-thumbnail-flip.jpg");
2033
width: 640px;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as React from "react"
2+
import Layout from "../components/layout"
3+
import astronaut from "../images/gatsby-astronaut.png"
4+
import pdf from "../files/pdf-example.pdf"
5+
6+
const Assets = () => (
7+
<Layout>
8+
<h2 className="merriweather-300">Font</h2>
9+
<h2 className="merriweather-300-italic">Font Italic</h2>
10+
<img data-testid="assets-img-static-folder" src="../gatsby-astronaut.png" alt="Gatsby Astronaut Static Folder" />
11+
<img data-testid="assets-img-import" src={astronaut} alt="Gatsby Astronaut" />
12+
<a data-testid="assets-pdf-import" href={pdf}>Download PDF</a>
13+
</Layout>
14+
)
15+
16+
export default Assets
Loading

integration-tests/artifacts/gatsby-browser.js

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ const React = require(`react`)
22
const { useMoreInfoQuery } = require("./src/hooks/use-more-info-query")
33
const Github = require(`./src/components/github`).default
44

5+
// global css import (make sure warm rebuild doesn't invalidate every file when css is imported)
6+
require("./imported.css")
7+
58
exports.wrapRootElement = ({ element }) => {
69
return (
710
<>
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
background: blue;
3+
}

packages/gatsby-plugin-less/src/gatsby-node.js

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ exports.onCreateWebpackConfig = (
3434
}
3535
const lessRuleModules = {
3636
test: /\.module\.less$/,
37+
// TODO(v5): Remove obsolete modules option from miniCssExtract
3738
use: [
3839
!isSSR &&
3940
loaders.miniCssExtract({

packages/gatsby-plugin-netlify-cms/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"html-webpack-skip-assets-plugin": "^1.0.3",
1515
"html-webpack-tags-plugin": "^3.0.2",
1616
"lodash": "^4.17.21",
17-
"mini-css-extract-plugin": "1.6.2",
17+
"mini-css-extract-plugin": "^2.4.4",
1818
"netlify-identity-widget": "^1.9.2"
1919
},
2020
"devDependencies": {

packages/gatsby-plugin-postcss/src/gatsby-node.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ exports.onCreateWebpackConfig = (
5050
}
5151
const postcssRuleModules = {
5252
test: MODULE_CSS_PATTERN,
53+
// TODO(v5): Remove obsolete modules option from miniCssExtract
5354
use: [
5455
!isSSR &&
5556
loaders.miniCssExtract({

packages/gatsby-plugin-sass/src/gatsby-node.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ exports.onCreateWebpackConfig = (
4343

4444
const sassRuleModules = {
4545
test: sassRuleModulesTest || /\.module\.s(a|c)ss$/,
46+
// TODO(v5): Remove obsolete modules option from miniCssExtract
4647
use: [
4748
!isSSR &&
4849
loaders.miniCssExtract({

packages/gatsby-plugin-stylus/src/gatsby-node.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ exports.onCreateWebpackConfig = (
5454

5555
const stylusRuleModules = {
5656
test: /\.module\.styl$/,
57+
// TODO(v5): Remove obsolete modules option from miniCssExtract
5758
use: [
5859
!isSSR &&
5960
loaders.miniCssExtract({

packages/gatsby/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"cookie": "^0.4.1",
4949
"core-js": "^3.17.2",
5050
"cors": "^2.8.5",
51-
"css-loader": "^5.2.7",
51+
"css-loader": "^6.5.1",
5252
"css-minimizer-webpack-plugin": "^2.0.0",
5353
"css.escape": "^1.5.1",
5454
"date-fns": "^2.25.0",
@@ -108,7 +108,7 @@
108108
"memoizee": "^0.4.15",
109109
"micromatch": "^4.0.4",
110110
"mime": "^2.5.2",
111-
"mini-css-extract-plugin": "1.6.2",
111+
"mini-css-extract-plugin": "^2.4.4",
112112
"mitt": "^1.2.0",
113113
"moment": "^2.29.1",
114114
"multer": "^1.4.3",

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

+35-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from "path"
2-
import { RuleSetRule, WebpackPluginInstance } from "webpack"
2+
import { RuleSetRule, WebpackPluginInstance, Configuration } from "webpack"
33
import { GraphQLSchema } from "graphql"
44
import { Plugin as PostCSSPlugin } from "postcss"
55
import autoprefixer from "autoprefixer"
@@ -65,12 +65,16 @@ type CSSModulesOptions =
6565
exportOnlyLocals?: boolean
6666
}
6767

68-
type MiniCSSExtractLoaderModuleOptions =
69-
| undefined
70-
| boolean
71-
| {
72-
namedExport?: boolean
73-
}
68+
interface IMiniCSSExtractLoaderModuleOptions {
69+
filename?: Required<Configuration>["output"]["filename"] | undefined
70+
chunkFilename?: Required<Configuration>["output"]["chunkFilename"] | undefined
71+
experimentalUseImportModule?: boolean | undefined
72+
ignoreOrder?: boolean | undefined
73+
insert?: string | ((linkTag: any) => void) | undefined
74+
attributes?: Record<string, string> | undefined
75+
linkType?: string | false | "text/css" | undefined
76+
runtime?: boolean | undefined
77+
}
7478
/**
7579
* Utils that produce webpack `loader` objects
7680
*/
@@ -234,27 +238,13 @@ export const createWebpackUtils = (
234238
}
235239
},
236240

237-
miniCssExtract: (
238-
options: {
239-
modules?: MiniCSSExtractLoaderModuleOptions
240-
} = {}
241-
) => {
242-
let moduleOptions: MiniCSSExtractLoaderModuleOptions = undefined
243-
241+
miniCssExtract: (options: IMiniCSSExtractLoaderModuleOptions = {}) => {
242+
// @ts-ignore - legacy modules
244243
const { modules, ...restOptions } = options
245244

246-
if (typeof modules === `boolean` && options.modules) {
247-
moduleOptions = {
248-
namedExport: true,
249-
}
250-
} else {
251-
moduleOptions = modules
252-
}
253-
254245
return {
255246
loader: MiniCssExtractPlugin.loader,
256247
options: {
257-
modules: moduleOptions,
258248
...restOptions,
259249
},
260250
}
@@ -283,13 +273,15 @@ export const createWebpackUtils = (
283273
loader: require.resolve(`css-loader`),
284274
options: {
285275
// Absolute urls (https or //) are not send to this function. Only resolvable paths absolute or relative ones.
286-
url: function (url: string): boolean {
287-
// When an url starts with /
288-
if (url.startsWith(`/`)) {
289-
return false
290-
}
291-
292-
return true
276+
url: {
277+
filter: function (url: string): boolean {
278+
// When an url starts with /
279+
if (url.startsWith(`/`)) {
280+
return false
281+
}
282+
283+
return true
284+
},
293285
},
294286
sourceMap: !PRODUCTION,
295287
modules: modulesOptions,
@@ -350,6 +342,7 @@ export const createWebpackUtils = (
350342
}
351343
},
352344

345+
// TODO(v5): Consider removing this (as not used anymore internally)
353346
url: (options = {}) => {
354347
return {
355348
loader: require.resolve(`url-loader`),
@@ -545,8 +538,11 @@ export const createWebpackUtils = (
545538
*/
546539
rules.fonts = (): RuleSetRule => {
547540
return {
548-
use: [loaders.url()],
549541
test: /\.(eot|otf|ttf|woff(2)?)(\?.*)?$/,
542+
type: `asset/resource`,
543+
generator: {
544+
filename: `${assetRelativeRoot}[name]-[hash][ext]`,
545+
},
550546
}
551547
}
552548

@@ -556,8 +552,11 @@ export const createWebpackUtils = (
556552
*/
557553
rules.images = (): RuleSetRule => {
558554
return {
559-
use: [loaders.url()],
560555
test: /\.(ico|svg|jpg|jpeg|png|gif|webp|avif)(\?.*)?$/,
556+
type: `asset/resource`,
557+
generator: {
558+
filename: `${assetRelativeRoot}[name]-[hash][ext]`,
559+
},
561560
}
562561
}
563562

@@ -567,8 +566,11 @@ export const createWebpackUtils = (
567566
*/
568567
rules.media = (): RuleSetRule => {
569568
return {
570-
use: [loaders.url()],
571569
test: /\.(mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/,
570+
type: `asset/resource`,
571+
generator: {
572+
filename: `${assetRelativeRoot}[name]-[hash][ext]`,
573+
},
572574
}
573575
}
574576

0 commit comments

Comments
 (0)