Skip to content
This repository has been 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 1 commit
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
Prev Previous commit
Next Next commit
fix: fix some error
  • Loading branch information
underfin committed Jul 2, 2020
commit 2180c5295cd663ec499e0e4c739b0924c7fc8c63
31 changes: 20 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { createFilter } from 'rollup-pluginutils'

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

type TemplateCompilerOptions = [TemplateCompiler, CompilerOptions]
type TemplateCompilers = TemplateCompiler | [TemplateCompiler, CompilerOptions]

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

// sfc style options
postcssOptions?: SFCAsyncStyleCompileOptions['postcssOptions']
Expand Down Expand Up @@ -140,25 +140,34 @@ 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.type === 'template') {
const compilerKey = query.compiler
let compiler = options.compiler
let compilerOptions = options.compilerOptions || {}
if (compilerKey) {
if (
options.templateCompilers &&
options.templateCompilers[compilerKey]
) {
;[compiler, compilerOptions] = options.templateCompilers[
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: `The "${compilerKey}" compiler not found.Please add "templateCompilers" options.`,
message: `Please ensure custom template compiler attribute.`,
})
}
}
Expand Down