-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nextjs): Add missing support swc for custom server (nrwl#21401)
- Loading branch information
1 parent
7a62f41
commit d31b951
Showing
4 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
packages/next/src/generators/custom-server/custom-server.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Tree, readJson } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports'; | ||
import { applicationGenerator } from '../application/application'; | ||
|
||
describe('app', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it('should create a custom server', async () => { | ||
const name = uniq('custom-server'); | ||
|
||
await applicationGenerator(tree, { | ||
name, | ||
style: 'css', | ||
projectNameAndRootFormat: 'as-provided', | ||
customServer: true, | ||
}); | ||
|
||
const projectConfig = readJson(tree, `${name}/project.json`); | ||
|
||
expect(tree.exists(`${name}/server/main.ts`)).toBeTruthy(); | ||
expect(tree.exists(`${name}/tsconfig.server.json`)).toBeTruthy(); | ||
expect(projectConfig.targets['build-custom-server']).toBeTruthy(); | ||
expect(projectConfig.targets['serve-custom-server']).toBeTruthy(); | ||
}); | ||
|
||
it('should create a custom server with swc', async () => { | ||
const name = uniq('custom-server'); | ||
|
||
await applicationGenerator(tree, { | ||
name, | ||
style: 'css', | ||
projectNameAndRootFormat: 'as-provided', | ||
customServer: true, | ||
swc: true, | ||
}); | ||
|
||
const projectConfig = readJson(tree, `${name}/project.json`); | ||
const packageJson = readJson(tree, `package.json`); | ||
|
||
expect(tree.exists(`${name}/.swcrc`)).toBeTruthy(); | ||
expect(projectConfig.targets['build-custom-server'].executor).toEqual( | ||
'@nx/js:swc' | ||
); | ||
expect(packageJson.devDependencies['@swc/core']).toBeTruthy(); | ||
expect(packageJson.devDependencies['@swc/cli']).toBeTruthy(); | ||
expect(packageJson.devDependencies['@swc-node/register']).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
function uniq(name: string) { | ||
return `${name}-${(Math.random() * 10000).toFixed(0)}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
Tree, | ||
addDependenciesToPackageJson, | ||
installPackagesTask, | ||
joinPathFragments, | ||
readJson, | ||
} from '@nx/devkit'; | ||
import { swcCliVersion, swcCoreVersion, swcNodeVersion } from './versions'; | ||
import { addSwcConfig } from '@nx/js/src/utils/swc/add-swc-config'; | ||
|
||
export function configureForSwc(tree: Tree, projectRoot: string) { | ||
const swcConfigPath = joinPathFragments(projectRoot, '.swcrc'); | ||
const rootPackageJson = readJson(tree, 'package.json'); | ||
|
||
const hasSwcDepedency = | ||
rootPackageJson.dependencies?.['@swc/core'] || | ||
rootPackageJson.devDependencies?.['@swc/core']; | ||
|
||
const hasSwcCliDependency = | ||
rootPackageJson.dependencies?.['@swc/cli'] || | ||
rootPackageJson.devDependencies?.['@swc/cli']; | ||
|
||
if (!tree.exists(swcConfigPath)) { | ||
addSwcConfig(tree, swcConfigPath); | ||
} | ||
|
||
if (!hasSwcDepedency || !hasSwcCliDependency) { | ||
addSwcDependencies(tree); | ||
return () => installPackagesTask(tree); | ||
} | ||
} | ||
|
||
function addSwcDependencies(tree: Tree) { | ||
return addDependenciesToPackageJson( | ||
tree, | ||
{}, | ||
{ | ||
'@swc-node/register': swcNodeVersion, | ||
'@swc/cli': swcCliVersion, | ||
'@swc/core': swcCoreVersion, | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters