diff --git a/build.config.ts b/build.config.ts index 32381b21..95878560 100644 --- a/build.config.ts +++ b/build.config.ts @@ -7,7 +7,6 @@ import { defineBuildConfig, type Config } from '@flex-development/mkbuild' import pathe from '@flex-development/pathe' import { at } from '@flex-development/tutils' -import type { BuildResult, PluginBuild } from 'esbuild' import pkg from './package.json' assert { type: 'json' } import tsconfig from './tsconfig.build.json' assert { type: 'json' } @@ -31,40 +30,6 @@ const config: Config = defineBuildConfig({ keepNames: true, minifySyntax: true, platform: 'node', - plugins: [ - { - name: 'ts-ignore-peers', - - /** - * Inserts `// @ts-ignore peer dependency` above property declarations - * using types from peer dependencies. - * - * [1]: https://esbuild.github.io/plugins - * - * @see https://regex101.com/r/6r7Cke - * @see https://github.com/microsoft/TypeScript/issues/38628#issuecomment-1439749496 - * - * @param {PluginBuild} build - [esbuild plugin api][1] - * @param {PluginBuild['onEnd']} build.onEnd - Build end callback - * @return {void} Nothing when complete - */ - setup({ onEnd }: PluginBuild): void { - return void onEnd((result: BuildResult<{ write: false }>): void => { - return void (result.outputFiles = result.outputFiles.map(output => { - return output.path.endsWith('.d.mts') - ? { - ...output, - text: output.text.replace( - /\n( +)(.+?\??: )(\w+<)?(import\('node-fetch'\).+)/g, - '\n$1// @ts-ignore peer dependency\n$1$2$3$4' - ) - } - : output - })) - }) - } - } - ], target: [ 'node' + at(/([\d.]+)/.exec(pkg.engines.node), 0, ''), tsconfig.compilerOptions.target diff --git a/docs/.vitepress/theme/comments/link-replacements.json b/docs/.vitepress/theme/comments/link-replacements.json index d01f2314..a1106fc6 100644 --- a/docs/.vitepress/theme/comments/link-replacements.json +++ b/docs/.vitepress/theme/comments/link-replacements.json @@ -32,7 +32,7 @@ "{@linkcode ParsedSubpath}": "/api/interfaces/#parsedsubpath", "{@linkcode Protocol}": "/api/types#protocol", "{@linkcode RESOLVE_EXTENSIONS}": "/api/#resolve-extensions", - "{@linkcode RequestInit}": "https://github.com/node-fetch/node-fetch/blob/v3.3.0/@types/index.d.ts#L71-L112", + "{@linkcode RequestInit}": "https://developer.mozilla.org/docs/Web/API/fetch#options", "{@linkcode RequireStatement}": "/api/interfaces#requirestatement", "{@linkcode ResolveAliasOptions}": "/api/interfaces#resolvealiasoptions", "{@linkcode ResolveModuleOptions}": "/api/interfaces#resolvemoduleoptions", diff --git a/package.json b/package.json index f5968c78..e2bc314b 100644 --- a/package.json +++ b/package.json @@ -145,7 +145,6 @@ "is-ci": "3.0.1", "jsonc-eslint-parser": "2.4.0", "lint-staged": "15.1.0", - "node-fetch": "3.3.2", "node-notifier": "10.0.1", "prettier": "3.1.0", "sh-syntax": "0.4.1", @@ -168,8 +167,7 @@ "yaml-eslint-parser": "1.2.2" }, "peerDependencies": { - "@types/node": ">=18.11.9", - "node-fetch": ">=3.3.0" + "@types/node": ">=18.11.9" }, "peerDependenciesMeta": { "@types/node": { diff --git a/src/interfaces/__tests__/options-get-format.spec-d.ts b/src/interfaces/__tests__/options-get-format.spec-d.ts index 76a35c5a..32252b51 100644 --- a/src/interfaces/__tests__/options-get-format.spec-d.ts +++ b/src/interfaces/__tests__/options-get-format.spec-d.ts @@ -10,7 +10,6 @@ import type { Optional, RequiredKeys } from '@flex-development/tutils' -import type { RequestInit } from 'node-fetch' import type TestSubject from '../options-get-format' describe('unit-d:interfaces/GetFormatOptions', () => { diff --git a/src/interfaces/__tests__/options-get-source.spec-d.ts b/src/interfaces/__tests__/options-get-source.spec-d.ts index c8cc02bb..9dcc8868 100644 --- a/src/interfaces/__tests__/options-get-source.spec-d.ts +++ b/src/interfaces/__tests__/options-get-source.spec-d.ts @@ -9,7 +9,6 @@ import type { OrLowercase, RequiredKeys } from '@flex-development/tutils' -import type { RequestInit } from 'node-fetch' import type TestSubject from '../options-get-source' describe('unit-d:interfaces/GetSourceOptions', () => { diff --git a/src/interfaces/options-get-format.ts b/src/interfaces/options-get-format.ts index bc2cd4b6..e2f9c8cf 100644 --- a/src/interfaces/options-get-format.ts +++ b/src/interfaces/options-get-format.ts @@ -27,10 +27,6 @@ interface GetFormatOptions { /** * Enable support for network based modules. * - * **Note**: Requires [`node-fetch`][1]. - * - * [1]: https://github.com/node-fetch/node-fetch - * * @see https://nodejs.org/docs/latest-v16.x/api/esm.html#https-and-http-imports * * @default false @@ -49,6 +45,9 @@ interface GetFormatOptions { /** * Map file extensions to module formats. * + * @see {@linkcode Ext} + * @see {@linkcode Format} + * * @default EXTENSION_FORMAT_MAP */ extension_format_map?: Optional> @@ -74,7 +73,7 @@ interface GetFormatOptions { * * @default {} */ - req?: Optional + req?: Optional } export type { GetFormatOptions as default } diff --git a/src/interfaces/options-get-source.ts b/src/interfaces/options-get-source.ts index 7d24ed7c..03f90faf 100644 --- a/src/interfaces/options-get-source.ts +++ b/src/interfaces/options-get-source.ts @@ -16,10 +16,6 @@ interface GetSourceOptions { /** * Enable support for network based modules. * - * **Note**: Requires [`node-fetch`][1]. - * - * [1]: https://github.com/node-fetch/node-fetch - * * @see https://nodejs.org/docs/latest-v16.x/api/esm.html#https-and-http-imports * * @default false @@ -53,7 +49,7 @@ interface GetSourceOptions { * * @default {} */ - req?: Optional + req?: Optional } export type { GetSourceOptions as default } diff --git a/src/utils/get-format.ts b/src/utils/get-format.ts index 39876992..8a799687 100644 --- a/src/utils/get-format.ts +++ b/src/utils/get-format.ts @@ -17,7 +17,6 @@ import { isBuiltin } from '@flex-development/is-builtin' import pathe, { type Ext } from '@flex-development/pathe' import { isEmptyString, - isUndefined, type EmptyString, type Nilable, type Nullable, @@ -68,14 +67,7 @@ const getFormat = async ( validateBoolean(wasm_modules, 'options.experimental_wasm_modules') validateBoolean(ignore_errors, 'options.ignore_errors') validateMap(extension_format_map, 'options.extension_format_map') - !isUndefined(req) && validateObject(req, 'options.req') - - /** - * Module {@linkcode id} as {@linkcode URL}. - * - * @const {URL} url - */ - const url: URL = toURL(id) + validateObject(req, 'options.req') /** * Extracts a [MIME type][1] from a {@linkcode URL} href, {@linkcode URL} @@ -85,12 +77,12 @@ const getFormat = async ( * [2]: https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Type * * @param {Nullable} value - `URL` component or `content-type` header - * @param {boolean} [content_type=false] - `value` is `content-type` header? + * @param {boolean} [header=false] - `value` is `content-type` header? * @return {Nilable} Module format, `null`, or `undefined` */ const mimeToFormat = ( value: Nullable, - content_type: boolean = false + header: boolean = false ): Nilable => { /** * Module format for {@linkcode value}. @@ -99,39 +91,45 @@ const getFormat = async ( */ let format: Nilable = null - // do nothing if value is empty - /* c8 ignore next */ if (!value) return format - - // sanitize content-type header - if (content_type) value = value.replace(/;.*/, ',') - - // add data: protocol to value - if (!value.startsWith('data:')) value = `data:${value}` - - // get format based on mime type - try { - switch (parseDataURL(value).mime) { - case 'application/javascript': - case 'application/typescript': - case 'text/javascript': - format = Format.MODULE - break - case 'application/json': - format = json_modules ? Format.JSON : null - break - case 'application/wasm': - format = wasm_modules ? Format.WASM : null - break - default: - break + if (value) { + // sanitize content-type header + if (header) value = value.replace(/;.*/, ',') + + // add data: protocol to value + if (!value.startsWith('data:')) value = `data:${value}` + + // get format based on mime type + try { + switch (parseDataURL(value).mime) { + case 'application/javascript': + case 'application/typescript': + case 'text/javascript': + format = Format.MODULE + break + case 'application/json': + format = json_modules ? Format.JSON : null + break + case 'application/wasm': + format = wasm_modules ? Format.WASM : null + break + default: + break + } + } catch { + format = ignore_errors ? undefined : null } - } catch { - format = ignore_errors ? undefined : null } return format } + /** + * Module {@linkcode id} as {@linkcode URL}. + * + * @const {URL} url + */ + const url: URL = toURL(id) + /** * Module format for {@linkcode id}. * @@ -198,8 +196,6 @@ const getFormat = async ( // esm-only and module id does not include file extension if (scope && scope.pkgjson.type === Format.MODULE) { if (isEmptyString(ext)) { - const { pkg } = scope - /** * Basename of {@linkcode url.pathname} * @@ -208,16 +204,16 @@ const getFormat = async ( const basename: string = pathe.basename(url.pathname) /** - * Relative path from {@linkcode pkg} to {@linkcode url.pathname}. + * Relative path from `package.json` file to module id. * * @const {string} relative */ const relative: string = pathe - .relative(pkg, url.pathname) + .relative(scope.pkg, url.pathname) .slice(1) suggestion = - `Loading extensionless files is not supported inside of "type":"module" package.json contexts. The package.json file ${pkg} caused this "type":"module" context. Try changing ${url.pathname} to have a file extension. Note the "bin" field of package.json can point to a file with an extension, for example {"type":"module","bin":{"${basename}":"${relative}.js"}}` + `Loading extensionless files is not supported inside of "type":"module" package.json contexts. The package.json file ${scope.pkg} caused this "type":"module" context. Try changing ${url.pathname} to have a file extension. Note the "bin" field of package.json can point to a file with an extension, for example {"type":"module","bin":{"${basename}":"${relative}.js"}}` } } @@ -228,8 +224,7 @@ const getFormat = async ( case 'http:': case 'https:': if (network_imports) { - const { default: fetch } = await import('node-fetch') - const { headers } = await fetch(url.href, req) + const { headers } = await fetch(url, req) format = mimeToFormat(headers.get('content-type'), true) } else { format = ignore_errors ? undefined : null diff --git a/src/utils/get-source.ts b/src/utils/get-source.ts index e6ca3765..ba7970a2 100644 --- a/src/utils/get-source.ts +++ b/src/utils/get-source.ts @@ -56,7 +56,7 @@ const getSource = async ( validateBoolean(network_imports, 'options.experimental_network_imports') !isUndefined(format) && validateString(format, 'options.format') validateBoolean(ignore_errors, 'options.ignore_errors') - !isUndefined(req) && validateObject(req, 'options.req') + validateObject(req, 'options.req') // exit early if format is Format.BUILTIN if (format === Format.BUILTIN) return undefined @@ -105,8 +105,7 @@ const getSource = async ( case 'http:': case 'https:': if (network_imports) { - const { default: fetch } = await import('node-fetch') - source = await (await fetch(url.href, req)).text() + source = await (await fetch(url, req)).text() } else { err = true } diff --git a/yarn.lock b/yarn.lock index 5f7d5108..2481431b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1826,7 +1826,6 @@ __metadata: is-ci: "npm:3.0.1" jsonc-eslint-parser: "npm:2.4.0" lint-staged: "npm:15.1.0" - node-fetch: "npm:3.3.2" node-notifier: "npm:10.0.1" prettier: "npm:3.1.0" sh-syntax: "npm:0.4.1" @@ -1849,7 +1848,6 @@ __metadata: yaml-eslint-parser: "npm:1.2.2" peerDependencies: "@types/node": ">=18.11.9" - node-fetch: ">=3.3.0" peerDependenciesMeta: "@types/node": optional: true @@ -4758,13 +4756,6 @@ __metadata: languageName: node linkType: hard -"data-uri-to-buffer@npm:^4.0.0": - version: 4.0.1 - resolution: "data-uri-to-buffer@npm:4.0.1" - checksum: 0d0790b67ffec5302f204c2ccca4494f70b4e2d940fea3d36b09f0bb2b8539c2e86690429eb1f1dc4bcc9e4df0644193073e63d9ee48ac9fce79ec1506e4aa4c - languageName: node - linkType: hard - "de-indent@npm:^1.0.2": version: 1.0.2 resolution: "de-indent@npm:1.0.2" @@ -6284,16 +6275,6 @@ __metadata: languageName: node linkType: hard -"fetch-blob@npm:^3.1.2, fetch-blob@npm:^3.1.4": - version: 3.2.0 - resolution: "fetch-blob@npm:3.2.0" - dependencies: - node-domexception: "npm:^1.0.0" - web-streams-polyfill: "npm:^3.0.3" - checksum: 5264ecceb5fdc19eb51d1d0359921f12730941e333019e673e71eb73921146dceabcb0b8f534582be4497312d656508a439ad0f5edeec2b29ab2e10c72a1f86b - languageName: node - linkType: hard - "fflate@npm:^0.7.4": version: 0.7.4 resolution: "fflate@npm:0.7.4" @@ -6401,15 +6382,6 @@ __metadata: languageName: node linkType: hard -"formdata-polyfill@npm:^4.0.10": - version: 4.0.10 - resolution: "formdata-polyfill@npm:4.0.10" - dependencies: - fetch-blob: "npm:^3.1.2" - checksum: 9b5001d2edef3c9449ac3f48bd4f8cc92e7d0f2e7c1a5c8ba555ad4e77535cc5cf621fabe49e97f304067037282dd9093b9160a3cb533e46420b446c4e6bc06f - languageName: node - linkType: hard - "fs-extra@npm:11.1.0, fs-extra@npm:^11.0.0": version: 11.1.0 resolution: "fs-extra@npm:11.1.0" @@ -8751,13 +8723,6 @@ __metadata: languageName: node linkType: hard -"node-domexception@npm:^1.0.0": - version: 1.0.0 - resolution: "node-domexception@npm:1.0.0" - checksum: e332522f242348c511640c25a6fc7da4f30e09e580c70c6b13cb0be83c78c3e71c8d4665af2527e869fc96848924a4316ae7ec9014c091e2156f41739d4fa233 - languageName: node - linkType: hard - "node-emoji@npm:2.1.0": version: 2.1.0 resolution: "node-emoji@npm:2.1.0" @@ -8810,17 +8775,6 @@ __metadata: languageName: node linkType: hard -"node-fetch@npm:3.3.2": - version: 3.3.2 - resolution: "node-fetch@npm:3.3.2" - dependencies: - data-uri-to-buffer: "npm:^4.0.0" - fetch-blob: "npm:^3.1.4" - formdata-polyfill: "npm:^4.0.10" - checksum: 24207ca8c81231c7c59151840e3fded461d67a31cf3e3b3968e12201a42f89ce4a0b5fb7079b1fa0a4655957b1ca9257553200f03a9f668b45ebad265ca5593d - languageName: node - linkType: hard - "node-fetch@npm:^2.6.7": version: 2.7.0 resolution: "node-fetch@npm:2.7.0" @@ -11685,13 +11639,6 @@ __metadata: languageName: node linkType: hard -"web-streams-polyfill@npm:^3.0.3": - version: 3.2.1 - resolution: "web-streams-polyfill@npm:3.2.1" - checksum: 08fcf97b7883c1511dd3da794f50e9bde75a660884783baaddb2163643c21a94086f394dc4bd20dff0f55c98d98d60c4bea05a5809ef5005bdf835b63ada8900 - languageName: node - linkType: hard - "web-vitals@npm:0.2.4": version: 0.2.4 resolution: "web-vitals@npm:0.2.4"