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

feat: custom template compiler #368

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
SFCTemplateCompileOptions,
SFCTemplateCompileResults,
SFCAsyncStyleCompileOptions,
TemplateCompiler,
CompilerOptions,
} from '@vue/compiler-sfc'
import fs from 'fs'
import createDebugger from 'debug'
Expand All @@ -29,6 +31,8 @@ import { createFilter } from 'rollup-pluginutils'

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

type TemplateCompilers = TemplateCompiler | [TemplateCompiler, CompilerOptions]

export interface Options {
include: string | RegExp | (string | RegExp)[]
exclude: string | RegExp | (string | RegExp)[]
Expand All @@ -49,6 +53,7 @@ export interface Options {
compiler?: SFCTemplateCompileOptions['compiler']
compilerOptions?: SFCTemplateCompileOptions['compilerOptions']
transformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls']
templateCompilers?: Record<string, TemplateCompilers>

// sfc style options
postcssOptions?: SFCAsyncStyleCompileOptions['postcssOptions']
Expand Down Expand Up @@ -140,14 +145,42 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
const query = parseVuePartRequest(id)
if (query.vue) {
if (!query.src && !filter(query.filename)) return null

const descriptor = getDescriptor(query.filename)
const hasScoped = descriptor.styles.some((s) => s.scoped)
if (query.src) {
this.addWatchFile(query.filename);
}

if (query.type === 'template') {
const compilerKey = query.compiler
let compiler = options.compiler
let compilerOptions = options.compilerOptions || {}
if (compilerKey) {
if (typeof compilerKey === 'string') {
if (
options.templateCompilers &&
options.templateCompilers[compilerKey]
) {
const compilers = options.templateCompilers[compilerKey]
if (Array.isArray(compilers)) {
;[compiler, compilerOptions] = compilers
} else {
compiler = compilers
}
} else {
this.error({
id: query.filename,
message: `The "${compilerKey}" compiler not found.Please add "templateCompilers" options.`,
})
}
} else {
this.error({
id: query.filename,
message: `Please ensure custom template compiler attribute.`,
})
}
}

debug(`transform(${id})`)
const block = descriptor.template!
const preprocessLang = block.lang
Expand All @@ -162,10 +195,10 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
preprocessLang,
preprocessOptions,
preprocessCustomRequire: options.preprocessCustomRequire,
compiler: options.compiler,
compiler,
ssr: isServer,
compilerOptions: {
...options.compilerOptions,
...compilerOptions,
scopeId: hasScoped ? `data-v-${query.id}` : undefined,
bindingMetadata: descriptor.script
? descriptor.script.bindings
Expand Down Expand Up @@ -338,6 +371,7 @@ type Query =
type: 'template'
id?: string
src?: true
compiler?: string
}
| {
filename: string
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy