From c8dfb98f53eaf0e3bdb58eb7ccd81b423eddc026 Mon Sep 17 00:00:00 2001 From: Tmk Date: Fri, 16 Feb 2024 19:35:33 +0800 Subject: [PATCH] refactor: replace bundle-require with jiti --- packages/modernjs-plugin/package.json | 3 -- .../modernjs-plugin/src/plugins/manifest.ts | 4 +-- packages/modernjs-plugin/src/utils/require.ts | 7 +++-- pnpm-lock.yaml | 30 ++++++++++++++++--- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/packages/modernjs-plugin/package.json b/packages/modernjs-plugin/package.json index 1bba71c5..5ecc3807 100644 --- a/packages/modernjs-plugin/package.json +++ b/packages/modernjs-plugin/package.json @@ -29,9 +29,6 @@ "prepack": "pnpm build", "lint:type": "tsc --noEmit" }, - "dependencies": { - "bundle-require": "^4.0.2" - }, "devDependencies": { "@modern-js/app-tools": "^2.46.1", "@modern-js/utils": "^2.46.1", diff --git a/packages/modernjs-plugin/src/plugins/manifest.ts b/packages/modernjs-plugin/src/plugins/manifest.ts index deaf0796..1c5c03be 100644 --- a/packages/modernjs-plugin/src/plugins/manifest.ts +++ b/packages/modernjs-plugin/src/plugins/manifest.ts @@ -23,9 +23,7 @@ export const manifestPlugin = (options: ManifestOptions): RsbuildPlugin => { const outputPath = path.join(distPath, 'manifest.json'); async function generateManifest() { - const { - mod: { default: manifest }, - } = await evalFile<{ default: unknown }>(sourcePath); + const manifest = await evalFile(sourcePath); const content = isDev() ? JSON.stringify(manifest, null, 2) : JSON.stringify(manifest); await fs.writeFile(outputPath, content); } diff --git a/packages/modernjs-plugin/src/utils/require.ts b/packages/modernjs-plugin/src/utils/require.ts index 1746756b..447c8c64 100644 --- a/packages/modernjs-plugin/src/utils/require.ts +++ b/packages/modernjs-plugin/src/utils/require.ts @@ -1,5 +1,6 @@ -import { bundleRequire } from 'bundle-require'; +// @ts-ignore ts source check +import createJITI from '@rsbuild/shared/jiti'; -export async function evalFile(filepath: string) { - return await bundleRequire({ filepath }); +export async function evalFile(filepath: string): Promise { + return await createJITI(__filename, { requireCache: false, interopDefault: true })(filepath); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b0a82153..4b1e4941 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -357,10 +357,6 @@ importers: version: 5.3.3 packages/modernjs-plugin: - dependencies: - bundle-require: - specifier: ^4.0.2 - version: 4.0.2(esbuild@0.19.12) devDependencies: '@modern-js/app-tools': specifier: ^2.46.1 @@ -2097,6 +2093,7 @@ packages: cpu: [ppc64] os: [aix] requiresBuild: true + dev: true optional: true /@esbuild/android-arm64@0.17.19: @@ -2114,6 +2111,7 @@ packages: cpu: [arm64] os: [android] requiresBuild: true + dev: true optional: true /@esbuild/android-arm@0.17.19: @@ -2131,6 +2129,7 @@ packages: cpu: [arm] os: [android] requiresBuild: true + dev: true optional: true /@esbuild/android-x64@0.17.19: @@ -2148,6 +2147,7 @@ packages: cpu: [x64] os: [android] requiresBuild: true + dev: true optional: true /@esbuild/darwin-arm64@0.17.19: @@ -2165,6 +2165,7 @@ packages: cpu: [arm64] os: [darwin] requiresBuild: true + dev: true optional: true /@esbuild/darwin-x64@0.17.19: @@ -2182,6 +2183,7 @@ packages: cpu: [x64] os: [darwin] requiresBuild: true + dev: true optional: true /@esbuild/freebsd-arm64@0.17.19: @@ -2199,6 +2201,7 @@ packages: cpu: [arm64] os: [freebsd] requiresBuild: true + dev: true optional: true /@esbuild/freebsd-x64@0.17.19: @@ -2216,6 +2219,7 @@ packages: cpu: [x64] os: [freebsd] requiresBuild: true + dev: true optional: true /@esbuild/linux-arm64@0.17.19: @@ -2233,6 +2237,7 @@ packages: cpu: [arm64] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/linux-arm@0.17.19: @@ -2250,6 +2255,7 @@ packages: cpu: [arm] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/linux-ia32@0.17.19: @@ -2267,6 +2273,7 @@ packages: cpu: [ia32] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/linux-loong64@0.17.19: @@ -2284,6 +2291,7 @@ packages: cpu: [loong64] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/linux-mips64el@0.17.19: @@ -2301,6 +2309,7 @@ packages: cpu: [mips64el] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/linux-ppc64@0.17.19: @@ -2318,6 +2327,7 @@ packages: cpu: [ppc64] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/linux-riscv64@0.17.19: @@ -2335,6 +2345,7 @@ packages: cpu: [riscv64] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/linux-s390x@0.17.19: @@ -2352,6 +2363,7 @@ packages: cpu: [s390x] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/linux-x64@0.17.19: @@ -2369,6 +2381,7 @@ packages: cpu: [x64] os: [linux] requiresBuild: true + dev: true optional: true /@esbuild/netbsd-x64@0.17.19: @@ -2386,6 +2399,7 @@ packages: cpu: [x64] os: [netbsd] requiresBuild: true + dev: true optional: true /@esbuild/openbsd-x64@0.17.19: @@ -2403,6 +2417,7 @@ packages: cpu: [x64] os: [openbsd] requiresBuild: true + dev: true optional: true /@esbuild/sunos-x64@0.17.19: @@ -2420,6 +2435,7 @@ packages: cpu: [x64] os: [sunos] requiresBuild: true + dev: true optional: true /@esbuild/win32-arm64@0.17.19: @@ -2437,6 +2453,7 @@ packages: cpu: [arm64] os: [win32] requiresBuild: true + dev: true optional: true /@esbuild/win32-ia32@0.17.19: @@ -2454,6 +2471,7 @@ packages: cpu: [ia32] os: [win32] requiresBuild: true + dev: true optional: true /@esbuild/win32-x64@0.17.19: @@ -2471,6 +2489,7 @@ packages: cpu: [x64] os: [win32] requiresBuild: true + dev: true optional: true /@eslint-community/eslint-utils@4.4.0(eslint@8.56.0): @@ -6916,6 +6935,7 @@ packages: dependencies: esbuild: 0.19.12 load-tsconfig: 0.2.5 + dev: true /cac@6.7.14: resolution: {integrity: sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==} @@ -8047,6 +8067,7 @@ packages: '@esbuild/win32-arm64': 0.19.12 '@esbuild/win32-ia32': 0.19.12 '@esbuild/win32-x64': 0.19.12 + dev: true /escalade@3.1.1: resolution: {integrity: sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==} @@ -9575,6 +9596,7 @@ packages: /load-tsconfig@0.2.5: resolution: {integrity: sha512-IXO6OCs9yg8tMKzfPZ1YmheJbZCiEsnBdcB03l0OcfK9prKnJb96siuHCr5Fl37/yo9DnKU+TLpxzTUspw9shg==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + dev: true /load-yaml-file@0.2.0: resolution: {integrity: sha512-OfCBkGEw4nN6JLtgRidPX6QxjBQGQf72q3si2uvqyFEMbycSFFHwAZeXx6cJgFM9wmLrf9zBwCP3Ivqa+LLZPw==}