Files
51b878f5a3 Switch to postcss.config.mjs, add type (#63380)
<!-- 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

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- 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?

Now that @phanect added support for ESM PostCSS config files in PR
#63109 (original issue #34448), PostCSS can use ESM config by default.

It needs to use an `.mjs` extension by default because `create-next-app`
scaffolds CommonJS apps by default.

This will also work with ESM projects which have added `"type":
"module"` in their `package.json`

### Why?

1. To convert one more file to ESM
2. To use the modern format
3. To follow other similar migrations that have taken place in the
Next.js codebase (eg. `next.config.mjs`)

### How?

- Change file extensions from `.cjs` to `.mjs` (change similar to PR
#58380)
- Change module format from CommonJS to ESM
- Add type for the config, for users who enable `checkJs: true` in
`tsconfig.json`

Co-authored-by: Sam Ko <[email protected]>
2024-03-18 16:57:55 +00:00

182 lines
4.2 KiB
TypeScript

/**
* @fileoverview
*
* This file contains utilities for `create-next-app` testing.
*/
import { ChildProcess, spawn, SpawnOptions } from 'child_process'
import { existsSync } from 'fs'
import { resolve } from 'path'
import glob from 'glob'
import Conf from 'next/dist/compiled/conf'
import {
getProjectSetting,
mapSrcFiles,
projectSpecification,
} from './specification'
import { CustomTemplateOptions, ProjectDeps, ProjectFiles } from './types'
const cli = require.resolve('create-next-app/dist/index.js')
/**
* Run the built version of `create-next-app` with the given arguments.
*/
export const createNextApp = (
args: string[],
options?: SpawnOptions,
testVersion?: string
) => {
const conf = new Conf({ projectName: 'create-next-app' })
conf.clear()
console.log(`[TEST] $ ${cli} ${args.join(' ')}`, { options })
const cloneEnv = { ...process.env }
// unset CI env as this skips the auto-install behavior
// being tested
delete cloneEnv.CI
delete cloneEnv.CIRCLECI
delete cloneEnv.GITHUB_ACTIONS
delete cloneEnv.CONTINUOUS_INTEGRATION
delete cloneEnv.RUN_ID
delete cloneEnv.BUILD_NUMBER
cloneEnv.NEXT_PRIVATE_TEST_VERSION = testVersion || 'canary'
return spawn('node', [cli].concat(args), {
...options,
env: {
...cloneEnv,
...options.env,
},
})
}
/**
* Return a Promise that resolves when the process exits with code 0 and rejects
* otherwise.
*/
export const spawnExitPromise = (childProcess: ChildProcess) => {
return new Promise((resolve, reject) => {
childProcess
.on('exit', (code) => {
if (code === 0) {
resolve(code)
} else {
reject(code)
}
})
.on('error', reject)
})
}
export const projectFilesShouldExist = ({
cwd,
projectName,
files,
}: ProjectFiles) => {
const projectRoot = resolve(cwd, projectName)
for (const file of files) {
try {
expect(existsSync(resolve(projectRoot, file))).toBe(true)
} catch (err) {
require('console').error(
`missing expected file ${file}`,
glob.sync('**/*', { cwd, ignore: '**/node_modules/**' }),
files
)
throw err
}
}
}
export const projectFilesShouldNotExist = ({
cwd,
projectName,
files,
}: ProjectFiles) => {
const projectRoot = resolve(cwd, projectName)
for (const file of files) {
try {
expect(existsSync(resolve(projectRoot, file))).toBe(false)
} catch (err) {
require('console').error(
`unexpected file present ${file}`,
glob.sync('**/*', { cwd, ignore: '**/node_modules/**' }),
files
)
throw err
}
}
}
export const projectDepsShouldBe = ({
cwd,
projectName,
type,
deps,
}: ProjectDeps) => {
const projectRoot = resolve(cwd, projectName)
const pkgJson = require(resolve(projectRoot, 'package.json'))
expect(Object.keys(pkgJson[type] || {}).sort()).toEqual(deps.sort())
}
export const shouldBeTemplateProject = ({
cwd,
projectName,
template,
mode,
srcDir,
}: CustomTemplateOptions) => {
projectFilesShouldExist({
cwd,
projectName,
files: getProjectSetting({ template, mode, setting: 'files', srcDir }),
})
// Tailwind templates share the same files (tailwind.config.js, postcss.config.mjs)
if (template !== 'app-tw' && template !== 'default-tw') {
projectFilesShouldNotExist({
cwd,
projectName,
files: mapSrcFiles(
projectSpecification[template][mode === 'js' ? 'ts' : 'js'].files,
srcDir
),
})
}
projectDepsShouldBe({
type: 'dependencies',
cwd,
projectName,
deps: getProjectSetting({ template, mode, setting: 'deps' }),
})
projectDepsShouldBe({
type: 'devDependencies',
cwd,
projectName,
deps: getProjectSetting({ template, mode, setting: 'devDeps' }),
})
}
export const shouldBeJavascriptProject = ({
cwd,
projectName,
template,
srcDir,
}: Omit<CustomTemplateOptions, 'mode'>) => {
shouldBeTemplateProject({ cwd, projectName, template, mode: 'js', srcDir })
}
export const shouldBeTypescriptProject = ({
cwd,
projectName,
template,
srcDir,
}: Omit<CustomTemplateOptions, 'mode'>) => {
shouldBeTemplateProject({ cwd, projectName, template, mode: 'ts', srcDir })
}