Skip to content

Commit 893cbf6

Browse files
committed
wip: adapt to vue2
1 parent 9288ef1 commit 893cbf6

11 files changed

+162
-353
lines changed

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@
4141
"devDependencies": {
4242
"@jridgewell/gen-mapping": "^0.3.1",
4343
"@jridgewell/trace-mapping": "^0.3.13",
44+
"@rollup/pluginutils": "^4.2.1",
4445
"debug": "^4.3.4",
4546
"esno": "^0.16.3",
47+
"hash-sum": "^2.0.0",
4648
"rollup": "^2.75.6",
4749
"slash": "^4.0.0",
4850
"source-map": "^0.6.1",
4951
"unbuild": "^0.7.4",
50-
"vite": "^3.0.0-alpha.12",
51-
"vue": "^2.7.0-alpha.9"
52+
"vite": "^2.9.12",
53+
"vue": "^2.7.0-alpha.12"
5254
}
5355
}

pnpm-lock.yaml

+20-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ import type * as _compiler from 'vue/compiler-sfc'
1010

1111
export function resolveCompiler(root: string): typeof _compiler {
1212
// resolve from project root first, then fallback to peer dep (if any)
13-
const compiler =
14-
tryRequire('vue/compiler-sfc', root) || tryRequire('vue/compiler-sfc')
13+
const compiler = tryRequire('vue/compiler-sfc', root)
1514

1615
if (!compiler) {
1716
throw new Error(
1817
`Failed to resolve vue/compiler-sfc.\n` +
19-
`@vitejs/plugin-vue requires vue (>=3.2.25) ` +
18+
`@vitejs/plugin-vue2 requires vue (>=2.7.0) ` +
2019
`to be present in the dependency tree.`
2120
)
2221
}
@@ -25,6 +24,7 @@ export function resolveCompiler(root: string): typeof _compiler {
2524
}
2625

2726
const _require = createRequire(import.meta.url)
27+
2828
function tryRequire(id: string, from?: string) {
2929
try {
3030
return from

src/handleHotUpdate.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _debug from 'debug'
21
import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc'
32
import type { HmrContext, ModuleNode } from 'vite'
43
import {
@@ -9,8 +8,6 @@ import {
98
import { getResolvedScript, setResolvedScript } from './script'
109
import type { ResolvedOptions } from '.'
1110

12-
const debug = _debug('vite:hmr')
13-
1411
const directRequestRE = /(\?|&)direct\b/
1512

1613
/**
@@ -75,9 +72,9 @@ export async function handleHotUpdate(
7572
const nextStyles = descriptor.styles || []
7673

7774
// force reload if CSS vars injection changed
78-
if (prevDescriptor.cssVars.join('') !== descriptor.cssVars.join('')) {
79-
affectedModules.add(mainModule)
80-
}
75+
// if (prevDescriptor.cssVars.join('') !== descriptor.cssVars.join('')) {
76+
// affectedModules.add(mainModule)
77+
// }
8178

8279
// force reload if scoped status has changed
8380
if (prevStyles.some((s) => s.scoped) !== nextStyles.some((s) => s.scoped)) {
@@ -156,9 +153,6 @@ export async function handleHotUpdate(
156153
if (didUpdateStyle) {
157154
updateType.push(`style`)
158155
}
159-
if (updateType.length) {
160-
debug(`[vue:update(${updateType.join('&')})] ${file}`)
161-
}
162156
return [...affectedModules].filter(Boolean) as ModuleNode[]
163157
}
164158

src/index.ts

+22-78
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import fs from 'fs'
22
import type { Plugin, ViteDevServer } from 'vite'
3-
import { createFilter } from 'vite'
4-
/* eslint-disable import/no-duplicates */
3+
import { createFilter } from '@rollup/pluginutils'
54
import type {
65
SFCBlock,
76
SFCScriptCompileOptions,
87
SFCStyleCompileOptions,
98
SFCTemplateCompileOptions
109
} from 'vue/compiler-sfc'
1110
import type * as _compiler from 'vue/compiler-sfc'
12-
/* eslint-enable import/no-duplicates */
1311
import { resolveCompiler } from './compiler'
1412
import { parseVueRequest } from './utils/query'
1513
import { getDescriptor, getSrcDescriptor } from './utils/descriptorCache'
@@ -34,32 +32,9 @@ export interface Options {
3432
template?: Partial<SFCTemplateCompileOptions>
3533
style?: Partial<SFCStyleCompileOptions>
3634

37-
/**
38-
* Transform Vue SFCs into custom elements.
39-
* - `true`: all `*.vue` imports are converted into custom elements
40-
* - `string | RegExp`: matched files are converted into custom elements
41-
*
42-
* @default /\.ce\.vue$/
43-
*/
44-
customElement?: boolean | string | RegExp | (string | RegExp)[]
45-
46-
/**
47-
* Enable Vue reactivity transform (experimental).
48-
* https://github.com/vuejs/core/tree/master/packages/reactivity-transform
49-
* - `true`: transform will be enabled for all vue,js(x),ts(x) files except
50-
* those inside node_modules
51-
* - `string | RegExp`: apply to vue + only matched files (will include
52-
* node_modules, so specify directories in necessary)
53-
* - `false`: disable in all cases
54-
*
55-
* @default false
56-
*/
57-
reactivityTransform?: boolean | string | RegExp | (string | RegExp)[]
58-
59-
/**
60-
* Use custom compiler-sfc instance. Can be used to force a specific version.
61-
*/
62-
compiler?: typeof _compiler
35+
// customElement?: boolean | string | RegExp | (string | RegExp)[]
36+
// reactivityTransform?: boolean | string | RegExp | (string | RegExp)[]
37+
// compiler?: typeof _compiler
6338
}
6439

6540
export interface ResolvedOptions extends Options {
@@ -74,33 +49,21 @@ export interface ResolvedOptions extends Options {
7449
export default function vuePlugin(rawOptions: Options = {}): Plugin {
7550
const {
7651
include = /\.vue$/,
77-
exclude,
78-
customElement = /\.ce\.vue$/,
79-
reactivityTransform = false
52+
exclude
53+
// customElement = /\.ce\.vue$/,
54+
// reactivityTransform = false
8055
} = rawOptions
8156

8257
const filter = createFilter(include, exclude)
8358

84-
const customElementFilter =
85-
typeof customElement === 'boolean'
86-
? () => customElement
87-
: createFilter(customElement)
88-
89-
const refTransformFilter =
90-
reactivityTransform === false
91-
? () => false
92-
: reactivityTransform === true
93-
? createFilter(/\.(j|t)sx?$/, /node_modules/)
94-
: createFilter(reactivityTransform)
95-
9659
let options: ResolvedOptions = {
9760
isProduction: process.env.NODE_ENV === 'production',
9861
compiler: null as any, // to be set in buildStart
9962
...rawOptions,
10063
include,
10164
exclude,
102-
customElement,
103-
reactivityTransform,
65+
// customElement,
66+
// reactivityTransform,
10467
root: process.cwd(),
10568
sourceMap: true,
10669
cssDevSourcemap: false,
@@ -117,25 +80,13 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
11780
return handleHotUpdate(ctx, options)
11881
},
11982

120-
config(config) {
121-
return {
122-
define: {
123-
__VUE_OPTIONS_API__: config.define?.__VUE_OPTIONS_API__ ?? true,
124-
__VUE_PROD_DEVTOOLS__: config.define?.__VUE_PROD_DEVTOOLS__ ?? false
125-
},
126-
ssr: {
127-
external: ['vue', '@vue/server-renderer']
128-
}
129-
}
130-
},
131-
13283
configResolved(config) {
13384
options = {
13485
...options,
13586
root: config.root,
87+
isProduction: config.isProduction,
13688
sourceMap: config.command === 'build' ? !!config.build.sourcemap : true,
13789
cssDevSourcemap: config.css?.devSourcemap ?? false,
138-
isProduction: config.isProduction,
13990
devToolsEnabled:
14091
!!config.define!.__VUE_PROD_DEVTOOLS__ || !config.isProduction
14192
}
@@ -146,7 +97,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
14697
},
14798

14899
buildStart() {
149-
options.compiler = options.compiler || resolveCompiler(options.root)
100+
options.compiler = resolveCompiler(options.root)
150101
},
151102

152103
async resolveId(id) {
@@ -200,29 +151,22 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
200151
return
201152
}
202153
if (!filter(filename) && !query.vue) {
203-
if (
204-
!query.vue &&
205-
refTransformFilter(filename) &&
206-
options.compiler.shouldTransformRef(code)
207-
) {
208-
return options.compiler.transformRef(code, {
209-
filename,
210-
sourceMap: true
211-
})
212-
}
154+
// if (
155+
// !query.vue &&
156+
// refTransformFilter(filename) &&
157+
// options.compiler.shouldTransformRef(code)
158+
// ) {
159+
// return options.compiler.transformRef(code, {
160+
// filename,
161+
// sourceMap: true
162+
// })
163+
// }
213164
return
214165
}
215166

216167
if (!query.vue) {
217168
// main request
218-
return transformMain(
219-
code,
220-
filename,
221-
options,
222-
this,
223-
ssr,
224-
customElementFilter(filename)
225-
)
169+
return transformMain(code, filename, options, this, ssr)
226170
} else {
227171
// sub block request
228172
const descriptor = query.src

0 commit comments

Comments
 (0)