Skip to content

Commit

Permalink
feat: update file-type to disable eval dynamic code errors during build
Browse files Browse the repository at this point in the history
  • Loading branch information
rxliuli committed Nov 21, 2024
1 parent b6b0e41 commit 16c015f
Show file tree
Hide file tree
Showing 7 changed files with 900 additions and 119 deletions.
21 changes: 11 additions & 10 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"scripts": {
"lint": "eslint .",
"test-off": "vitest",
"build": "tshy",
"dev": "tshy --watch",
"build": "tsup",
"dev": "tsup --watch",
"clean": "rm -rf node_modules .tshy .tshy-build dist .turbo"
},
"author": "Andrew Lisowski <[email protected]>",
Expand All @@ -20,7 +20,7 @@
"@jimp/utils": "workspace:*",
"await-to-js": "^3.0.0",
"exif-parser": "^0.1.12",
"file-type": "^16.0.0",
"file-type": "^19.6.0",
"mime": "3"
},
"devDependencies": {
Expand All @@ -32,6 +32,7 @@
"@types/node": "^18.19.48",
"eslint": "^9.9.1",
"tshy": "^3.0.2",
"tsup": "^8.3.5",
"typescript": "^5.5.4",
"vite-plugin-node-polyfills": "^0.22.0",
"vitest": "^2.0.5"
Expand All @@ -49,21 +50,21 @@
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/index.cjs",
"types": "./dist/index.d.cts",
"type": "module",
"publishConfig": {
"access": "public"
},
"sideEffects": false,
"module": "./dist/esm/index.js"
"module": "./dist/index.js"
}
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bitmap, Format, JimpClass, Edge } from "@jimp/types";
import { cssColorToHex, scan, scanIterator } from "@jimp/utils";
import fileType from "file-type/core.js";
import { fileTypeFromBuffer } from "#file-type/core";
import { to } from "await-to-js";
import { existsSync, readFile, writeFile } from "@jimp/file-ops";
import mime from "mime/lite.js";
Expand Down Expand Up @@ -334,7 +334,7 @@ export function createJimp<
const actualBuffer =
buffer instanceof ArrayBuffer ? bufferFromArrayBuffer(buffer) : buffer;

const mime = await fileType.fromBuffer(actualBuffer);
const mime = await fileTypeFromBuffer(actualBuffer);

if (!mime || !mime.mime) {
throw new Error("Could not find MIME for Buffer");
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/polyfills/cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { fileTypeFromBuffer as fileTypeFromBufferCore } from 'file-type/core'

export const fileTypeFromBuffer: typeof fileTypeFromBufferCore = async (
buffer,
) => {
const { fileTypeFromBuffer } = await import('file-type/core')
return await fileTypeFromBuffer(buffer)
}
1 change: 1 addition & 0 deletions packages/core/src/polyfills/esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { fileTypeFromBuffer } from 'file-type/core'
7 changes: 6 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"extends": "@jimp/config-typescript/base.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"module": "ESNext",
"moduleResolution": "bundler",
"paths": {
"#file-type/core": ["./src/polyfills/esm.ts"]
}
}
}
16 changes: 16 additions & 0 deletions packages/core/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
clean: true,
esbuildOptions(options) {
options.alias = {
'#file-type/core':
options.format === 'esm'
? './src/polyfills/esm.ts'
: './src/polyfills/cjs.ts',
}
},
})
Loading

0 comments on commit 16c015f

Please sign in to comment.