Skip to content

Commit d18ab9e

Browse files
committed
feat(plugin-vue): support optional @vue/compiler-sfc peer dep
1 parent 8da60e3 commit d18ab9e

File tree

10 files changed

+53
-41
lines changed

10 files changed

+53
-41
lines changed

Diff for: 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": {

Diff for: 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>

Diff for: 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
}

Diff for: 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+
}

Diff for: 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
})

Diff for: 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) {

Diff for: 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,

Diff for: 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}`,

Diff for: 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
})

Diff for: 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)