From 6819dc91f6455f255d3d9c0db5eb8934b2165ae5 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 6 Feb 2022 23:25:29 +0900 Subject: [PATCH] Add an esbuild plugin to add `@ts-nocheck` annotation for vendors (#12248) * Enable prettier * Add `esbuildPluginTsNocheck` * Run build * Update cspell.json --- .prettierignore | 2 +- cspell.json | 1 + scripts/vendors/bundle-vendors.mjs | 6 ++++-- scripts/vendors/esbuild-plugin-ts-nocheck.mjs | 20 +++++++++++++++++++ vendors/mem.js | 1 + vendors/string-width.js | 1 + 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 scripts/vendors/esbuild-plugin-ts-nocheck.mjs diff --git a/.prettierignore b/.prettierignore index 81812aeda..305b9916e 100644 --- a/.prettierignore +++ b/.prettierignore @@ -10,4 +10,4 @@ coverage/ /website/static/lib/ /website/static/playground.js .nyc_output -vendors/ +/vendors/ diff --git a/cspell.json b/cspell.json index d578a035c..6b9a2c064 100644 --- a/cspell.json +++ b/cspell.json @@ -213,6 +213,7 @@ "neoclide", "neoformat", "neovim", + "nocheck", "nnoremap", "nonenumerable", "Nonspacing", diff --git a/scripts/vendors/bundle-vendors.mjs b/scripts/vendors/bundle-vendors.mjs index 0657255de..1036803d2 100644 --- a/scripts/vendors/bundle-vendors.mjs +++ b/scripts/vendors/bundle-vendors.mjs @@ -9,6 +9,7 @@ import { writePackage } from "write-pkg"; import { readPackageUp } from "read-pkg-up"; import vendors from "./vendors.mjs"; import { writeVendorVersions } from "./vendor-versions.mjs"; +import esbuildPluginTsNocheck from "./esbuild-plugin-ts-nocheck.mjs"; const { __dirname, require } = createEsmUtils(import.meta); const rootDir = path.join(__dirname, "..", ".."); @@ -32,7 +33,7 @@ async function lockVersions(vendors) { async function fileExists(filePath) { try { - return (await fs.stat(filePath)).isFile() + return (await fs.stat(filePath)).isFile(); } catch { return false; } @@ -41,7 +42,7 @@ async function fileExists(filePath) { async function cleanExistsBundledJS() { for (const file of await fs.readdir(vendorsDir)) { const filePath = path.join(vendorsDir, file); - if (path.extname(file) === ".js" && await (fileExists(filePath))) { + if (path.extname(file) === ".js" && (await fileExists(filePath))) { await fs.rm(filePath); } } @@ -58,6 +59,7 @@ async function bundle(vendor) { bundle: true, target: ["node12.17.0"], platform: "node", + plugins: [esbuildPluginTsNocheck()], outfile, }; await esbuild.build(esbuildOption); diff --git a/scripts/vendors/esbuild-plugin-ts-nocheck.mjs b/scripts/vendors/esbuild-plugin-ts-nocheck.mjs new file mode 100644 index 000000000..44c0452f2 --- /dev/null +++ b/scripts/vendors/esbuild-plugin-ts-nocheck.mjs @@ -0,0 +1,20 @@ +import fs from "node:fs"; + +const tsNoCheck = "// @ts-nocheck\n"; + +export default function esbuildPluginTsNocheck() { + return { + name: "ts-no-check", + setup(build) { + const options = build.initialOptions; + const { outfile } = options; + build.onEnd(() => { + if (!fs.existsSync(outfile)) { + throw new Error(`${outfile} not exists`); + } + const text = fs.readFileSync(outfile, "utf8"); + fs.writeFileSync(outfile, tsNoCheck + text); + }); + }, + }; +} diff --git a/vendors/mem.js b/vendors/mem.js index 5ffd6ad0d..dae72c450 100644 --- a/vendors/mem.js +++ b/vendors/mem.js @@ -1,3 +1,4 @@ +// @ts-nocheck var __create = Object.create; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; diff --git a/vendors/string-width.js b/vendors/string-width.js index 82918aae1..54b9b0cbf 100644 --- a/vendors/string-width.js +++ b/vendors/string-width.js @@ -1,3 +1,4 @@ +// @ts-nocheck var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor;