Skip to content

Commit

Permalink
fix(webpack): throw a standard error
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Feb 6, 2024
1 parent e442dfa commit 41978c0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/rspack/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ export function createBuildContext(compilation: Compilation): UnpluginBuildConte
}

export function createContext(loader: LoaderContext): UnpluginContext {
function normalizeMessage(error: string | UnpluginMessage): Error {
return typeof error === 'string'
? new Error(error)
: error as Error
}
return {
error: error => loader.emitError(normalizeMessage(error)),
warn: message => loader.emitWarning(normalizeMessage(message)),
}
}

export function normalizeMessage(error: string | UnpluginMessage): Error {
const err = new Error(typeof error === 'string' ? error : error.message)
if (typeof error === 'object') {
err.stack = error.stack
err.cause = error.meta
}
return err
}
14 changes: 9 additions & 5 deletions src/webpack/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ export function createBuildContext(options: ContextOptions, compilation?: Compil
}

export function createContext(loader: LoaderContext<{ unpluginName: string }>): UnpluginContext {
function normalizeMessage(error: string | UnpluginMessage): Error {
return typeof error === 'string'
? new Error(error)
: error as Error
}
return {
error: error => loader.emitError(normalizeMessage(error)),
warn: message => loader.emitWarning(normalizeMessage(message)),
}
}

export function normalizeMessage(error: string | UnpluginMessage): Error {
const err = new Error(typeof error === 'string' ? error : error.message)
if (typeof error === 'object') {
err.stack = error.stack
err.cause = error.meta
}
return err
}
4 changes: 2 additions & 2 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import VirtualModulesPlugin from 'webpack-virtual-modules'
import type { ResolvePluginInstance, Resolver } from 'webpack'
import type { ResolvedUnpluginOptions, UnpluginContext, UnpluginContextMeta, UnpluginFactory, UnpluginInstance, WebpackCompiler } from '../types'
import { normalizeAbsolutePath, shouldLoad, toArray, transformUse } from '../utils'
import { contextOptionsFromCompilation, createBuildContext } from './context'
import { contextOptionsFromCompilation, createBuildContext, normalizeMessage } from './context'

const TRANSFORM_LOADER = resolve(
__dirname,
Expand Down Expand Up @@ -105,7 +105,7 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
const pluginContext: UnpluginContext = {
error(msg) {
if (error == null)
error = typeof msg === 'string' ? new Error(msg) : msg as Error
error = normalizeMessage(msg)
else
console.error(`unplugin/webpack: multiple errors returned from resolveId hook: ${msg}`)
},
Expand Down

0 comments on commit 41978c0

Please sign in to comment.