From 65b70dc5dba211235e14164d48e54eda87987811 Mon Sep 17 00:00:00 2001 From: likui <2218301630@qq.com> Date: Wed, 1 Jul 2020 11:35:25 +0800 Subject: [PATCH 1/2] feat: custom template compiler --- src/index.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 57f147e..c2f9091 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,8 @@ import { SFCTemplateCompileOptions, SFCTemplateCompileResults, SFCAsyncStyleCompileOptions, + TemplateCompiler, + CompilerOptions, } from '@vue/compiler-sfc' import fs from 'fs' import createDebugger from 'debug' @@ -28,6 +30,8 @@ import { createFilter } from 'rollup-pluginutils' const debug = createDebugger('rollup-plugin-vue') +type TemplateCompilerOptions = [TemplateCompiler, CompilerOptions] + export interface Options { include: string | RegExp | (string | RegExp)[] exclude: string | RegExp | (string | RegExp)[] @@ -44,6 +48,7 @@ export interface Options { compiler?: SFCTemplateCompileOptions['compiler'] compilerOptions?: SFCTemplateCompileOptions['compilerOptions'] transformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls'] + templateCompilers?: Record // sfc style options postcssOptions?: SFCAsyncStyleCompileOptions['postcssOptions'] @@ -139,6 +144,25 @@ export default function PluginVue(userOptions: Partial = {}): Plugin { 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 + ] + } else { + this.error({ + id: query.filename, + message: `The "${compilerKey}" compiler not found.Please add "templateCompilers" options.`, + }) + } + } + debug(`transform(${id})`) const block = descriptor.template! const result = compileTemplate({ @@ -147,10 +171,10 @@ export default function PluginVue(userOptions: Partial = {}): Plugin { inMap: query.src ? undefined : block.map, preprocessLang: block.lang, preprocessCustomRequire: options.preprocessCustomRequire, - compiler: options.compiler, + compiler, ssr: isServer, compilerOptions: { - ...options.compilerOptions, + ...compilerOptions, scopeId: hasScoped ? `data-v-${query.id}` : undefined, }, transformAssetUrls: options.transformAssetUrls, @@ -292,6 +316,7 @@ type Query = type: 'template' id?: string src?: true + compiler?: string } | { filename: string From 2180c5295cd663ec499e0e4c739b0924c7fc8c63 Mon Sep 17 00:00:00 2001 From: likui <2218301630@qq.com> Date: Thu, 2 Jul 2020 11:50:44 +0800 Subject: [PATCH 2/2] fix: fix some error --- src/index.ts | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index c2f9091..d04a0da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)[] @@ -48,7 +48,7 @@ export interface Options { compiler?: SFCTemplateCompileOptions['compiler'] compilerOptions?: SFCTemplateCompileOptions['compilerOptions'] transformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls'] - templateCompilers?: Record + templateCompilers?: Record // sfc style options postcssOptions?: SFCAsyncStyleCompileOptions['postcssOptions'] @@ -140,7 +140,6 @@ export default function PluginVue(userOptions: Partial = {}): 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') { @@ -148,17 +147,27 @@ export default function PluginVue(userOptions: Partial = {}): Plugin { 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.`, }) } }