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 3 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
31 changes: 27 additions & 4 deletions packages/create-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ async function init() {
message:
typeof argTemplate === 'string' && !TEMPLATES.includes(argTemplate)
? reset(
`"${argTemplate}" isn't a valid template. Please choose from below: `,
)
`"${argTemplate}" isn't a valid template. Please choose from below: `,
)
: reset('Select a framework:'),
initial: 0,
choices: FRAMEWORKS.map((framework) => {
Expand Down Expand Up @@ -443,7 +443,7 @@ async function init() {
const isYarn1 = pkgManager === 'yarn' && pkgInfo?.version.startsWith('1.')

const { customCommand } =
FRAMEWORKS.flatMap((f) => f.variants).find((v) => v.name === template) ?? {}
FRAMEWORKS.flatMap((f) => f.variants).find((v) => v.name === template) ?? {}

if (customCommand) {
const fullCustomCommand = customCommand
Expand Down Expand Up @@ -512,7 +512,12 @@ async function init() {

pkg.name = packageName || getProjectName()

write('package.json', JSON.stringify(pkg, null, 2) + '\n')
if (template.endsWith('-ts')) {
if (!pkg.devDependencies) pkg.devDependencies = {}
pkg.devDependencies['@types/node'] = `^${process.versions.node}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.versions.node may not match the version of @types/node published on npm

For example, odd numbers of versions are not available on npmjs, only even numbers of version are available.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reminder. You're right, I overlooked this point. Moving forward, I will consider automatically adding @types/node only when the user's Node version meets Vite's requirements and is an even-numbered version. Otherwise, I'll output a prompt in the console. This might be a more refined solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this still has issue. For example, at the time of writing this post, the latest node on https://nodejs.org/en is 22.11.0, but the latest @types/node on https://www.npmjs.com/package/@types/node?activeTab=versions is 22.9.0 . This will cause a mismatch and the package cannot be resolved.

Copy link
Contributor

@smeng9 smeng9 Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we probably have to use npm's api to fetch all available versions and pick the most suitable version in a range?

}

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

if (isReactSwc) {
setupReactSwc(root, template.endsWith('-ts'))
Expand Down Expand Up @@ -624,6 +629,24 @@ 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)
})
Loading