Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable esmodule optimizations for css modules #62639

Open
wants to merge 6 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { webpack } from 'next/dist/compiled/webpack/webpack'

export function getClientStyleLoader({
optimizeCssModuleMappings,
hasAppDir,
isAppDir,
isDevelopment,
assetPrefix,
}: {
optimizeCssModuleMappings?: boolean
hasAppDir: boolean
isAppDir?: boolean
isDevelopment: boolean
Expand Down Expand Up @@ -47,7 +49,15 @@ export function getClientStyleLoader({
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: `${assetPrefix}/_next/`,
esModule: false,
// Activating the mini-css-extract-plugin esmodules export
// allows webpack to optimize the module tree and to inline
// the css module class names directly into react components
//
// Activating this option will break one edge case:
// import { default } from './file.module.css'
// Therefore it is behind the experimental flag
// `optimizeCssModuleMappings`
esModule: optimizeCssModuleMappings ? true : false,
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function getGlobalCssLoader(
// loader
loaders.push(
getClientStyleLoader({
optimizeCssModuleMappings: ctx.experimental.optimizeCssModuleMappings,
hasAppDir: ctx.hasAppDir,
isAppDir: ctx.isAppDir,
isDevelopment: ctx.isDevelopment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function getCssModuleLoader(
// loader
loaders.push(
getClientStyleLoader({
optimizeCssModuleMappings: ctx.experimental.optimizeCssModuleMappings,
hasAppDir: ctx.hasAppDir,
isAppDir: ctx.isAppDir,
isDevelopment: ctx.isDevelopment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function getNextFontLoader(
// loader
loaders.push(
getClientStyleLoader({
optimizeCssModuleMappings: ctx.experimental.optimizeCssModuleMappings,
hasAppDir: ctx.hasAppDir,
isDevelopment: ctx.isDevelopment,
assetPrefix: ctx.assetPrefix,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
caseSensitiveRoutes: z.boolean().optional(),
disableOptimizedLoading: z.boolean().optional(),
disablePostcssPresetEnv: z.boolean().optional(),
optimizeCssModuleMappings: z.boolean().optional(),
esmExternals: z.union([z.boolean(), z.literal('loose')]).optional(),
serverActions: z
.object({
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ export interface ExperimentalConfig {
// Use Record<string, unknown> as critters doesn't export its Option type
// https://github.com/GoogleChromeLabs/critters/blob/a590c05f9197b656d2aeaae9369df2483c26b072/packages/critters/src/index.d.ts
optimizeCss?: boolean | Record<string, unknown>
/**
* `optimizeCssModuleMappings` enables a compiler optimization that reduces the size of js code for css modules.
* However it will break `import { default } from './example.module.css'` in .js files.
*/
optimizeCssModuleMappings?: boolean
nextScriptWorkers?: boolean
scrollRestoration?: boolean
externalDir?: boolean
Expand Down
Loading