Skip to content

Commit 83ddf72

Browse files
authored
chore: use unbuild to bundle plugins (#8139)
1 parent 72cf80f commit 83ddf72

18 files changed

+199
-205
lines changed

.eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist
2+
playground-temp
3+
temp
4+

.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ module.exports = defineConfig({
125125
}
126126
},
127127
{
128-
files: ['packages/create-vite/template-*/**'],
128+
files: ['packages/create-vite/template-*/**', '**/build.config.ts'],
129129
rules: {
130130
'node/no-missing-import': 'off'
131131
}

.github/workflows/ci.yml

+4-13
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,8 @@ jobs:
6666
- name: Install Playwright
6767
run: pnpm playwright install
6868

69-
- name: Build vite
70-
run: pnpm run ci-build-vite
71-
72-
- name: Build plugin-vue
73-
run: pnpm run build-plugin-vue
74-
75-
- name: Build plugin-react
76-
run: pnpm run build-plugin-react
69+
- name: Build
70+
run: pnpm run build
7771

7872
- name: Test unit
7973
run: pnpm run test-unit
@@ -107,11 +101,8 @@ jobs:
107101
env:
108102
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: "1"
109103

110-
- name: Prepare
111-
run: |
112-
pnpm run ci-build-vite
113-
pnpm run build-plugin-vue
114-
pnpm run build-plugin-react
104+
- name: Build
105+
run: pnpm run build
115106

116107
- name: Lint
117108
run: pnpm run lint

.prettierignore

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
docs/.vitepress/dist/
2-
packages/vite/dist/
3-
packages/vite/temp/
4-
packages/plugin-react/dist/
5-
packages/plugin-vue/dist/
61
packages/*/CHANGELOG.md
72
playground-temp/
3+
dist/
4+
temp/
85
LICENSE.md
9-
.prettierignore
106
pnpm-lock.yaml
117
pnpm-workspace.yaml
128
playground/tsconfig-json-load-error/has-error/tsconfig.json

package.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,26 @@
1616
"preinstall": "npx only-allow pnpm",
1717
"format": "prettier --write .",
1818
"lint": "eslint packages/*/{src,types}/** playground/**/__tests__/** scripts/**",
19+
"typecheck": "tsc -p scripts --noEmit && tsc -p playground --noEmit",
1920
"test": "run-s test-unit test-serve test-build",
2021
"test-serve": "vitest run -c vitest.config.e2e.ts",
2122
"test-build": "cross-env VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
2223
"test-unit": "vitest run",
2324
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 vitest run -c vitest.config.e2e.ts",
2425
"debug-build": "cross-env VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 vitest run -c vitest.config.e2e.ts",
2526
"docs": "vitepress dev docs",
26-
"build-docs": "vitepress build docs",
27-
"serve-docs": "vitepress serve docs",
27+
"docs-build": "vitepress build docs",
28+
"docs-serve": "vitepress serve docs",
29+
"build": "pnpm -r --filter=./packages/* run build",
30+
"dev": "pnpm -r --parallel --filter=./packages/* run dev",
2831
"release": "ts-node scripts/release.ts",
2932
"ci-publish": "ts-node scripts/publishCI.ts",
30-
"typecheck": "tsc -p scripts --noEmit && tsc -p playground --noEmit",
31-
"build": "run-s build-vite build-plugin-vue build-plugin-react",
32-
"build-vite": "cd packages/vite && npm run build",
33-
"build-plugin-vue": "cd packages/plugin-vue && npm run build",
34-
"build-plugin-react": "cd packages/plugin-react && npm run build",
35-
"ci-build-vite": "cd packages/vite && npm run ci-build",
36-
"ci-docs": "run-s build-vite build-plugin-vue build-docs"
33+
"ci-docs": "run-s build docs-build"
3734
},
3835
"devDependencies": {
3936
"@microsoft/api-extractor": "^7.23.1",
4037
"@types/babel__core": "^7.1.19",
38+
"@types/babel__standalone": "^7.1.4",
4139
"@types/convert-source-map": "^1.5.2",
4240
"@types/cross-spawn": "^6.0.2",
4341
"@types/debug": "^4.1.7",
@@ -83,6 +81,7 @@
8381
"sirv": "^2.0.2",
8482
"ts-node": "^10.7.0",
8583
"typescript": "^4.6.4",
84+
"unbuild": "^0.7.4",
8685
"vite": "workspace:*",
8786
"vitepress": "^0.22.4",
8887
"vitest": "^0.12.4",
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
entries: ['src/index'],
5+
clean: true,
6+
declaration: true,
7+
rollup: {
8+
emitCJS: true
9+
}
10+
})

packages/plugin-vue-jsx/index.d.ts

-14
This file was deleted.

packages/plugin-vue-jsx/package.json

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
"license": "MIT",
55
"author": "Evan You",
66
"files": [
7-
"index.js",
8-
"index.d.ts"
7+
"dist"
98
],
10-
"main": "index.js",
11-
"types": "index.d.ts",
9+
"main": "./dist/index.cjs",
10+
"module": "./dist/index.mjs",
11+
"types": "./dist/index.d.ts",
12+
"exports": {
13+
".": {
14+
"types": "./dist/index.d.ts",
15+
"import": "./dist/index.mjs",
16+
"require": "./dist/index.cjs"
17+
}
18+
},
19+
"scripts": {
20+
"dev": "unbuild --stub",
21+
"build": "unbuild && pnpm run patch-cjs",
22+
"patch-cjs": "ts-node ../../scripts/patchCJS.ts",
23+
"prepublishOnly": "npm run build"
24+
},
1225
"engines": {
1326
"node": ">=14.6.0"
1427
},
@@ -28,6 +41,9 @@
2841
"@rollup/pluginutils": "^4.2.1",
2942
"@vue/babel-plugin-jsx": "^1.1.1"
3043
},
44+
"devDependencies": {
45+
"vite": "workspace:*"
46+
},
3147
"peerDependencies": {
3248
"vite": "^2.0.0",
3349
"vue": "^3.0.0"

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

+32-52
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
// @ts-check
2-
const babel = require('@babel/core')
3-
const jsx = require('@vue/babel-plugin-jsx')
4-
const importMeta = require('@babel/plugin-syntax-import-meta')
5-
const { createFilter, normalizePath } = require('@rollup/pluginutils')
6-
const { createHash } = require('crypto')
7-
const path = require('path')
1+
import { createHash } from 'crypto'
2+
import path from 'path'
3+
import type { types } from '@babel/core'
4+
import babel from '@babel/core'
5+
import jsx from '@vue/babel-plugin-jsx'
6+
// @ts-expect-error missing type
7+
import importMeta from '@babel/plugin-syntax-import-meta'
8+
import { createFilter, normalizePath } from '@rollup/pluginutils'
9+
import type { ComponentOptions } from 'vue'
10+
import type { Plugin } from 'vite'
11+
import type { Options } from './types'
12+
13+
export * from './types'
814

915
const ssrRegisterHelperId = '/__vue-jsx-ssr-register-helper'
1016
const ssrRegisterHelperCode =
@@ -14,10 +20,8 @@ const ssrRegisterHelperCode =
1420
/**
1521
* This function is serialized with toString() and evaluated as a virtual
1622
* module during SSR
17-
* @param {import('vue').ComponentOptions} comp
18-
* @param {string} filename
1923
*/
20-
function ssrRegisterHelper(comp, filename) {
24+
function ssrRegisterHelper(comp: ComponentOptions, filename: string) {
2125
const setup = comp.setup
2226
comp.setup = (props, ctx) => {
2327
// @ts-ignore
@@ -29,17 +33,7 @@ function ssrRegisterHelper(comp, filename) {
2933
}
3034
}
3135

32-
/**
33-
* @typedef { import('@rollup/pluginutils').FilterPattern} FilterPattern
34-
* @typedef { { include?: FilterPattern, exclude?: FilterPattern, babelPlugins?: any[] } } CommonOptions
35-
*/
36-
37-
/**
38-
*
39-
* @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions & CommonOptions} options
40-
* @returns {import('vite').Plugin}
41-
*/
42-
function vueJsxPlugin(options = {}) {
36+
function vueJsxPlugin(options: Options = {}): Plugin {
4337
let root = ''
4438
let needHmr = false
4539
let needSourceMap = true
@@ -110,31 +104,28 @@ function vueJsxPlugin(options = {}) {
110104
sourceMaps: needSourceMap,
111105
sourceFileName: id,
112106
configFile: false
113-
})
107+
})!
114108

115109
if (!ssr && !needHmr) {
110+
if (!result.code) return
116111
return {
117112
code: result.code,
118113
map: result.map
119114
}
120115
}
121116

117+
interface HotComponent {
118+
local: string
119+
exported: string
120+
id: string
121+
}
122+
122123
// check for hmr injection
123-
/**
124-
* @type {{ name: string }[]}
125-
*/
126-
const declaredComponents = []
127-
/**
128-
* @type {{
129-
* local: string,
130-
* exported: string,
131-
* id: string,
132-
* }[]}
133-
*/
134-
const hotComponents = []
124+
const declaredComponents: { name: string }[] = []
125+
const hotComponents: HotComponent[] = []
135126
let hasDefault = false
136127

137-
for (const node of result.ast.program.body) {
128+
for (const node of result.ast!.program.body) {
138129
if (node.type === 'VariableDeclaration') {
139130
const names = parseComponentDecls(node, code)
140131
if (names.length) {
@@ -204,7 +195,7 @@ function vueJsxPlugin(options = {}) {
204195
if (hotComponents.length) {
205196
if (hasDefault && (needHmr || ssr)) {
206197
result.code =
207-
result.code.replace(
198+
result.code!.replace(
208199
/export default defineComponent/g,
209200
`const __default__ = defineComponent`
210201
) + `\nexport default __default__`
@@ -239,6 +230,7 @@ function vueJsxPlugin(options = {}) {
239230
}
240231
}
241232

233+
if (!result.code) return
242234
return {
243235
code: result.code,
244236
map: result.map
@@ -248,11 +240,7 @@ function vueJsxPlugin(options = {}) {
248240
}
249241
}
250242

251-
/**
252-
* @param {import('@babel/core').types.VariableDeclaration} node
253-
* @param {string} source
254-
*/
255-
function parseComponentDecls(node, source) {
243+
function parseComponentDecls(node: types.VariableDeclaration, source: string) {
256244
const names = []
257245
for (const decl of node.declarations) {
258246
if (decl.id.type === 'Identifier' && isDefineComponentCall(decl.init)) {
@@ -264,10 +252,7 @@ function parseComponentDecls(node, source) {
264252
return names
265253
}
266254

267-
/**
268-
* @param {import('@babel/core').types.Node} node
269-
*/
270-
function isDefineComponentCall(node) {
255+
function isDefineComponentCall(node?: types.Node | null) {
271256
return (
272257
node &&
273258
node.type === 'CallExpression' &&
@@ -276,13 +261,8 @@ function isDefineComponentCall(node) {
276261
)
277262
}
278263

279-
/**
280-
* @param {string} text
281-
* @returns {string}
282-
*/
283-
function getHash(text) {
264+
function getHash(text: string) {
284265
return createHash('sha256').update(text).digest('hex').substring(0, 8)
285266
}
286267

287-
module.exports = vueJsxPlugin
288-
vueJsxPlugin.default = vueJsxPlugin
268+
export default vueJsxPlugin

packages/plugin-vue-jsx/src/types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { VueJSXPluginOptions } from '@vue/babel-plugin-jsx'
2+
import type { FilterPattern } from '@rollup/pluginutils'
3+
4+
export interface FilterOptions {
5+
include?: FilterPattern
6+
exclude?: FilterPattern
7+
}
8+
9+
export type Options = VueJSXPluginOptions &
10+
FilterOptions & { babelPlugins?: any[] }

packages/plugin-vue-jsx/tsconfig.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"include": ["src"],
3+
"exclude": ["**/*.spec.ts"],
4+
"compilerOptions": {
5+
"outDir": "dist",
6+
"target": "ES2018",
7+
"module": "CommonJS",
8+
"moduleResolution": "Node",
9+
"strict": true,
10+
"declaration": true,
11+
"sourceMap": true,
12+
"noUnusedLocals": true,
13+
"esModuleInterop": true,
14+
"paths": {
15+
"types/*": ["../vite/types/*"],
16+
"vite": ["../vite/src/node/index.js"]
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)