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

Commit

Permalink
feat: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Jan 12, 2021
1 parent ee6575a commit 1c430ab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export async function transformStyle(
})

if (result.errors.length) {
result.errors.forEach((error: any) => {
pluginContext.error(error)
})
result.errors.forEach((error) =>
pluginContext.error({ id: filename, message: error })
)
return null
}

Expand Down
32 changes: 28 additions & 4 deletions src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import * as vueTemplateCompiler from 'vue-template-compiler'
import path from 'path'
import { TransformPluginContext } from 'rollup'
import { ResolvedOptions } from './index'
import { createRollupError } from './utils/error'

export function compileSFCTemplate(
source: string,
block: SFCBlock,
filename: string,
{ root, isProduction, vueTemplateOptions, devServer }: ResolvedOptions,
{ root, isProduction, vueTemplateOptions = {}, devServer }: ResolvedOptions,
pluginContext: TransformPluginContext
): string {
const { tips, errors, code } = compileTemplate({
Expand All @@ -29,12 +30,35 @@ export function compileSFCTemplate(
})

if (tips) {
tips.forEach(console.warn)
tips.forEach((warn) =>
pluginContext.error({
id: filename,
message: typeof warn === 'string' ? warn : warn.msg,
})
)
}

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

if (devServer) {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RollupError } from 'rollup'

export function createRollupError(id: string, error: any): RollupError {
;(error as RollupError).id = id
;(error as RollupError).plugin = 'vite-plugin-vue2'

return error as RollupError
}

0 comments on commit 1c430ab

Please sign in to comment.