Skip to content

Commit 49478ae

Browse files
authored
feat(create-vite): migrate to ESM (#8253)
1 parent 76fdc27 commit 49478ae

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

packages/create-vite/index.js

+39-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
#!/usr/bin/env node
22

33
// @ts-check
4-
const fs = require('fs')
5-
const path = require('path')
6-
// Avoids autoconversion to number of the project name by defining that the args
7-
// non associated with an option ( _ ) needs to be parsed as a string. See #4606
8-
const argv = require('minimist')(process.argv.slice(2), { string: ['_'] })
9-
// eslint-disable-next-line node/no-restricted-require
10-
const prompts = require('prompts')
11-
const {
12-
yellow,
13-
green,
14-
cyan,
4+
import fs from 'fs'
5+
import path from 'path'
6+
import { fileURLToPath } from 'url'
7+
import minimist from 'minimist'
8+
import prompts from 'prompts'
9+
import {
1510
blue,
16-
magenta,
11+
cyan,
12+
green,
1713
lightRed,
14+
magenta,
1815
red,
19-
reset
20-
} = require('kolorist')
16+
reset,
17+
yellow
18+
} from 'kolorist'
2119

20+
// Avoids autoconversion to number of the project name by defining that the args
21+
// non associated with an option ( _ ) needs to be parsed as a string. See #4606
22+
const argv = minimist(process.argv.slice(2), { string: ['_'] })
2223
const cwd = process.cwd()
2324

2425
const FRAMEWORKS = [
@@ -238,7 +239,11 @@ async function init() {
238239

239240
console.log(`\nScaffolding project in ${root}...`)
240241

241-
const templateDir = path.join(__dirname, `template-${template}`)
242+
const templateDir = path.resolve(
243+
fileURLToPath(import.meta.url),
244+
'..',
245+
`template-${template}`
246+
)
242247

243248
const write = (file, content) => {
244249
const targetPath = renameFiles[file]
@@ -256,7 +261,9 @@ async function init() {
256261
write(file)
257262
}
258263

259-
const pkg = require(path.join(templateDir, `package.json`))
264+
const pkg = JSON.parse(
265+
fs.readFileSync(path.join(templateDir, `package.json`), 'utf-8')
266+
)
260267

261268
pkg.name = packageName || targetDir
262269

@@ -291,12 +298,18 @@ function copy(src, dest) {
291298
}
292299
}
293300

301+
/**
302+
* @param {string} projectName
303+
*/
294304
function isValidPackageName(projectName) {
295305
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
296306
projectName
297307
)
298308
}
299309

310+
/**
311+
* @param {string} projectName
312+
*/
300313
function toValidPackageName(projectName) {
301314
return projectName
302315
.trim()
@@ -306,6 +319,10 @@ function toValidPackageName(projectName) {
306319
.replace(/[^a-z0-9-~]+/g, '-')
307320
}
308321

322+
/**
323+
* @param {string} srcDir
324+
* @param {string} destDir
325+
*/
309326
function copyDir(srcDir, destDir) {
310327
fs.mkdirSync(destDir, { recursive: true })
311328
for (const file of fs.readdirSync(srcDir)) {
@@ -315,11 +332,17 @@ function copyDir(srcDir, destDir) {
315332
}
316333
}
317334

335+
/**
336+
* @param {string} path
337+
*/
318338
function isEmpty(path) {
319339
const files = fs.readdirSync(path)
320340
return files.length === 0 || (files.length === 1 && files[0] === '.git')
321341
}
322342

343+
/**
344+
* @param {string} dir
345+
*/
323346
function emptyDir(dir) {
324347
if (!fs.existsSync(dir)) {
325348
return

packages/create-vite/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "create-vite",
33
"version": "2.9.3",
4+
"type": "module",
45
"license": "MIT",
56
"author": "Evan You",
67
"bin": {

0 commit comments

Comments
 (0)