Skip to content

Commit b17b5ae

Browse files
committed
feat(plugin-vue): support optional @vue/compiler-sfc peer dep
1 parent 8926c96 commit b17b5ae

File tree

19 files changed

+298
-301
lines changed

19 files changed

+298
-301
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
"yorkie": "^2.0.0"
6161
},
6262
"peerDependencies": {
63+
"less": ">=4.0.0",
6364
"sass": ">=1.0.0",
64-
"stylus": ">=0.54.0",
65-
"less": ">=4.0.0"
65+
"stylus": ">=0.54.0"
6666
},
6767
"peerDependenciesMeta": {
6868
"sass": {

packages/playground/alias/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"serve": "vite preview"
1010
},
1111
"dependencies": {
12-
"vue": "^3.2.6"
12+
"vue": "^3.2.13"
1313
}
1414
}

packages/playground/extensions/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"serve": "vite preview"
1010
},
1111
"dependencies": {
12-
"vue": "^3.2.6"
12+
"vue": "^3.2.13"
1313
}
1414
}

packages/playground/optimize-deps/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"react": "^17.0.1",
2222
"react-dom": "^17.0.1",
2323
"resolve-linked": "0.0.0",
24-
"vue": "^3.2.6",
25-
"vuex": "^4.0.0-rc.2"
24+
"vue": "^3.2.13",
25+
"vuex": "^4.0.0"
2626
}
2727
}

packages/playground/preload/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
"serve": "vite preview"
1010
},
1111
"dependencies": {
12-
"vue": "^3.2.6",
13-
"vue-router": "^4.0.6"
12+
"vue": "^3.2.13",
13+
"vue-router": "^4.0.0"
1414
},
1515
"devDependencies": {
16-
"@vitejs/plugin-vue": "^1.0.0",
17-
"@vue/compiler-sfc": "^3.2.6"
16+
"@vitejs/plugin-vue": "^1.0.0"
1817
}
1918
}

packages/playground/ssr-vue/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
},
1616
"dependencies": {
1717
"example-external-component": "file:example-external-component",
18-
"vue": "^3.2.6",
19-
"vue-router": "^4.0.3"
18+
"vue": "^3.2.13",
19+
"vue-router": "^4.0.0"
2020
},
2121
"devDependencies": {
2222
"@vitejs/plugin-vue": "^1.0.0",
2323
"@vitejs/plugin-vue-jsx": "^1.1.2",
24-
"@vue/compiler-sfc": "^3.2.6",
25-
"@vue/server-renderer": "^3.2.6",
2624
"dep-import-type": "link:./dep-import-type",
2725
"compression": "^1.7.4",
2826
"cross-env": "^7.0.3",

packages/playground/ssr-vue/src/entry-server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createApp } from './main'
2-
import { renderToString } from '@vue/server-renderer'
2+
import { renderToString } from 'vue/server-renderer'
33

44
export async function render(url, manifest) {
55
const { app, router } = createApp()

packages/playground/tailwind/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
"dependencies": {
1212
"autoprefixer": "^10.3.0",
1313
"tailwindcss": "^2.2.4",
14-
"vue": "^3.2.6"
14+
"vue": "^3.2.13"
1515
},
1616
"devDependencies": {
1717
"@vitejs/plugin-vue": "^1.0.0",
18-
"@vue/compiler-sfc": "^3.2.6",
19-
"vue-router": "^4.0.10"
18+
"vue-router": "^4.0.0"
2019
}
2120
}

packages/playground/vue/package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
"serve": "vite preview"
1010
},
1111
"dependencies": {
12-
"lodash-es": "^4.17.20",
13-
"vue": "^3.2.6"
12+
"lodash-es": "^4.17.20"
1413
},
1514
"devDependencies": {
1615
"@vitejs/plugin-vue": "^1.0.0",
17-
"@vue/compiler-sfc": "^3.2.6",
1816
"js-yaml": "^3.14.1",
1917
"less": "^3.13.0",
2018
"pug": "^3.0.0",

packages/plugin-vue/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @vitejs/plugin-vue [![npm](https://img.shields.io/npm/v/@vitejs/plugin-vue.svg)](https://npmjs.com/package/@vitejs/plugin-vue)
22

3-
Note: requires `@vue/compiler-sfc` as peer dependency. This is largely a port of `rollup-plugin-vue` with some vite-specific tweaks.
3+
> Note: as of `vue` 3.2.13+ and `@vitejs/plugin-vue` 1.9.0+, `@vue/compiler-sfc` is no longer required as a peer dependency.
44
55
```js
66
// vite.config.js
@@ -46,7 +46,7 @@ export interface Options {
4646
*/
4747
refTransform?: boolean | string | RegExp | (string | RegExp)[]
4848

49-
// options to pass on to @vue/compiler-sfc
49+
// options to pass on to vue/compiler-sfc
5050
script?: Partial<SFCScriptCompileOptions>
5151
template?: Partial<SFCTemplateCompileOptions>
5252
style?: Partial<SFCStyleCompileOptions>

packages/plugin-vue/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"dev": "tsc -p . -w --incremental",
1313
"build": "rimraf dist && run-s build-bundle build-types",
14-
"build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@vue/compiler-sfc --external:vite --outfile=dist/index.js",
14+
"build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@vue/compiler-sfc --external:vue/compiler-sfc --external:vite --outfile=dist/index.js",
1515
"build-types": "tsc -p . --emitDeclarationOnly --outDir temp && api-extractor run && rimraf temp",
1616
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-vue",
1717
"release": "node ../../scripts/release.js"
@@ -29,17 +29,17 @@
2929
},
3030
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-vue#readme",
3131
"peerDependencies": {
32-
"@vue/compiler-sfc": "^3.2.6",
3332
"vite": "^2.5.10"
3433
},
3534
"devDependencies": {
3635
"@rollup/pluginutils": "^4.1.1",
3736
"@types/hash-sum": "^1.0.0",
38-
"@vue/compiler-sfc": "^3.2.12",
3937
"debug": "^4.3.2",
4038
"hash-sum": "^2.0.0",
4139
"rollup": "^2.38.5",
4240
"slash": "^3.0.0",
43-
"source-map": "^0.6.1"
41+
"source-map": "^0.6.1",
42+
"vue": "^3.2.13",
43+
"@vue/compiler-sfc": "^3.2.13"
4444
}
4545
}

packages/plugin-vue/src/compiler.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// extend the descriptor so we can store the scopeId on it
2+
declare module '@vue/compiler-sfc' {
3+
interface SFCDescriptor {
4+
id: string
5+
}
6+
}
7+
8+
import * as _compiler from '@vue/compiler-sfc'
9+
10+
export let compiler: typeof _compiler
11+
12+
try {
13+
// Vue 3.2.13+ ships the SFC compiler directly under the `vue` package
14+
// making it no longer necessary to have @vue/compiler-sfc separately installed.
15+
compiler = require('vue/compiler-sfc')
16+
} catch (e) {
17+
try {
18+
compiler = require('@vue/compiler-sfc')
19+
} catch (e) {
20+
throw new Error(
21+
`@vitejs/plugin-vue requires vue (>=3.2.13) or @vue/compiler-sfc ` +
22+
`to be present in the dependency tree.`
23+
)
24+
}
25+
}

packages/plugin-vue/src/index.ts

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
try {
2-
require.resolve('@vue/compiler-sfc')
3-
} catch (e) {
4-
throw new Error(
5-
'@vitejs/plugin-vue requires @vue/compiler-sfc to be present in the dependency ' +
6-
'tree.'
7-
)
8-
}
9-
101
import fs from 'fs'
112
import { Plugin, ViteDevServer } from 'vite'
123
import { createFilter } from '@rollup/pluginutils'
134
import {
145
SFCBlock,
156
SFCScriptCompileOptions,
167
SFCStyleCompileOptions,
17-
SFCTemplateCompileOptions,
18-
shouldTransformRef,
19-
transformRef
8+
SFCTemplateCompileOptions
209
} from '@vue/compiler-sfc'
10+
import { compiler } from './compiler'
2111
import { parseVueRequest } from './utils/query'
2212
import { getDescriptor } from './utils/descriptorCache'
2313
import { getResolvedScript } from './script'
@@ -27,13 +17,6 @@ import { transformTemplateAsModule } from './template'
2717
import { transformStyle } from './style'
2818
import { EXPORT_HELPER_ID, helperCode } from './helper'
2919

30-
// extend the descriptor so we can store the scopeId on it
31-
declare module '@vue/compiler-sfc' {
32-
interface SFCDescriptor {
33-
id: string
34-
}
35-
}
36-
3720
export { parseVueRequest, VueQuery } from './utils/query'
3821

3922
export interface Options {
@@ -108,7 +91,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
10891
: createFilter(refTransform)
10992

11093
// compat for older verisons
111-
const canUseRefTransform = typeof shouldTransformRef === 'function'
94+
const canUseRefTransform = typeof compiler.shouldTransformRef === 'function'
11295

11396
let options: ResolvedOptions = {
11497
isProduction: process.env.NODE_ENV === 'production',
@@ -209,8 +192,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
209192
if (!query.vue && refTransformFilter(filename)) {
210193
if (!canUseRefTransform) {
211194
this.warn('refTransform requires @vue/compiler-sfc@^3.2.5.')
212-
} else if (shouldTransformRef(code)) {
213-
return transformRef(code, {
195+
} else if (compiler.shouldTransformRef(code)) {
196+
return compiler.transformRef(code, {
214197
filename,
215198
sourceMap: true
216199
})

packages/plugin-vue/src/main.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import qs from 'querystring'
22
import path from 'path'
3-
import { rewriteDefault, SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
3+
import { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
4+
import { compiler } from './compiler'
45
import { ResolvedOptions } from '.'
56
import {
67
createDescriptor,
@@ -271,7 +272,7 @@ async function genScriptCode(
271272
// If the script is js/ts and has no external src, it can be directly placed
272273
// in the main module.
273274
if ((!script.lang || script.lang === 'ts') && !script.src) {
274-
scriptCode = rewriteDefault(script.content, '_sfc_main')
275+
scriptCode = compiler.rewriteDefault(script.content, '_sfc_main')
275276
map = script.map
276277
} else {
277278
if (script.src) {

packages/plugin-vue/src/script.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { compileScript, SFCDescriptor, SFCScriptBlock } from '@vue/compiler-sfc'
1+
import { SFCDescriptor, SFCScriptBlock } from '@vue/compiler-sfc'
22
import { ResolvedOptions } from '.'
33
import { resolveTemplateCompilerOptions } from './template'
4+
import { compiler } from './compiler'
45

56
// ssr and non ssr builds would output different script content
67
const clientCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
@@ -38,7 +39,7 @@ export function resolveScript(
3839

3940
let resolved: SFCScriptBlock | null = null
4041

41-
resolved = compileScript(descriptor, {
42+
resolved = compiler.compileScript(descriptor, {
4243
...options.script,
4344
id: descriptor.id,
4445
isProd: options.isProduction,

packages/plugin-vue/src/style.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { compileStyleAsync, SFCDescriptor } from '@vue/compiler-sfc'
1+
import { SFCDescriptor } from '@vue/compiler-sfc'
22
import { TransformPluginContext } from 'rollup'
33
import { ResolvedOptions } from '.'
4+
import { compiler } from './compiler'
45

56
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
67
export async function transformStyle(
@@ -13,7 +14,7 @@ export async function transformStyle(
1314
const block = descriptor.styles[index]
1415
// vite already handles pre-processors and CSS module so this is only
1516
// applying SFC-specific transforms like scoped mode and CSS vars rewrite (v-bind(var))
16-
const result = await compileStyleAsync({
17+
const result = await compiler.compileStyleAsync({
1718
...options.style,
1819
filename: descriptor.filename,
1920
id: `data-v-${descriptor.id}`,

packages/plugin-vue/src/template.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import path from 'path'
22
import slash from 'slash'
33
import {
4-
compileTemplate,
54
SFCDescriptor,
65
SFCTemplateCompileOptions,
76
SFCTemplateCompileResults,
87
CompilerOptions
9-
} from '@vue/compiler-sfc'
8+
} from 'vue/compiler-sfc'
109
import { PluginContext, TransformPluginContext } from 'rollup'
1110
import { ResolvedOptions } from '.'
1211
import { getResolvedScript } from './script'
1312
import { createRollupError } from './utils/error'
13+
import { compiler } from './compiler'
1414

1515
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1616
export async function transformTemplateAsModule(
@@ -70,7 +70,7 @@ export function compile(
7070
ssr: boolean
7171
) {
7272
const filename = descriptor.filename
73-
const result = compileTemplate({
73+
const result = compiler.compileTemplate({
7474
...resolveTemplateCompilerOptions(descriptor, options, ssr)!,
7575
source: code
7676
})

packages/plugin-vue/src/utils/descriptorCache.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import fs from 'fs'
22
import path from 'path'
33
import slash from 'slash'
44
import hash from 'hash-sum'
5-
import { CompilerError, parse, SFCDescriptor } from '@vue/compiler-sfc'
5+
import { CompilerError, SFCDescriptor } from '@vue/compiler-sfc'
66
import { ResolvedOptions } from '..'
7+
import { compiler } from '../compiler'
78

89
// node_modules/@vue/compiler-sfc/dist/compiler-sfc.d.ts SFCParseResult should be exported so it can be re-used
910
export interface SFCParseResult {
@@ -19,7 +20,7 @@ export function createDescriptor(
1920
source: string,
2021
{ root, isProduction, sourceMap }: ResolvedOptions
2122
): SFCParseResult {
22-
const { descriptor, errors } = parse(source, {
23+
const { descriptor, errors } = compiler.parse(source, {
2324
filename,
2425
sourceMap
2526
})

0 commit comments

Comments
 (0)