Skip to content

Commit ff295ca

Browse files
fix: propagate tsc errors (#82)
1 parent 320f20f commit ff295ca

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/resolver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export function createDtsResolvePlugin({
8484
const isRelativeOrAbsolute =
8585
id.startsWith('.') || path.isAbsolute(id)
8686
if (isRelativeOrAbsolute) {
87-
return this.error(`Cannot resolve import '${id}' from '${importer}'`)
87+
return this.error(
88+
`Cannot resolve import '${id}' from '${importer}'`,
89+
)
8890
}
8991
return external
9092
}

src/tsc/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,19 @@ export function tscEmit(tscOptions: TscOptions): TscResult {
336336
// @ts-expect-error private API: forceDtsEmit
337337
true,
338338
)
339-
if (emitSkipped && diagnostics.length) {
340-
return { error: ts.formatDiagnostics(diagnostics, formatHost) }
339+
const emitErrors = diagnostics.filter(
340+
(d: ts.Diagnostic) => d.category === ts.DiagnosticCategory.Error,
341+
)
342+
if (emitErrors.length > 0) {
343+
return { error: ts.formatDiagnostics(emitErrors, formatHost) }
344+
}
345+
if (emitSkipped) {
346+
const errors = ts
347+
.getPreEmitDiagnostics(program)
348+
.filter((d: ts.Diagnostic) => d.category === ts.DiagnosticCategory.Error)
349+
if (errors.length > 0) {
350+
return { error: ts.formatDiagnostics(errors, formatHost) }
351+
}
341352
}
342353

343354
// If TypeScript skipped emitting because the file is already a .d.ts (e.g. a

tests/fixtures/type-error.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const n: number = 'string'

tests/tsc.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ describe('tsc', () => {
1717
dts({
1818
emitDtsOnly: true,
1919
compilerOptions: {
20+
module: 'preserve',
21+
moduleResolution: 'bundler',
2022
skipLibCheck: true,
2123
isolatedDeclarations: false,
2224
},
@@ -33,7 +35,11 @@ describe('tsc', () => {
3335
[
3436
dts({
3537
emitDtsOnly: true,
36-
compilerOptions: { isolatedDeclarations: false },
38+
compilerOptions: {
39+
module: 'preserve',
40+
moduleResolution: 'bundler',
41+
isolatedDeclarations: false,
42+
},
3743
}),
3844
],
3945
)
@@ -132,6 +138,9 @@ describe('tsc', () => {
132138
dts({
133139
emitDtsOnly: true,
134140
vue: true,
141+
compilerOptions: {
142+
isolatedDeclarations: false,
143+
},
135144
}),
136145
])
137146
expect(snapshot).toMatchSnapshot()
@@ -167,4 +176,14 @@ describe('tsc', () => {
167176
])
168177
expect(snapshot).toMatchSnapshot()
169178
})
179+
180+
test('fail on type errors', async () => {
181+
await expect(() =>
182+
rolldownBuild(path.resolve(dirname, 'fixtures/type-error.ts'), [
183+
dts({
184+
oxc: false,
185+
}),
186+
]),
187+
).rejects.toThrow('error TS2322')
188+
})
170189
})

0 commit comments

Comments
 (0)