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

feat!: migrate to ESM #8178

Merged
merged 24 commits into from May 21, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.cjs
Expand Up @@ -148,6 +148,12 @@ module.exports = defineConfig({
rules: {
'@typescript-eslint/triple-slash-reference': 'off'
}
},
{
files: 'packages/vite/**/*.*',
rules: {
'no-restricted-globals': ['error', 'require', '__dirname', '__filename']
}
}
]
})
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -77,6 +77,7 @@
"prompts": "^2.4.2",
"rimraf": "^3.0.2",
"rollup": "^2.72.1",
"rollup-plugin-esbuild": "^4.9.1",
"semver": "^7.3.7",
"simple-git-hooks": "^2.7.0",
"sirv": "^2.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue-jsx/src/index.ts
@@ -1,7 +1,7 @@
import { createHash } from 'crypto'
import path from 'path'
import type { types } from '@babel/core'
import babel from '@babel/core'
import * as babel from '@babel/core'
import jsx from '@vue/babel-plugin-jsx'
// @ts-expect-error missing type
import importMeta from '@babel/plugin-syntax-import-meta'
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue/src/style.ts
Expand Up @@ -51,7 +51,7 @@ export async function transformStyle(
}

const map = result.map
? formatPostcssSourceMap(
? await formatPostcssSourceMap(
// version property of result.map is declared as string
// but actually it is a number
result.map as Omit<RawSourceMap, 'version'> as ExistingRawSourceMap,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/LICENSE.md
Expand Up @@ -1648,7 +1648,7 @@ Repository: gulpjs/glob-parent

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

> node-http-proxy
Expand Down
10 changes: 5 additions & 5 deletions packages/vite/bin/vite.js
@@ -1,10 +1,10 @@
#!/usr/bin/env node
const { performance } = require('perf_hooks')
import { performance } from 'perf_hooks'

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

Expand Down Expand Up @@ -41,7 +41,7 @@ if (debugIndex > 0) {
}

function start() {
require('../dist/node/cli')
return import('../dist/node/cli.js')
}

if (profileIndex > 0) {
Expand All @@ -50,7 +50,7 @@ if (profileIndex > 0) {
if (next && !next.startsWith('-')) {
process.argv.splice(profileIndex, 1)
}
const inspector = require('inspector')
const inspector = await import('inspector').then((r) => r.default)
const session = (global.__vite_profile_session = new inspector.Session())
session.connect()
session.post('Profiler.enable', () => {
Expand Down
32 changes: 32 additions & 0 deletions packages/vite/index.cjs
@@ -0,0 +1,32 @@
/* eslint-disable no-restricted-globals */

// type utils
module.exports.defineConfig = (config) => config

// proxy cjs utils (sync functions)
Object.assign(module.exports, require('./dist/node-cjs/publicUtils.cjs'))

// async functions, can be redirect from ESM build
const asyncFunctions = [
'build',
'createServer',
'preview',
'transformWithEsbuild',
'resolveConfig',
'optimizeDeps',
'formatPostcssSourceMap'
]
asyncFunctions.forEach((name) => {
module.exports[name] = (...args) =>
import('./dist/node/index.js').then((i) => i[name](...args))
})

// some sync functions are marked not supported due to their complexity and uncommon usage
const unsupportedCJS = ['resolvePackageEntry', 'resolvePackageData']
unsupportedCJS.forEach((name) => {
module.exports[name] = () => {
throw new Error(
`"${name}" is not supported in CJS build of Vite 3.\nPlease use ESM or dynamic imports \`const { ${name} } = await import('vite')\`.`
)
}
})
22 changes: 17 additions & 5 deletions packages/vite/package.json
@@ -1,18 +1,29 @@
{
"name": "vite",
"version": "3.0.0-alpha.1",
"type": "module",
"license": "MIT",
"author": "Evan You",
"description": "Native-ESM powered web dev build tool",
"bin": {
"vite": "bin/vite.js"
},
"main": "dist/node/index.js",
"types": "dist/node/index.d.ts",
"main": "./dist/node/index.js",
"module": "./dist/node/index.js",
Comment on lines +11 to +12
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 we could remove these 2 fields since Vite 3 supports Node 14+, Node 12+ supports exports field and exports takes priority than main and module so Node.js will never reach these 2 fields.

"types": "./dist/node/index.d.ts",
"exports": {
".": {
"types": "./dist/node/index.d.ts",
"import": "./dist/node/index.js",
"require": "./index.cjs"
},
"./*": "./*"
antfu marked this conversation as resolved.
Show resolved Hide resolved
},
"files": [
"bin",
"dist",
"client.d.ts",
"index.cjs",
"src/client",
"types"
],
Expand All @@ -29,12 +40,12 @@
},
"homepage": "https://github.com/vitejs/vite/tree/main/#readme",
"scripts": {
"dev": "rimraf dist && rollup -c -w",
"dev": "rimraf dist && pnpm run build-bundle -w",
"build": "rimraf dist && run-s build-bundle build-types",
"build-bundle": "rollup -c",
"build-bundle": "rollup --config rollup.config.ts --configPlugin esbuild",
bluwy marked this conversation as resolved.
Show resolved Hide resolved
"build-types": "run-s build-temp-types patch-types roll-types",
"build-temp-types": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
"patch-types": "ts-node scripts/patchTypes.ts",
"patch-types": "esno scripts/patchTypes.ts",
"roll-types": "api-extractor run && rimraf temp",
"lint": "eslint --ext .ts src/**",
"format": "prettier --write --parser typescript \"src/**/*.ts\"",
Expand Down Expand Up @@ -75,6 +86,7 @@
"dotenv": "^14.3.2",
"dotenv-expand": "^5.1.0",
"es-module-lexer": "^0.10.5",
"esno": "^0.15.0",
"estree-walker": "^2.0.2",
"etag": "^1.8.1",
"fast-glob": "^3.2.11",
Expand Down