Skip to content

Commit 394533f

Browse files
jantimonshuding
andauthored
next-flight-css-loader - fix !=! support (#51115)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change --> ### What? - Use a `cssModules` option to configure the `next-flight-css-loader.ts` based on the match resource. - Added types - Ran prettier ### Why? NextJs supports css modules and global css files. The `next-flight-css-loader.ts` is capable of dealing with both formats. However under the hood the loader shares almost no code for css modules and global css files. To branch into the correct behaviour the `next-flight-css-loader.ts` checks the extension of the file using `this.resourcePath.match(/\.module\.(css|sass|scss)$/)`. `this.resourcePath` does not include the information from webpacks `!=!` import syntax. One solution would be to use `this._module.matchResource` instead of `this.resourcePath`. But imho passing it from the webpack.config.js instead of duplicating the css module file regexp felt cleaner. ### Further questions - Should we update the loader banner comment? - Can we drop `this.data.__checksum` in the pitch loader function for css modules? (it would speed up css modules and for me it looks like it isn't needed anymore for css modules because of 2eeb0c7 (4. April by @shuding) - Should we split the loader into two loaders? Fixes #52208 --------- Co-authored-by: Shu Ding <g@shud.in>
1 parent 13e769a commit 394533f

File tree

2 files changed

+79
-40
lines changed

2 files changed

+79
-40
lines changed

packages/next/src/build/webpack/config/blocks/css/index.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,14 @@ export const css = curry(async function css(
258258
test: regexCssModules,
259259
issuerLayer: APP_LAYER_RULE,
260260
use: [
261-
require.resolve('../../../loaders/next-flight-css-loader'),
261+
{
262+
loader: require.resolve(
263+
'../../../loaders/next-flight-css-loader'
264+
),
265+
options: {
266+
cssModules: true,
267+
},
268+
},
262269
...getCssModuleLoader(
263270
{ ...ctx, isAppDir: true },
264271
lazyPostCSSInitializer
@@ -291,7 +298,14 @@ export const css = curry(async function css(
291298
test: regexSassModules,
292299
issuerLayer: APP_LAYER_RULE,
293300
use: [
294-
require.resolve('../../../loaders/next-flight-css-loader'),
301+
{
302+
loader: require.resolve(
303+
'../../../loaders/next-flight-css-loader'
304+
),
305+
options: {
306+
cssModules: true,
307+
},
308+
},
295309
...getCssModuleLoader(
296310
{ ...ctx, isAppDir: true },
297311
lazyPostCSSInitializer,
@@ -338,7 +352,14 @@ export const css = curry(async function css(
338352
sideEffects: true,
339353
test: [regexCssGlobal, regexSassGlobal],
340354
issuerLayer: APP_LAYER_RULE,
341-
use: require.resolve('../../../loaders/next-flight-css-loader'),
355+
use: {
356+
loader: require.resolve(
357+
'../../../loaders/next-flight-css-loader'
358+
),
359+
options: {
360+
cssModules: false,
361+
},
362+
},
342363
})
343364
: null,
344365
markRemovable({
@@ -389,7 +410,14 @@ export const css = curry(async function css(
389410
test: regexCssGlobal,
390411
issuerLayer: APP_LAYER_RULE,
391412
use: [
392-
require.resolve('../../../loaders/next-flight-css-loader'),
413+
{
414+
loader: require.resolve(
415+
'../../../loaders/next-flight-css-loader'
416+
),
417+
options: {
418+
cssModules: false,
419+
},
420+
},
393421
...getGlobalCssLoader(
394422
{ ...ctx, isAppDir: true },
395423
lazyPostCSSInitializer
@@ -401,7 +429,14 @@ export const css = curry(async function css(
401429
test: regexSassGlobal,
402430
issuerLayer: APP_LAYER_RULE,
403431
use: [
404-
require.resolve('../../../loaders/next-flight-css-loader'),
432+
{
433+
loader: require.resolve(
434+
'../../../loaders/next-flight-css-loader'
435+
),
436+
options: {
437+
cssModules: false,
438+
},
439+
},
405440
...getGlobalCssLoader(
406441
{ ...ctx, isAppDir: true },
407442
lazyPostCSSInitializer,

packages/next/src/build/webpack/loaders/next-flight-css-loader.ts

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,56 @@
55
*/
66

77
import crypto from 'crypto'
8+
import type webpack from 'webpack'
89

9-
export function pitch(this: any) {
10-
if (process.env.NODE_ENV !== 'production') {
11-
const content = this.fs.readFileSync(this.resourcePath)
12-
this.data.__checksum = crypto
13-
.createHash('sha256')
14-
.update(typeof content === 'string' ? Buffer.from(content) : content)
15-
.digest()
16-
.toString('hex')
17-
}
10+
type NextServerCSSLoaderOptions = {
11+
cssModules: boolean
1812
}
1913

20-
const NextServerCSSLoader = function (this: any, content: string) {
21-
this.cacheable && this.cacheable()
22-
23-
// Only add the checksum during development.
24-
if (process.env.NODE_ENV !== 'production') {
25-
const isCSSModule = this.resourcePath.match(/\.module\.(css|sass|scss)$/)
26-
const checksum = crypto
27-
.createHash('sha256')
28-
.update(
29-
this.data.__checksum +
30-
(typeof content === 'string' ? Buffer.from(content) : content)
31-
)
32-
.digest()
33-
.toString('hex')
34-
.substring(0, 12)
35-
36-
if (isCSSModule) {
37-
return `\
14+
const NextServerCSSLoader: webpack.LoaderDefinitionFunction<NextServerCSSLoaderOptions> =
15+
function (content) {
16+
this.cacheable && this.cacheable()
17+
const options = this.getOptions()
18+
let isCSSModule = options.cssModules
19+
20+
// Only add the checksum during development.
21+
if (process.env.NODE_ENV !== 'production') {
22+
// This check is only for backwards compatibility.
23+
// TODO: Remove this in the next major version (next 14)
24+
if (isCSSModule === undefined) {
25+
this.emitWarning(
26+
new Error(
27+
"No 'cssModules' option was found for the next-flight-css-loader plugin."
28+
)
29+
)
30+
isCSSModule =
31+
this.resourcePath.match(/\.module\.(css|sass|scss)$/) !== null
32+
}
33+
const checksum = crypto
34+
.createHash('sha256')
35+
.update(typeof content === 'string' ? Buffer.from(content) : content)
36+
.digest()
37+
.toString('hex')
38+
.substring(0, 12)
39+
40+
if (isCSSModule) {
41+
return `\
3842
${content}
3943
module.exports.__checksum = ${JSON.stringify(checksum)}
4044
`
41-
}
45+
}
4246

43-
// Server CSS imports are always available for HMR, so we attach
44-
// `module.hot.accept()` to the generated module.
45-
const hmrCode = 'if (module.hot) { module.hot.accept() }'
47+
// Server CSS imports are always available for HMR, so we attach
48+
// `module.hot.accept()` to the generated module.
49+
const hmrCode = 'if (module.hot) { module.hot.accept() }'
4650

47-
return `\
51+
return `\
4852
export default ${JSON.stringify(checksum)}
4953
${hmrCode}
5054
`
51-
}
55+
}
5256

53-
return content
54-
}
57+
return content
58+
}
5559

5660
export default NextServerCSSLoader

0 commit comments

Comments
 (0)