From e479a8b5a629027ddcd17f52b975c3677af4c69f Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Thu, 14 Apr 2022 23:14:06 +0800 Subject: [PATCH] feat: Support ES module. --- package.json | 13 ++++++++++--- src/index.ts | 14 ++------------ tsconfig.json | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 2b5569e..b7d7819 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "homepage": "https://jaywcjlove.github.io/image2uri/", "license": "MIT", "author": "Kenny ", - "main": "lib/index.js", + "type": "module", + "exports": "./lib/index.js", + "main": "./lib/index.js", "scripts": { "prepare": "npm run build", "watch": "tsbb watch --disable-babel", @@ -21,7 +23,11 @@ "lib", "src" ], - "jest": {}, + "jest": { + "transformIgnorePatterns": [ + "/node_modules/?!(.*)" + ] + }, "keywords": [ "image", "uri", @@ -31,9 +37,10 @@ "image2uri" ], "dependencies": { - "node-fetch": "~2.6.1" + "node-fetch": "^3.2.3" }, "devDependencies": { + "@types/node": "~17.0.24", "tsbb": "^3.7.2" } } diff --git a/src/index.ts b/src/index.ts index 0a16cb9..78e5820 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,19 +16,9 @@ export const extTypeMap = { '.svg': 'image/svg+xml' } -export type ExtType = '.png' - | '.apng' - | '.gif' - | '.jpg' - | '.jpeg' - | '.bm' - | '.bmp' - | '.webp' - | '.ico' - | '.svg'; - +export type ExtType = keyof typeof extTypeMap; export default function image2uri(file: string, options: { ext?: string } = {}): string | Promise { - const ext = options.ext || path.extname(file) as ExtType; + const ext: ExtType = (options.ext || path.extname(file)) as ExtType; const contentType = extTypeMap[ext] if (validUrl(file)) { return fetch(file).then((response) => response.buffer()).then((buffer) => { diff --git a/tsconfig.json b/tsconfig.json index ba7a1f6..4cb1c59 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,19 @@ { "compilerOptions": { - "module": "CommonJS", - "esModuleInterop": true, - "declaration": true, + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "node", "sourceMap": true, + "declaration": true, + "allowSyntheticDefaultImports": true, + "noFallthroughCasesInSwitch": true, + "noImplicitThis": true, + "strictBindCallApply": true, + "strictNullChecks": true, + "strictPropertyInitialization": true, + "stripInternal": true, + "esModuleInterop": false, + "noImplicitAny": true, "outDir": "lib", "baseUrl": "." },