Skip to content

Commit

Permalink
feat!: migrate to ESM (#8178)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored May 21, 2022
1 parent f4d6262 commit 76fdc27
Show file tree
Hide file tree
Showing 39 changed files with 597 additions and 311 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ Repository: gulpjs/glob-parent

## http-proxy
License: MIT
By: Charlie Robbins
By: Charlie Robbins, jcrugzz <[email protected]>
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
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions packages/vite/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 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',
'loadConfigFromFile'
]
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')\`.`
)
}
})
27 changes: 22 additions & 5 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
{
"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",
"types": "./dist/node/index.d.ts",
"exports": {
".": {
"types": "./dist/node/index.d.ts",
"import": "./dist/node/index.js",
"require": "./index.cjs"
},
"./client": {
"types": "./client.d.ts"
},
"./terser": {
"require": "./dist/node-cjs/terser.cjs"
}
},
"files": [
"bin",
"dist",
"client.d.ts",
"index.cjs",
"src/client",
"types"
],
Expand All @@ -29,12 +45,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",
"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 +91,7 @@
"dotenv": "^14.3.2",
"dotenv-expand": "^5.1.0",
"es-module-lexer": "^0.10.5",
"esno": "^0.16.3",
"estree-walker": "^2.0.2",
"etag": "^1.8.1",
"fast-glob": "^3.2.11",
Expand Down
Loading

0 comments on commit 76fdc27

Please sign in to comment.