diff --git a/src/build/index.js b/src/build/index.js index 54537dfbb..af8a0406d 100644 --- a/src/build/index.js +++ b/src/build/index.js @@ -93,6 +93,11 @@ const tasks = new Listr([ if (ctx.bundlesize) { const gzip = await gzipSize(outfile) const maxsize = bytes(ctx.bundlesizeMax) + + if (maxsize == null) { + throw new Error(`Could not parse bytes from "${ctx.bundlesizeMax}"`) + } + const diff = gzip - maxsize task.output = 'Use https://esbuild.github.io/analyze/ to load "./dist/stats.json".' diff --git a/src/document-check.js b/src/document-check.js index 1885f8acd..ad1c1691d 100644 --- a/src/document-check.js +++ b/src/document-check.js @@ -11,8 +11,16 @@ import { formatCode, formatError, fromRoot, hasTsconfig, readJson } from './util * @typedef {import("./types").GlobalOptions} GlobalOptions * @typedef {import("./types").DocsVerifierOptions} DocsVerifierOptions * @typedef {import("listr").ListrTaskWrapper} Task + * @typedef {import("ts-node").TSError} TSError */ +/** + * A list of tsc errors to ignore when compiling code snippets in documentation. + */ +const TS_ERRORS_TO_SUPRESS = [ + 2307 // Cannot find module '...' or its corresponding type declarations +] + const tasks = new Listr( [ { @@ -54,7 +62,8 @@ const tasks = new Listr( target: 'esnext', module: 'esnext', noImplicitAny: true, - noEmit: true + noEmit: true, + skipLibCheck: true } } ]) @@ -64,6 +73,15 @@ const tasks = new Listr( results.forEach((result) => { if (result.error) { + // ignore some diagnostic codes + if (isTSError(result.error)) { + const diagnosticCodes = result.error?.diagnosticCodes?.filter(code => !TS_ERRORS_TO_SUPRESS.includes(code)) + + if (diagnosticCodes.length === 0) { + return + } + } + process.exitCode = 1 console.log(kleur.red().bold(`Error compiling example code block ${result.index} in file ${result.file}:`)) console.log(formatError(result.error)) @@ -89,3 +107,12 @@ const tasks = new Listr( ) export default tasks + +/** + * + * @param {*} err + * @returns {err is TSError} + */ +function isTSError (err) { + return Array.isArray(err.diagnosticCodes) +} diff --git a/test/fixtures/document-check/pass/GOODREADME.md b/test/fixtures/document-check/pass/GOODREADME.md index 3097c2fae..a571c111b 100644 --- a/test/fixtures/document-check/pass/GOODREADME.md +++ b/test/fixtures/document-check/pass/GOODREADME.md @@ -1,3 +1,8 @@ ```ts -export const a = 1; +import { foo } from 'dep-we-do-not-have' + +// should not cause an error because we ignore TS2307 +foo() + +export const a = 1 ``` \ No newline at end of file