Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 65b70dc

Browse files
committed
feat: custom template compiler
1 parent 1f71e17 commit 65b70dc

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/index.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
SFCTemplateCompileOptions,
1818
SFCTemplateCompileResults,
1919
SFCAsyncStyleCompileOptions,
20+
TemplateCompiler,
21+
CompilerOptions,
2022
} from '@vue/compiler-sfc'
2123
import fs from 'fs'
2224
import createDebugger from 'debug'
@@ -28,6 +30,8 @@ import { createFilter } from 'rollup-pluginutils'
2830

2931
const debug = createDebugger('rollup-plugin-vue')
3032

33+
type TemplateCompilerOptions = [TemplateCompiler, CompilerOptions]
34+
3135
export interface Options {
3236
include: string | RegExp | (string | RegExp)[]
3337
exclude: string | RegExp | (string | RegExp)[]
@@ -44,6 +48,7 @@ export interface Options {
4448
compiler?: SFCTemplateCompileOptions['compiler']
4549
compilerOptions?: SFCTemplateCompileOptions['compilerOptions']
4650
transformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls']
51+
templateCompilers?: Record<string, TemplateCompilerOptions>
4752

4853
// sfc style options
4954
postcssOptions?: SFCAsyncStyleCompileOptions['postcssOptions']
@@ -139,6 +144,25 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
139144
const descriptor = getDescriptor(query.filename)
140145
const hasScoped = descriptor.styles.some((s) => s.scoped)
141146
if (query.type === 'template') {
147+
const compilerKey = query.compiler
148+
let compiler = options.compiler
149+
let compilerOptions = options.compilerOptions || {}
150+
if (compilerKey) {
151+
if (
152+
options.templateCompilers &&
153+
options.templateCompilers[compilerKey]
154+
) {
155+
;[compiler, compilerOptions] = options.templateCompilers[
156+
compilerKey
157+
]
158+
} else {
159+
this.error({
160+
id: query.filename,
161+
message: `The "${compilerKey}" compiler not found.Please add "templateCompilers" options.`,
162+
})
163+
}
164+
}
165+
142166
debug(`transform(${id})`)
143167
const block = descriptor.template!
144168
const result = compileTemplate({
@@ -147,10 +171,10 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
147171
inMap: query.src ? undefined : block.map,
148172
preprocessLang: block.lang,
149173
preprocessCustomRequire: options.preprocessCustomRequire,
150-
compiler: options.compiler,
174+
compiler,
151175
ssr: isServer,
152176
compilerOptions: {
153-
...options.compilerOptions,
177+
...compilerOptions,
154178
scopeId: hasScoped ? `data-v-${query.id}` : undefined,
155179
},
156180
transformAssetUrls: options.transformAssetUrls,
@@ -292,6 +316,7 @@ type Query =
292316
type: 'template'
293317
id?: string
294318
src?: true
319+
compiler?: string
295320
}
296321
| {
297322
filename: string

0 commit comments

Comments
 (0)