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 11 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 @@ -141,6 +141,12 @@ module.exports = defineConfig({
rules: {
'@typescript-eslint/triple-slash-reference': 'off'
}
},
{
files: 'packages/vite/**/*.*',
rules: {
'no-restricted-globals': ['error', 'require', '__dirname', '__filename']
}
}
]
})
9 changes: 6 additions & 3 deletions 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 Expand Up @@ -73,7 +73,7 @@ function vueJsxPlugin(options: Options = {}): Plugin {
}
},

transform(code, id, opt) {
async transform(code, id, opt) {
const ssr = typeof opt === 'boolean' ? opt : (opt && opt.ssr) === true
const {
include,
Expand All @@ -91,7 +91,10 @@ function vueJsxPlugin(options: Options = {}): Plugin {
const plugins = [importMeta, [jsx, babelPluginOptions], ...babelPlugins]
if (id.endsWith('.tsx') || filepath.endsWith('.tsx')) {
plugins.push([
require('@babel/plugin-transform-typescript'),
// @ts-ignore missing type
await import('@babel/plugin-transform-typescript').then(
(r) => r.default
),
// @ts-ignore
{ isTSX: true, allowExtensions: true }
])
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
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')
const session = (global.__vite_profile_session = new inspector.Session())
session.connect()
session.post('Profiler.enable', () => {
Expand Down
36 changes: 36 additions & 0 deletions packages/vite/index.cjs
@@ -0,0 +1,36 @@
/* eslint-disable no-restricted-globals */
// Proxy ESM to be used in CJS

module.exports.build = (...args) =>
import('./dist/node/index.js').then((i) => i.build(...args))

module.exports.createServer = (...args) =>
import('./dist/node/index.js').then((i) => i.createServer(...args))

module.exports.preview = (...args) =>
import('./dist/node/index.js').then((i) => i.preview(...args))

module.exports.transformWithEsbuild = (...args) =>
import('./dist/node/index.js').then((i) => i.transformWithEsbuild(...args))

module.exports.formatPostcssSourceMap = (...args) =>
import('./dist/node/index.js').then((i) => i.formatPostcssSourceMap(...args))

module.exports.splitVendorChunkPlugin = (...args) =>
require('./dist/node-cjs/splitVendorChunk.cjs').splitVendorChunkPlugin(
...args
)
antfu marked this conversation as resolved.
Show resolved Hide resolved

module.exports.defineConfig = (config) => config

const os = require('os')
const path = require('path')
const isWindows = os.platform() === 'win32'

function slash(p) {
return p.replace(/\\/g, '/')
}

module.exports.normalizePath = (id) => {
return path.posix.normalize(isWindows ? slash(id) : id)
}
18 changes: 15 additions & 3 deletions packages/vite/package.json
@@ -1,18 +1,29 @@
{
"name": "vite",
"version": "3.0.0-alpha.0",
"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 @@ -34,7 +45,7 @@
"build-bundle": "rollup -c",
"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