From f03d245ef1a4d13306b543aeebcdc3141f575e15 Mon Sep 17 00:00:00 2001 From: sugoidogo Date: Fri, 3 Apr 2026 23:24:39 -0700 Subject: [PATCH] target node 20 (oldest supported release) --- .gitignore | 3 ++- main.js | 26 ++++++++++++++++++++++++++ main.ts | 32 -------------------------------- package.json | 4 ++-- src/main.ts | 42 ------------------------------------------ tsconfig.json | 9 --------- 6 files changed, 30 insertions(+), 86 deletions(-) create mode 100644 main.js delete mode 100644 main.ts delete mode 100644 src/main.ts delete mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 048f623..0465952 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules index.d.ts -*.tgz \ No newline at end of file +*.tgz +*lock* \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..6267eb5 --- /dev/null +++ b/main.js @@ -0,0 +1,26 @@ +// @ts-check +import * as fs from "node:fs"; +const globalRegex = / (?:var|function) (\w+)/g; +const globalSymbols = []; +let buffer = ''; +for (const basename of fs.readdirSync("node_modules/@types/node/web-globals")) { + buffer += fs.readFileSync("node_modules/@types/node/web-globals/" + basename, "utf8"); +} +for (const match of buffer.matchAll(globalRegex)) { + globalSymbols.push(match[0].trim()); +} +const declarationRegex = /(?:^\/\*\*[\s\S]+?\*\/\n)?^(interface|type|declare \w+) (\w+).+\n(?:(?: .+\n)*};?\n)?/gm; +const importableTypes = new Set(); +buffer = ''; +for (const match of fs.readFileSync("node_modules/@types/web/index.d.ts", "utf8").matchAll(declarationRegex)) { + if (!match[1].startsWith("declare")) { + importableTypes.add(match[2]); + continue; + } + if (globalSymbols.includes(match[1].replace("declare ", "") + " " + match[2])) { + buffer += match[0].replace(/^declare /gm, "").replaceAll("\n", "\n\t"); + } +} +buffer = `import type { \n\t${Array.from(importableTypes).join(",\n\t")}\n} from "@sugoidogo/importable-types-web"\ndeclare global {\n\t${buffer.trimEnd()}\n}`; +fs.writeFileSync("index.d.ts", buffer, "utf8"); +fs.copyFileSync("node_modules/@types/web/LICENSE.txt", "LICENSE.txt"); diff --git a/main.ts b/main.ts deleted file mode 100644 index b64c7c7..0000000 --- a/main.ts +++ /dev/null @@ -1,32 +0,0 @@ -import * as fs from "node:fs" - -const globalRegex = / (?:var|function) (\w+)/g -const globalSymbols: string[] = [] -let buffer = '' - -for (const basename of fs.readdirSync("node_modules/@types/node/web-globals")) { - buffer += fs.readFileSync("node_modules/@types/node/web-globals/" + basename, "utf8") -} - -for (const match of buffer.matchAll(globalRegex)) { - globalSymbols.push(match[0].trim()) -} - -const declarationRegex = /(?:^\/\*\*[\s\S]+?\*\/\n)?^(interface|type|declare \w+) (\w+).+\n(?:(?: .+\n)*};?\n)?/gm -const importableTypes=new Set() -buffer = '' - -for (const match of fs.readFileSync("node_modules/@types/web/index.d.ts", "utf8").matchAll(declarationRegex)) { - if (!match[1].startsWith("declare")) { - importableTypes.add(match[2]) - continue - } - if (globalSymbols.includes(match[1].replace("declare ","")+" "+match[2])) { - buffer+=match[0].replace(/^declare /gm,"").replaceAll("\n","\n\t") - } -} - -buffer=`import type { \n\t${Array.from(importableTypes).join(",\n\t")}\n} from "@sugoidogo/importable-types-web"\ndeclare global {\n\t${buffer.trimEnd()}\n}` - -fs.writeFileSync("index.d.ts", buffer, "utf8") -fs.copyFileSync("node_modules/@types/web/LICENSE.txt","LICENSE.txt") \ No newline at end of file diff --git a/package.json b/package.json index dd137c2..4ea85ed 100644 --- a/package.json +++ b/package.json @@ -14,14 +14,14 @@ "index.d.ts" ], "scripts": { - "prepack": "node main.ts" + "prepack": "node main.js" }, "dependencies": { "undici-types": "^7.21.0" }, "devDependencies": { "@sugoidogo/importable-types-web": "^1.0.0", - "@types/node": "^25.2.2", + "@types/node": "^20.19.39", "@types/web": "^0.0.329" } } diff --git a/src/main.ts b/src/main.ts deleted file mode 100644 index 7fd8a38..0000000 --- a/src/main.ts +++ /dev/null @@ -1,42 +0,0 @@ -import * as fs from 'node:fs' -import * as path from 'node:path' - -const sourceDir = 'node_modules/@types/node/' -const importRegex = /import .+? from "(?:node\:)?(.+?)"/g - -if (fs.existsSync('dist')) fs.rmSync('dist', { 'recursive': true }) - -let imports=new Set() -for (const basename of fs.readdirSync(sourceDir + 'web-globals/')) { - imports.add('web-globals/'+basename.split('.')[0]) -} - -let startSize=0 -do { - startSize = imports.size - let buffer = '' - for (const importName of imports) { - if (!fs.existsSync(sourceDir + importName + '.d.ts')) { - console.warn(importName + ' is not in sources, ignoring') - continue - } - buffer+=fs.readFileSync(sourceDir+importName+'.d.ts') - } - for (const match of buffer.matchAll(importRegex)) { - imports.add(match[1]) - } -} while (startSize != imports.size) - -let index = '' -for (const importName of imports) { - if (!fs.existsSync(sourceDir + importName + '.d.ts')) { - console.warn(importName + ' is not in sources, ignoring') - continue - } - fs.mkdirSync(path.dirname('dist/'+importName+'.d.ts'),{'recursive':true}) - fs.copyFileSync(sourceDir + importName + '.d.ts', 'dist/' + importName + '.d.ts') - index += `/// \n` -} - -fs.writeFileSync('dist/index.d.ts',index) -fs.cpSync('node_modules/@types/node/LICENSE', 'LICENSE') \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index 15e152a..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "compilerOptions": { - "target": "es2022", - "module": "es2022", - "moduleResolution": "node", - "lib": ["ES2022"], - "types": ["node"] - } -} \ No newline at end of file