Skip to content

Commit e07490e

Browse files
committedDec 12, 2021
feat: support reactivityTransform option
BREAKING CHANGE: remove `refSugar` option, require `vue@^3.2.13`
1 parent b391b04 commit e07490e

12 files changed

+45
-58
lines changed
 

‎README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
55
- [Documentation](https://vue-loader.vuejs.org)
66

7-
## v16 Only Options
7+
## v16+ Only Options
88

9-
- `refSugar: boolean`: enable experimental ref sugar.
9+
- `reactivityTransform: boolean`: enable [Vue Reactivity Transform](https://github.com/vuejs/rfcs/discussions/369) (SFCs only).
10+
11+
- ~~`refSugar: boolean`: **removed.** use `reactivityTransform` instead.~~
1012

1113
- `customElement: boolean | RegExp`: enable custom elements mode. An SFC loaded in custom elements mode inlines its `<style>` tags as strings under the component's `styles` option. When used with `defineCustomElement` from Vue core, the styles will be injected into the custom element's shadow root.
1214

‎example/webpack.config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ module.exports = (env = {}) => {
5454
test: /\.vue$/,
5555
loader: 'vue-loader',
5656
options: {
57-
refSugar: true,
58-
// enableTsInTemplate: false,
57+
reactivityTransform: true,
5958
},
6059
},
6160
{

‎package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"@types/mini-css-extract-plugin": "^0.9.1",
5252
"@types/webpack": "^4.41.0",
5353
"@types/webpack-merge": "^4.1.5",
54-
"@vue/compiler-sfc": "^3.2.12",
5554
"babel-loader": "^8.1.0",
5655
"cache-loader": "^4.1.0",
5756
"conventional-changelog-cli": "^2.1.1",
@@ -81,7 +80,7 @@
8180
"ts-loader-v9": "npm:ts-loader@^9.2.4",
8281
"typescript": "^4.4.3",
8382
"url-loader": "^4.1.0",
84-
"vue": "^3.2.13",
83+
"vue": "^3.2.25",
8584
"vue-i18n": "^9.1.7",
8685
"webpack": "^4.41.2",
8786
"webpack-cli": "^3.3.10",

‎src/compiler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// extend the descriptor so we can store the scopeId on it
2-
declare module '@vue/compiler-sfc' {
2+
declare module 'vue/compiler-sfc' {
33
interface SFCDescriptor {
44
id: string
55
}
66
}
77

8-
import * as _compiler from '@vue/compiler-sfc'
8+
import * as _compiler from 'vue/compiler-sfc'
99

1010
export let compiler: typeof _compiler
1111

‎src/descriptorCache.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs'
2-
import type { SFCDescriptor } from '@vue/compiler-sfc'
3-
import { compiler } from './compiler'
2+
import type { SFCDescriptor } from 'vue/compiler-sfc'
3+
import { parse } from 'vue/compiler-sfc'
44

55
const cache = new Map<string, SFCDescriptor>()
66

@@ -20,7 +20,7 @@ export function getDescriptor(filename: string): SFCDescriptor {
2020
// loaders being run in separate threads. The only way to deal with this is to
2121
// read from disk directly...
2222
const source = fs.readFileSync(filename, 'utf-8')
23-
const { descriptor } = compiler.parse(source, {
23+
const { descriptor } = parse(source, {
2424
filename,
2525
sourceMap: true,
2626
})

‎src/formatError.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { CompilerError } from '@vue/compiler-sfc'
2-
import { compiler } from './compiler'
1+
import type { CompilerError } from 'vue/compiler-sfc'
2+
import { generateCodeFrame } from 'vue/compiler-sfc'
33
import chalk = require('chalk')
44

55
export function formatError(
@@ -13,11 +13,7 @@ export function formatError(
1313
}
1414
const locString = `:${loc.start.line}:${loc.start.column}`
1515
const filePath = chalk.gray(`at ${file}${locString}`)
16-
const codeframe = compiler.generateCodeFrame(
17-
source,
18-
loc.start.offset,
19-
loc.end.offset
20-
)
16+
const codeframe = generateCodeFrame(source, loc.start.offset, loc.end.offset)
2117
err.message = `\n${chalk.red(
2218
`VueCompilerError: ${err.message}`
2319
)}\n${filePath}\n${chalk.yellow(codeframe)}\n`

‎src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import * as loaderUtils from 'loader-utils'
55

66
import hash = require('hash-sum')
77

8-
import { compiler } from './compiler'
8+
import { parse } from 'vue/compiler-sfc'
99
import type {
1010
TemplateCompiler,
1111
CompilerOptions,
1212
SFCBlock,
1313
SFCTemplateCompileOptions,
1414
SFCScriptCompileOptions,
15-
} from '@vue/compiler-sfc'
15+
} from 'vue/compiler-sfc'
1616
import { selectBlock } from './select'
1717
import { genHotReloadCode } from './hotReload'
1818
import { genCSSModulesCode } from './cssModules'
@@ -30,7 +30,7 @@ export interface VueLoaderOptions {
3030
transformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls']
3131
compiler?: TemplateCompiler | string
3232
compilerOptions?: CompilerOptions
33-
refSugar?: boolean
33+
reactivityTransform?: boolean
3434
customElement?: boolean | RegExp
3535

3636
hotReload?: boolean
@@ -88,7 +88,7 @@ export default function loader(
8888
mode === 'production' || process.env.NODE_ENV === 'production'
8989

9090
const filename = resourcePath.replace(/\?.*$/, '')
91-
const { descriptor, errors } = compiler.parse(source, {
91+
const { descriptor, errors } = parse(source, {
9292
filename,
9393
sourceMap,
9494
})

‎src/resolveScript.ts

+20-29
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type {
33
SFCDescriptor,
44
SFCScriptBlock,
55
TemplateCompiler,
6-
} from '@vue/compiler-sfc'
6+
} from 'vue/compiler-sfc'
77
import type { VueLoaderOptions } from 'src'
88
import { resolveTemplateTSOptions } from './util'
9-
import { compiler } from './compiler'
9+
import { compileScript } from 'vue/compiler-sfc'
1010

1111
const clientCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
1212
const serverCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
@@ -53,34 +53,25 @@ export function resolveScript(
5353
templateCompiler = options.compiler
5454
}
5555

56-
if (compiler.compileScript) {
57-
try {
58-
resolved = compiler.compileScript(descriptor, {
59-
id: scopeId,
60-
isProd,
61-
inlineTemplate: enableInline,
62-
refSugar: options.refSugar,
63-
babelParserPlugins: options.babelParserPlugins,
64-
templateOptions: {
65-
ssr: isServer,
66-
compiler: templateCompiler,
67-
compilerOptions: {
68-
...options.compilerOptions,
69-
...resolveTemplateTSOptions(descriptor, options),
70-
},
71-
transformAssetUrls: options.transformAssetUrls || true,
56+
try {
57+
resolved = compileScript(descriptor, {
58+
id: scopeId,
59+
isProd,
60+
inlineTemplate: enableInline,
61+
reactivityTransform: options.reactivityTransform,
62+
babelParserPlugins: options.babelParserPlugins,
63+
templateOptions: {
64+
ssr: isServer,
65+
compiler: templateCompiler,
66+
compilerOptions: {
67+
...options.compilerOptions,
68+
...resolveTemplateTSOptions(descriptor, options),
7269
},
73-
})
74-
} catch (e) {
75-
loaderContext.emitError(e)
76-
}
77-
} else if (descriptor.scriptSetup) {
78-
loaderContext.emitError(
79-
`<script setup> is not supported by the installed version of ` +
80-
`@vue/compiler-sfc - please upgrade.`
81-
)
82-
} else {
83-
resolved = descriptor.script
70+
transformAssetUrls: options.transformAssetUrls || true,
71+
},
72+
})
73+
} catch (e) {
74+
loaderContext.emitError(e)
8475
}
8576

8677
cacheToUse.set(descriptor, resolved)

‎src/select.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import webpack = require('webpack')
2-
import type { SFCDescriptor } from '@vue/compiler-sfc'
2+
import type { SFCDescriptor } from 'vue/compiler-sfc'
33
import type { ParsedUrlQuery } from 'querystring'
44
import { resolveScript } from './resolveScript'
55
import type { VueLoaderOptions } from 'src'

‎src/stylePostLoader.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as qs from 'querystring'
2-
import { compiler } from './compiler'
32
import webpack = require('webpack')
3+
import { compileStyle } from 'vue/compiler-sfc'
44

55
// This is a post loader that handles scoped CSS transforms.
66
// Injected right before css-loader by the global pitcher (../pitch.js)
77
// for any <style scoped> selection requests initiated from within vue files.
88
const StylePostLoader: webpack.loader.Loader = function (source, inMap) {
99
const query = qs.parse(this.resourceQuery.slice(1))
10-
const { code, map, errors } = compiler.compileStyle({
10+
const { code, map, errors } = compileStyle({
1111
source: source as string,
1212
filename: this.resourcePath,
1313
id: `data-v-${query.id}`,

‎src/templateLoader.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import * as qs from 'querystring'
33
import * as loaderUtils from 'loader-utils'
44
import { VueLoaderOptions } from './'
55
import { formatError } from './formatError'
6-
import type { TemplateCompiler } from '@vue/compiler-sfc'
6+
import type { TemplateCompiler } from 'vue/compiler-sfc'
77
import { getDescriptor } from './descriptorCache'
88
import { resolveScript } from './resolveScript'
99
import { resolveTemplateTSOptions } from './util'
10-
import { compiler } from './compiler'
10+
import { compileTemplate } from 'vue/compiler-sfc'
1111

1212
// Loader that compiles raw template into JavaScript functions.
1313
// This is injected by the global pitcher (../pitch) for template
@@ -42,7 +42,7 @@ const TemplateLoader: webpack.loader.Loader = function (source, inMap) {
4242
templateCompiler = options.compiler
4343
}
4444

45-
const compiled = compiler.compileTemplate({
45+
const compiled = compileTemplate({
4646
source,
4747
filename: loaderContext.resourcePath,
4848
inMap,

‎src/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SFCDescriptor, CompilerOptions } from '@vue/compiler-sfc'
1+
import type { SFCDescriptor, CompilerOptions } from 'vue/compiler-sfc'
22
import type { VueLoaderOptions } from '.'
33

44
export function resolveTemplateTSOptions(

0 commit comments

Comments
 (0)