Unverified Commit 422ccb98 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

fix: allow user directory with `styled-jsx` prefix in `next dev` (#41484)

Fixes #41476, ref #40679

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 4ba10021
...@@ -69,8 +69,7 @@ const babelIncludeRegexes: RegExp[] = [ ...@@ -69,8 +69,7 @@ const babelIncludeRegexes: RegExp[] = [
/next[\\/]dist[\\/](esm[\\/])?shared[\\/]lib/, /next[\\/]dist[\\/](esm[\\/])?shared[\\/]lib/,
/next[\\/]dist[\\/](esm[\\/])?client/, /next[\\/]dist[\\/](esm[\\/])?client/,
/next[\\/]dist[\\/](esm[\\/])?pages/, /next[\\/]dist[\\/](esm[\\/])?pages/,
/[\\/](strip-ansi|ansi-regex)[\\/]/, /[\\/](strip-ansi|ansi-regex|styled-jsx)[\\/]/,
/styled-jsx[\\/]/,
] ]
const BABEL_CONFIG_FILES = [ const BABEL_CONFIG_FILES = [
......
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
describe('project directory with styled-jsx suffix', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
},
dirSuffix: '-styled-jsx',
})
})
afterAll(() => next.destroy())
it('should work', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('hello world')
})
})
...@@ -11,17 +11,18 @@ async function createNextInstall( ...@@ -11,17 +11,18 @@ async function createNextInstall(
dependencies, dependencies,
installCommand, installCommand,
packageJson = {}, packageJson = {},
packageLockPath = '' packageLockPath = '',
dirSuffix = ''
) { ) {
const tmpDir = await fs.realpath(process.env.NEXT_TEST_DIR || os.tmpdir()) const tmpDir = await fs.realpath(process.env.NEXT_TEST_DIR || os.tmpdir())
const origRepoDir = path.join(__dirname, '../../') const origRepoDir = path.join(__dirname, '../../')
const installDir = path.join( const installDir = path.join(
tmpDir, tmpDir,
`next-install-${randomBytes(32).toString('hex')}` `next-install-${randomBytes(32).toString('hex')}${dirSuffix}`
) )
const tmpRepoDir = path.join( const tmpRepoDir = path.join(
tmpDir, tmpDir,
`next-repo-${randomBytes(32).toString('hex')}` `next-repo-${randomBytes(32).toString('hex')}${dirSuffix}`
) )
// ensure swc binary is present in the native folder if // ensure swc binary is present in the native folder if
......
import path from 'path' import path from 'path'
import assert from 'assert' import assert from 'assert'
import { NextConfig } from 'next' import { NextInstance, NextInstanceOpts } from './next-modes/base'
import { InstallCommand, NextInstance, PackageJson } from './next-modes/base'
import { NextDevInstance } from './next-modes/next-dev' import { NextDevInstance } from './next-modes/next-dev'
import { NextStartInstance } from './next-modes/next-start' import { NextStartInstance } from './next-modes/next-start'
import { NextDeployInstance } from './next-modes/next-deploy' import { NextDeployInstance } from './next-modes/next-deploy'
...@@ -110,24 +109,9 @@ if (typeof afterAll === 'function') { ...@@ -110,24 +109,9 @@ if (typeof afterAll === 'function') {
* test mode. The next instance will be isolated from the monorepo * test mode. The next instance will be isolated from the monorepo
* to prevent relying on modules that shouldn't be * to prevent relying on modules that shouldn't be
*/ */
export async function createNext(opts: { export async function createNext(
files: opts: NextInstanceOpts & { skipStart?: boolean }
| FileRef ): Promise<NextInstance> {
| {
[filename: string]: string | FileRef
}
dependencies?: {
[name: string]: string
}
nextConfig?: NextConfig
skipStart?: boolean
installCommand?: InstallCommand
buildCommand?: string
packageJson?: PackageJson
startCommand?: string
packageLockPath?: string
env?: Record<string, string>
}): Promise<NextInstance> {
try { try {
if (nextInstance) { if (nextInstance) {
throw new Error(`createNext called without destroying previous instance`) throw new Error(`createNext called without destroying previous instance`)
......
...@@ -13,71 +13,45 @@ export type InstallCommand = ...@@ -13,71 +13,45 @@ export type InstallCommand =
| ((ctx: { dependencies: { [key: string]: string } }) => string) | ((ctx: { dependencies: { [key: string]: string } }) => string)
export type PackageJson = { export type PackageJson = {
dependencies?: { [key: string]: string }
[key: string]: unknown [key: string]: unknown
} }
export interface NextInstanceOpts {
files: FileRef | { [filename: string]: string | FileRef }
dependencies?: { [name: string]: string }
packageJson?: PackageJson
packageLockPath?: string
nextConfig?: NextConfig
installCommand?: InstallCommand
buildCommand?: string
startCommand?: string
env?: Record<string, string>
dirSuffix?: string
}
export class NextInstance { export class NextInstance {
protected files: protected files: FileRef | { [filename: string]: string | FileRef }
| FileRef
| {
[filename: string]: string | FileRef
}
protected nextConfig?: NextConfig protected nextConfig?: NextConfig
protected installCommand?: InstallCommand protected installCommand?: InstallCommand
protected buildCommand?: string protected buildCommand?: string
protected startCommand?: string protected startCommand?: string
protected dependencies?: { [name: string]: string } protected dependencies?: PackageJson['dependencies'] = {}
protected events: { [eventName: string]: Set<any> } protected events: { [eventName: string]: Set<any> } = {}
public testDir: string public testDir: string
protected isStopping: boolean protected isStopping: boolean = false
protected isDestroyed: boolean protected isDestroyed: boolean = false
protected childProcess: ChildProcess protected childProcess: ChildProcess
protected _url: string protected _url: string
protected _parsedUrl: URL protected _parsedUrl: URL
protected packageJson: PackageJson protected packageJson?: PackageJson = {}
protected packageLockPath?: string protected packageLockPath?: string
protected basePath?: string protected basePath?: string
protected env?: Record<string, string> protected env?: Record<string, string>
public forcedPort?: string public forcedPort?: string
public dirSuffix: string = ''
constructor({ constructor(opts: NextInstanceOpts) {
files, Object.assign(this, opts)
dependencies,
nextConfig,
installCommand,
buildCommand,
startCommand,
packageJson = {},
packageLockPath,
env,
}: {
files:
| FileRef
| {
[filename: string]: string | FileRef
}
dependencies?: {
[name: string]: string
}
packageJson?: PackageJson
packageLockPath?: string
nextConfig?: NextConfig
installCommand?: InstallCommand
buildCommand?: string
startCommand?: string
env?: Record<string, string>
}) {
this.files = files
this.dependencies = dependencies
this.nextConfig = nextConfig
this.installCommand = installCommand
this.buildCommand = buildCommand
this.startCommand = startCommand
this.packageJson = packageJson
this.packageLockPath = packageLockPath
this.events = {}
this.isDestroyed = false
this.isStopping = false
this.env = env
} }
protected async createTestDir({ protected async createTestDir({
...@@ -94,7 +68,7 @@ export class NextInstance { ...@@ -94,7 +68,7 @@ export class NextInstance {
: process.env.NEXT_TEST_DIR || (await fs.realpath(os.tmpdir())) : process.env.NEXT_TEST_DIR || (await fs.realpath(os.tmpdir()))
this.testDir = path.join( this.testDir = path.join(
tmpDir, tmpDir,
`next-test-${Date.now()}-${(Math.random() * 1000) | 0}` `next-test-${Date.now()}-${(Math.random() * 1000) | 0}${this.dirSuffix}`
) )
const reactVersion = process.env.NEXT_TEST_REACT_VERSION || 'latest' const reactVersion = process.env.NEXT_TEST_REACT_VERSION || 'latest'
...@@ -102,7 +76,7 @@ export class NextInstance { ...@@ -102,7 +76,7 @@ export class NextInstance {
react: reactVersion, react: reactVersion,
'react-dom': reactVersion, 'react-dom': reactVersion,
...this.dependencies, ...this.dependencies,
...((this.packageJson.dependencies as object | undefined) || {}), ...this.packageJson?.dependencies,
} }
if (skipInstall) { if (skipInstall) {
...@@ -147,7 +121,8 @@ export class NextInstance { ...@@ -147,7 +121,8 @@ export class NextInstance {
finalDependencies, finalDependencies,
this.installCommand, this.installCommand,
this.packageJson, this.packageJson,
this.packageLockPath this.packageLockPath,
this.dirSuffix
) )
} }
require('console').log('created next.js install, writing test files') require('console').log('created next.js install, writing test files')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment