From dd536a51ecba706cbe4de248d0f68dbcf4f29755 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Mon, 11 Mar 2024 22:19:50 +0100 Subject: [PATCH] Switch to .cjs PostCSS config (#58380) ### What? Prevent confusing error messages when changing to `"type": "module"` in `package.json` ``` ./node_modules/next/dist/build/webpack/loaders/css-loader/src/index.js??ruleSet[1].rules[2].oneOf[8].use[1]!./node_modules/next/dist/build/webpack/loaders/postcss-loader/src/index.js??ruleSet[1].rules[2].oneOf[8].use[2]!./src/styles/index.css Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/my/repo/components/postcss.config.js from /path/to/my/repo/components/node_modules/next/dist/lib/find-config.js not supported. Instead change the require of postcss.config.js in /path/to/my/repo/components/node_modules/next/dist/lib/find-config.js to a dynamic import() which is available in all CommonJS modules. ``` ### Why? Even though PostCSS itself [supports ESM and TypeScript configuration files](https://github.com/postcss/postcss-load-config/issues/230), Next.js itself does not (because of `next/lib/find-config`): - https://github.com/vercel/next.js/issues/34448 ### How? By switching to `.cjs`, the config will stay recognized as CommonJS even after switching to `"type": "module"` in `package.json` cc @balazsorban44 --------- Co-authored-by: Sam Ko --- .../app-tw/js/{postcss.config.js => postcss.config.cjs} | 0 .../app-tw/ts/{postcss.config.js => postcss.config.cjs} | 0 .../js/{postcss.config.js => postcss.config.cjs} | 0 .../ts/{postcss.config.js => postcss.config.cjs} | 0 packages/create-next-app/templates/index.ts | 2 +- test/integration/create-next-app/lib/specification.ts | 8 ++++---- test/integration/create-next-app/lib/utils.ts | 2 +- 7 files changed, 6 insertions(+), 6 deletions(-) rename packages/create-next-app/templates/app-tw/js/{postcss.config.js => postcss.config.cjs} (100%) rename packages/create-next-app/templates/app-tw/ts/{postcss.config.js => postcss.config.cjs} (100%) rename packages/create-next-app/templates/default-tw/js/{postcss.config.js => postcss.config.cjs} (100%) rename packages/create-next-app/templates/default-tw/ts/{postcss.config.js => postcss.config.cjs} (100%) diff --git a/packages/create-next-app/templates/app-tw/js/postcss.config.js b/packages/create-next-app/templates/app-tw/js/postcss.config.cjs similarity index 100% rename from packages/create-next-app/templates/app-tw/js/postcss.config.js rename to packages/create-next-app/templates/app-tw/js/postcss.config.cjs diff --git a/packages/create-next-app/templates/app-tw/ts/postcss.config.js b/packages/create-next-app/templates/app-tw/ts/postcss.config.cjs similarity index 100% rename from packages/create-next-app/templates/app-tw/ts/postcss.config.js rename to packages/create-next-app/templates/app-tw/ts/postcss.config.cjs diff --git a/packages/create-next-app/templates/default-tw/js/postcss.config.js b/packages/create-next-app/templates/default-tw/js/postcss.config.cjs similarity index 100% rename from packages/create-next-app/templates/default-tw/js/postcss.config.js rename to packages/create-next-app/templates/default-tw/js/postcss.config.cjs diff --git a/packages/create-next-app/templates/default-tw/ts/postcss.config.js b/packages/create-next-app/templates/default-tw/ts/postcss.config.cjs similarity index 100% rename from packages/create-next-app/templates/default-tw/ts/postcss.config.js rename to packages/create-next-app/templates/default-tw/ts/postcss.config.cjs diff --git a/packages/create-next-app/templates/index.ts b/packages/create-next-app/templates/index.ts index 717d931fa6d5a..170916d8f7608 100644 --- a/packages/create-next-app/templates/index.ts +++ b/packages/create-next-app/templates/index.ts @@ -51,7 +51,7 @@ export const installTemplate = async ({ if (!tailwind) copySource.push( mode == "ts" ? "tailwind.config.ts" : "!tailwind.config.js", - "!postcss.config.js", + "!postcss.config.cjs", ); await copy(copySource, root, { diff --git a/test/integration/create-next-app/lib/specification.ts b/test/integration/create-next-app/lib/specification.ts index c4071032f3f31..7ac5a27c6b996 100644 --- a/test/integration/create-next-app/lib/specification.ts +++ b/test/integration/create-next-app/lib/specification.ts @@ -68,7 +68,7 @@ export const projectSpecification: ProjectSpecification = { 'pages/_app.js', 'pages/api/hello.js', 'pages/index.js', - 'postcss.config.js', + 'postcss.config.cjs', 'tailwind.config.js', ], deps: [], @@ -80,7 +80,7 @@ export const projectSpecification: ProjectSpecification = { 'pages/_app.tsx', 'pages/api/hello.ts', 'pages/index.tsx', - 'postcss.config.js', + 'postcss.config.cjs', 'tailwind.config.ts', 'tsconfig.json', ], @@ -125,7 +125,7 @@ export const projectSpecification: ProjectSpecification = { 'app/layout.js', 'app/page.js', 'jsconfig.json', - 'postcss.config.js', + 'postcss.config.cjs', 'tailwind.config.js', ], }, @@ -143,7 +143,7 @@ export const projectSpecification: ProjectSpecification = { 'app/layout.tsx', 'app/page.tsx', 'next-env.d.ts', - 'postcss.config.js', + 'postcss.config.cjs', 'tailwind.config.ts', 'tsconfig.json', ], diff --git a/test/integration/create-next-app/lib/utils.ts b/test/integration/create-next-app/lib/utils.ts index 91495a69f0b26..0c1a9633eac9c 100644 --- a/test/integration/create-next-app/lib/utils.ts +++ b/test/integration/create-next-app/lib/utils.ts @@ -135,7 +135,7 @@ export const shouldBeTemplateProject = ({ files: getProjectSetting({ template, mode, setting: 'files', srcDir }), }) - // Tailwind templates share the same files (tailwind.config.js, postcss.config.js) + // Tailwind templates share the same files (tailwind.config.js, postcss.config.cjs) if (template !== 'app-tw' && template !== 'default-tw') { projectFilesShouldNotExist({ cwd,