Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
feat: add vueTemplateCompiler option
Browse files Browse the repository at this point in the history
fix: #76
  • Loading branch information
underfin committed Jun 3, 2021
1 parent 0de32c9 commit 3274c05
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ViteDevServer, Plugin } from 'vite'
import { SFCBlock } from '@vue/component-compiler-utils'
import { handleHotUpdate } from './hmr'
import { transformVueJsx } from './jsxTransform'
import { VueTemplateCompiler } from './template/types'

export const vueComponentNormalizer = '\0/vite/vueComponentNormalizer'
export const vueHotReload = '\0/vite/vueHotReload'
Expand All @@ -26,6 +27,10 @@ declare module '@vue/component-compiler-utils' {
export interface VueViteOptions {
include?: string | RegExp | (string | RegExp)[]
exclude?: string | RegExp | (string | RegExp)[]
/**
* Custom vue template compiler.
*/
vueTemplateCompiler?: VueTemplateCompiler
/**
* The options for `@vue/component-compiler-utils`.
*/
Expand Down
19 changes: 14 additions & 5 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SFCBlock } from '@vue/component-compiler-utils'
import * as vueTemplateCompiler from 'vue-template-compiler'
import * as defaultVueTemplateCompiler from 'vue-template-compiler'
import { TransformPluginContext } from 'rollup'
import { ResolvedOptions } from './index'
import { createRollupError } from './utils/error'
Expand All @@ -10,7 +10,11 @@ export function compileSFCTemplate(
source: string,
block: SFCBlock,
filename: string,
{ root, isProduction, vueTemplateOptions = {}, devServer }: ResolvedOptions,
{
isProduction,
vueTemplateOptions = {},
vueTemplateCompiler = defaultVueTemplateCompiler as any,
}: ResolvedOptions,
pluginContext: TransformPluginContext
) {
const { tips, errors, code } = compileTemplate({
Expand Down Expand Up @@ -43,17 +47,22 @@ export function compileSFCTemplate(
}

if (errors) {
const generateCodeFrame = (vueTemplateCompiler as any).generateCodeFrame
errors.forEach((error) => {
// 2.6 compiler outputs errors as objects with range
if (
vueTemplateCompiler.generateCodeFrame &&
generateCodeFrame &&
vueTemplateOptions.compilerOptions?.outputSourceRange
) {
const { msg, start, end } = error as vueTemplateCompiler.ErrorWithRange
const {
msg,
start,
end,
} = error as defaultVueTemplateCompiler.ErrorWithRange
return pluginContext.error(
createRollupError(filename, {
message: msg,
frame: vueTemplateCompiler.generateCodeFrame(source, start, end),
frame: generateCodeFrame(source, start, end),
})
)
} else {
Expand Down

0 comments on commit 3274c05

Please sign in to comment.