diff --git a/.gitignore b/.gitignore index c58c7db..c4f6a33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules dist -*.tgz \ No newline at end of file +*.tgz +deno.lock +package-lock.json \ No newline at end of file diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 62123e4..0000000 --- a/package-lock.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "name": "node-web-globals", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "dependencies": { - "@types/node": "^25.0.9" - } - }, - "node_modules/@types/node": { - "version": "25.0.9", - "resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.9.tgz", - "integrity": "sha512-/rpCXHlCWeqClNBwUhDcusJxXYDjZTyE8v5oTO7WbL8eij2nKhUeU89/6xgjU7N4/Vh3He0BtyhJdQbDyhiXAw==", - "license": "MIT", - "dependencies": { - "undici-types": "~7.16.0" - } - }, - "node_modules/undici-types": { - "version": "7.16.0", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz", - "integrity": "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==", - "license": "MIT" - } - } -} diff --git a/package.json b/package.json index e132b8b..5786f7e 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,9 @@ "prepack": "node src/main.ts" }, "dependencies": { + "undici-types": "^7.21.0" + }, + "devDependencies": { "@types/node": "^25.0.9" } } diff --git a/src/main.ts b/src/main.ts index 4b7bf08..7fd8a38 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,42 @@ import * as fs from 'node:fs' +import * as path from 'node:path' -fs.cpSync('node_modules/@types/node/web-globals','dist',{recursive:true}) -fs.cpSync('node_modules/@types/node/LICENSE', 'LICENSE') -const node_types_index = fs.readFileSync('node_modules/@types/node/index.d.ts').toString() -let globals_types_index = '' -for (const line of node_types_index.split('\n')) { - if (line.includes('path="web-globals/')) { - globals_types_index+=line.replace('path="web-globals/','path="')+'\n' - } +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]) } -fs.writeFileSync('dist/index.d.ts',globals_types_index) \ No newline at end of file + +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 index 77c3bcc..221645b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,9 @@ { "compilerOptions": { + "target": "esnext", "module": "esnext", - "moduleResolution": "node" + "moduleResolution": "node", + "lib": ["ESNext"], + "types": ["node"] } } \ No newline at end of file