From 5b740373ce9e1b6e596d54cc6df19a0e06258b8b Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 15 Sep 2023 10:38:20 +0800 Subject: [PATCH] chore: remove deps of asserts --- scripts/vendor.ts | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/scripts/vendor.ts b/scripts/vendor.ts index 65ae7d5..ac8b6fe 100644 --- a/scripts/vendor.ts +++ b/scripts/vendor.ts @@ -1,6 +1,7 @@ /* eslint-disable unicorn/prefer-top-level-await */ import fs from 'node:fs' import fsp from 'node:fs/promises' +import { join } from 'node:path' import { downloadTemplate } from 'giget' // This script clones recast and patches, and then re-bundle it so we get rid of the unnecessary polyfills @@ -19,24 +20,21 @@ async function cloneRecast() { await fsp.rm('vendor/recast/tsconfig.json') // Remove the assert import and usage - await filterLines('vendor/recast/lib/patcher.ts', (line) => { - if (line.startsWith('import assert from')) { - return false - } - if (/^\s*assert\./.test(line)) { - return `false && ` + line - } - return line - }) - await filterLines('vendor/recast/lib/patcher.ts', (line) => { - if (line.startsWith('import assert from')) { - return false - } - if (/^\s*assert\./.test(line)) { - return `false && ` + line - } - return line - }) + await Promise.all(fs.readdirSync('vendor/recast/lib', { withFileTypes: true }).map(async (file) => { + if (!file.isFile()) { return } + return filterLines(join(file.path, file.name), (line) => { + if (line.startsWith('import assert from')) { + return false + } + if (/^\s*assert\./.test(line)) { + if (line.endsWith(';')) { + return false + } + return `// @ts-ignore \n false && ` + line + } + return line + }) + })) // Remove the require(), and since we are providing our own parser anyway await filterLines('vendor/recast/lib/options.ts', (line) => { @@ -46,6 +44,10 @@ async function cloneRecast() { return line }) + await filterLines('vendor/recast/lib/parser.ts', (line) => { + return line.replace('require("esprima")', `false && require("")`) + }) + await filterLines('vendor/recast/lib/util.ts', (line) => { if (line.includes('isBrowser() ? "\\n"')) { return 'return "\\n"' } return line