Skip to content

Commit 76fdc27

Browse files
authoredMay 21, 2022
feat!: migrate to ESM (#8178)
1 parent f4d6262 commit 76fdc27

39 files changed

+597
-311
lines changed
 

‎.eslintrc.cjs

+6
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ module.exports = defineConfig({
148148
rules: {
149149
'@typescript-eslint/triple-slash-reference': 'off'
150150
}
151+
},
152+
{
153+
files: 'packages/vite/**/*.*',
154+
rules: {
155+
'no-restricted-globals': ['error', 'require', '__dirname', '__filename']
156+
}
151157
}
152158
]
153159
})

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"prompts": "^2.4.2",
7878
"rimraf": "^3.0.2",
7979
"rollup": "^2.72.1",
80+
"rollup-plugin-esbuild": "^4.9.1",
8081
"semver": "^7.3.7",
8182
"simple-git-hooks": "^2.7.0",
8283
"sirv": "^2.0.2",

‎packages/plugin-vue-jsx/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createHash } from 'crypto'
22
import path from 'path'
33
import type { types } from '@babel/core'
4-
import babel from '@babel/core'
4+
import * as babel from '@babel/core'
55
import jsx from '@vue/babel-plugin-jsx'
66
// @ts-expect-error missing type
77
import importMeta from '@babel/plugin-syntax-import-meta'

‎packages/plugin-vue/src/style.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export async function transformStyle(
5151
}
5252

5353
const map = result.map
54-
? formatPostcssSourceMap(
54+
? await formatPostcssSourceMap(
5555
// version property of result.map is declared as string
5656
// but actually it is a number
5757
result.map as Omit<RawSourceMap, 'version'> as ExistingRawSourceMap,

‎packages/vite/LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ Repository: gulpjs/glob-parent
16481648

16491649
## http-proxy
16501650
License: MIT
1651-
By: Charlie Robbins
1651+
By: Charlie Robbins, jcrugzz <jcrugzz@gmail.com>
16521652
Repository: https://github.com/http-party/node-http-proxy.git
16531653

16541654
> node-http-proxy

‎packages/vite/bin/vite.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env node
2-
const { performance } = require('perf_hooks')
2+
import { performance } from 'perf_hooks'
33

4-
if (!__dirname.includes('node_modules')) {
4+
if (!import.meta.url.includes('node_modules')) {
55
try {
66
// only available as dev dependency
7-
require('source-map-support').install()
7+
await import('source-map-support').then((r) => r.default.install())
88
} catch (e) {}
99
}
1010

@@ -41,7 +41,7 @@ if (debugIndex > 0) {
4141
}
4242

4343
function start() {
44-
require('../dist/node/cli')
44+
return import('../dist/node/cli.js')
4545
}
4646

4747
if (profileIndex > 0) {
@@ -50,7 +50,7 @@ if (profileIndex > 0) {
5050
if (next && !next.startsWith('-')) {
5151
process.argv.splice(profileIndex, 1)
5252
}
53-
const inspector = require('inspector')
53+
const inspector = await import('inspector').then((r) => r.default)
5454
const session = (global.__vite_profile_session = new inspector.Session())
5555
session.connect()
5656
session.post('Profiler.enable', () => {

‎packages/vite/index.cjs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable no-restricted-globals */
2+
3+
// type utils
4+
module.exports.defineConfig = (config) => config
5+
6+
// proxy cjs utils (sync functions)
7+
Object.assign(module.exports, require('./dist/node-cjs/publicUtils.cjs'))
8+
9+
// async functions, can be redirect from ESM build
10+
const asyncFunctions = [
11+
'build',
12+
'createServer',
13+
'preview',
14+
'transformWithEsbuild',
15+
'resolveConfig',
16+
'optimizeDeps',
17+
'formatPostcssSourceMap',
18+
'loadConfigFromFile'
19+
]
20+
asyncFunctions.forEach((name) => {
21+
module.exports[name] = (...args) =>
22+
import('./dist/node/index.js').then((i) => i[name](...args))
23+
})
24+
25+
// some sync functions are marked not supported due to their complexity and uncommon usage
26+
const unsupportedCJS = ['resolvePackageEntry', 'resolvePackageData']
27+
unsupportedCJS.forEach((name) => {
28+
module.exports[name] = () => {
29+
throw new Error(
30+
`"${name}" is not supported in CJS build of Vite 3.\nPlease use ESM or dynamic imports \`const { ${name} } = await import('vite')\`.`
31+
)
32+
}
33+
})

‎packages/vite/package.json

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
{
22
"name": "vite",
33
"version": "3.0.0-alpha.1",
4+
"type": "module",
45
"license": "MIT",
56
"author": "Evan You",
67
"description": "Native-ESM powered web dev build tool",
78
"bin": {
89
"vite": "bin/vite.js"
910
},
10-
"main": "dist/node/index.js",
11-
"types": "dist/node/index.d.ts",
11+
"main": "./dist/node/index.js",
12+
"module": "./dist/node/index.js",
13+
"types": "./dist/node/index.d.ts",
14+
"exports": {
15+
".": {
16+
"types": "./dist/node/index.d.ts",
17+
"import": "./dist/node/index.js",
18+
"require": "./index.cjs"
19+
},
20+
"./client": {
21+
"types": "./client.d.ts"
22+
},
23+
"./terser": {
24+
"require": "./dist/node-cjs/terser.cjs"
25+
}
26+
},
1227
"files": [
1328
"bin",
1429
"dist",
1530
"client.d.ts",
31+
"index.cjs",
1632
"src/client",
1733
"types"
1834
],
@@ -29,12 +45,12 @@
2945
},
3046
"homepage": "https://github.com/vitejs/vite/tree/main/#readme",
3147
"scripts": {
32-
"dev": "rimraf dist && rollup -c -w",
48+
"dev": "rimraf dist && pnpm run build-bundle -w",
3349
"build": "rimraf dist && run-s build-bundle build-types",
34-
"build-bundle": "rollup -c",
50+
"build-bundle": "rollup --config rollup.config.ts --configPlugin esbuild",
3551
"build-types": "run-s build-temp-types patch-types roll-types",
3652
"build-temp-types": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
37-
"patch-types": "ts-node scripts/patchTypes.ts",
53+
"patch-types": "esno scripts/patchTypes.ts",
3854
"roll-types": "api-extractor run && rimraf temp",
3955
"lint": "eslint --ext .ts src/**",
4056
"format": "prettier --write --parser typescript \"src/**/*.ts\"",
@@ -75,6 +91,7 @@
7591
"dotenv": "^14.3.2",
7692
"dotenv-expand": "^5.1.0",
7793
"es-module-lexer": "^0.10.5",
94+
"esno": "^0.16.3",
7895
"estree-walker": "^2.0.2",
7996
"etag": "^1.8.1",
8097
"fast-glob": "^3.2.11",

0 commit comments

Comments
 (0)
Please sign in to comment.