Skip to content

Commit ab5c475

Browse files
authored
chore: use unbuild to bundle plugins (#8139)
1 parent da6bbaa commit ab5c475

File tree

6 files changed

+91
-70
lines changed

6 files changed

+91
-70
lines changed

build.config.ts

+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+
})

index.d.ts

-14
This file was deleted.

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"

index.js renamed to 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

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[] }

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)