Skip to content

Commit

Permalink
fix: emit error messages on webpack & esbuild
Browse files Browse the repository at this point in the history
closes #149
  • Loading branch information
sxzz committed Feb 6, 2024
1 parent b1f54c2 commit 8067705
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
},
"dependencies": {
"debug": "^4.3.4",
"unplugin": "~1.7.0",
"unplugin": "~1.7.1",
"vite": "^5.0.12"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from './script'
import { transformTemplateInMain } from './template'
import { isEqualBlock, isOnlyTemplateChanged } from './handleHotUpdate'
import { createRollupError } from './utils/error'
import { createError } from './utils/error'
import { EXPORT_HELPER_ID } from './helper'
import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc'
import type { PluginContext } from 'rollup'
Expand Down Expand Up @@ -64,9 +64,7 @@ export async function transformMain(
}

if (errors.length > 0) {
errors.forEach((error) =>
pluginContext.error(createRollupError(filename, error)),
)
errors.forEach((error) => pluginContext.error(createError(filename, error)))
return null
}

Expand Down
8 changes: 2 additions & 6 deletions src/core/template.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import slash from 'slash'
import { getResolvedScript, resolveScript } from './script'
import { createRollupError } from './utils/error'
import { createError } from './utils/error'
import type {
CompilerOptions,
SFCDescriptor,
Expand Down Expand Up @@ -100,11 +100,7 @@ export function compile(

if (result.errors.length > 0) {
result.errors.forEach((error) =>
pluginContext.error(
typeof error === 'string'
? { id: filename, message: error }
: createRollupError(filename, error),
),
pluginContext.error(createError(filename, error)),
)
}

Expand Down
20 changes: 11 additions & 9 deletions src/core/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import type { UnpluginMessage } from 'unplugin'
import type { CompilerError } from 'vue/compiler-sfc'
import type { RollupError } from 'rollup'

export function createRollupError(
export function createError(
id: string,
error: CompilerError | SyntaxError,
): RollupError {
error: CompilerError | SyntaxError | string,
): UnpluginMessage | string {
if (typeof error === 'string') {
return error
}

const { message, name, stack } = error
const rollupError: RollupError = {
const unpluginMessage: UnpluginMessage = {
id,
plugin: 'vue',
message,
name,
stack,
}

if ('code' in error && error.loc) {
rollupError.loc = {
unpluginMessage.loc = {
file: id,
line: error.loc.start.line,
column: error.loc.start.column,
}
}

return rollupError
return unpluginMessage
}

0 comments on commit 8067705

Please sign in to comment.