Skip to content

Commit 3209438

Browse files
authored
fix(minify): add error context to minification errors (#10)
* Initial plan * fix(minify): add error context (file name, location, source snippet) to minification errors Agent-Logs-Url: https://github.com/fz6m/lightningcss-loader/sessions/7bee3cf6-fc1a-4c91-9306-bbfc4cfdddf8 * fix(minify): address review feedback - use unknown type and guard loc.column Agent-Logs-Url: https://github.com/fz6m/lightningcss-loader/sessions/7bee3cf6-fc1a-4c91-9306-bbfc4cfdddf8 * fix(minify): use template strings, array join, and Error cause per review Agent-Logs-Url: https://github.com/fz6m/lightningcss-loader/sessions/9fa6e350-3a04-4ac0-9969-7ef873718216 * chore: revert unintended pnpm-lock.yaml changes Agent-Logs-Url: https://github.com/fz6m/lightningcss-loader/sessions/9fa6e350-3a04-4ac0-9969-7ef873718216 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 7f3601a commit 3209438

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/minify.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,48 @@ export class LightningCssMinifyPlugin {
113113
key: ECacheKey.minify,
114114
})
115115

116-
const result = await this.transform({
117-
filename: asset.name,
118-
code,
119-
minify: true,
120-
sourceMap: sourcemap,
121-
targets,
122-
...transformFeatureOptions,
123-
...transformOptions,
124-
})
116+
let result
117+
try {
118+
result = await this.transform({
119+
filename: asset.name,
120+
code,
121+
minify: true,
122+
sourceMap: sourcemap,
123+
targets,
124+
...transformFeatureOptions,
125+
...transformOptions,
126+
})
127+
} catch (error: unknown) {
128+
const err = error as Record<string, any>
129+
const fileName = err.fileName || asset.name
130+
const loc = err.loc as
131+
| { line: number; column?: number }
132+
| undefined
133+
134+
const messageParts: string[] = [
135+
`${err.message || error}`,
136+
]
137+
138+
if (loc && typeof loc.line === 'number') {
139+
const position = typeof loc.column === 'number'
140+
? `${loc.line}:${loc.column}`
141+
: `${loc.line}`
142+
messageParts.push(` in ${fileName}:${position}`, '')
143+
144+
const lines = sourceAsString.split('\n')
145+
const start = Math.max(0, loc.line - 4)
146+
const end = Math.min(lines.length, loc.line + 3)
147+
for (let i = start; i < end; i++) {
148+
const lineNum = i + 1
149+
const marker = lineNum === loc.line ? '>' : ' '
150+
messageParts.push(`${marker} ${lineNum} | ${lines[i]}`)
151+
}
152+
} else {
153+
messageParts.push(` in ${fileName}`)
154+
}
155+
156+
throw new Error(messageParts.join('\n'), { cause: error })
157+
}
125158
const codeString = result.code.toString()
126159

127160
compilation.updateAsset(

0 commit comments

Comments
 (0)