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

chore(create-vite): add @types/node as devDependency in *-ts #18653

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 26 additions & 1 deletion packages/create-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,15 @@ async function init() {

pkg.name = packageName || getProjectName()

write('package.json', JSON.stringify(pkg, null, 2) + '\n')
if (template.endsWith('-ts')) {
const major = parseInt(process.versions.node.split('.')[0])
if ((major === 18 || major >= 20) && (major & 1) === 0) {
if (!pkg.devDependencies) pkg.devDependencies = {}
pkg.devDependencies['@types/node'] = `^${process.versions.node}`
}
}

write('package.json', JSON.stringify(pkg, sortKeys, 2) + '\n')

if (isReactSwc) {
setupReactSwc(root, template.endsWith('-ts'))
Expand Down Expand Up @@ -624,6 +632,23 @@ function editFile(file: string, callback: (content: string) => string) {
fs.writeFileSync(file, callback(content), 'utf-8')
}

function sortKeys(key: string, value: any) {
if (
(key === 'dependencies' || key === 'devDependencies') &&
typeof value === 'object' &&
value != null
) {
const sortedObject: Record<string, any> = {}
Object.keys(value)
.sort()
.forEach((it) => {
sortedObject[it] = value[it]
})
return sortedObject
}
return value
}

init().catch((e) => {
console.error(e)
})