From c24c66a015c4d8da67615305e69288687baaf860 Mon Sep 17 00:00:00 2001 From: tianfeng Date: Tue, 30 Dec 2025 11:20:49 +0800 Subject: [PATCH] fix: support jsonviewer cjs --- packages/semi-json-viewer-core/package.json | 4 +-- .../script/compileLib.js | 32 ++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/semi-json-viewer-core/package.json b/packages/semi-json-viewer-core/package.json index c2172ced6d..7e2bd66513 100644 --- a/packages/semi-json-viewer-core/package.json +++ b/packages/semi-json-viewer-core/package.json @@ -2,8 +2,8 @@ "name": "@douyinfe/semi-json-viewer-core", "version": "2.89.0-beta.0", "description": "", - "main": "lib/index.js", - "module": "lib/index.js", + "main": "lib/cjs/index.js", + "module": "lib/es/index.js", "typings": "src/index.ts", "scripts": { "build:lib": "node ./script/compileLib.js", diff --git a/packages/semi-json-viewer-core/script/compileLib.js b/packages/semi-json-viewer-core/script/compileLib.js index a97cb29067..96500c3f26 100644 --- a/packages/semi-json-viewer-core/script/compileLib.js +++ b/packages/semi-json-viewer-core/script/compileLib.js @@ -16,7 +16,7 @@ const compileWorker = async ()=>{ }; -const buildMain = async ()=>{ +const buildMain = async (format)=>{ const mainEntry = path.join(__dirname, "..", "src/index.ts"); const result = await esbuild.build({ @@ -24,7 +24,7 @@ const buildMain = async ()=>{ bundle: true, packages: 'external', write: false, - format: 'esm' + format: format }); return result.outputFiles[0].text; @@ -35,21 +35,37 @@ const buildMain = async ()=>{ const compile = async ()=>{ const workerRaw = await compileWorker(); - const mainRaw = await buildMain(); + // 编译 ESM 格式 + const mainRawESM = await buildMain('esm'); + const finalRawESM = mainRawESM.replaceAll("%WORKER_RAW%", encodeURIComponent(workerRaw)); - const finalRaw = mainRaw.replaceAll("%WORKER_RAW%", encodeURIComponent(workerRaw)); + // 编译 CJS 格式 + const mainRawCJS = await buildMain('cjs'); + const finalRawCJS = mainRawCJS.replaceAll("%WORKER_RAW%", encodeURIComponent(workerRaw)); - const saveDir = path.join(__dirname, "..", "lib"); + const libDir = path.join(__dirname, "..", "lib"); + const esDir = path.join(libDir, "es"); + const cjsDir = path.join(libDir, "cjs"); const workerSaveDir = path.join(__dirname, "..", "workerLib"); - if (!fs.existsSync(saveDir)) { - fs.mkdirSync(saveDir); + // 创建必要的目录 + if (!fs.existsSync(libDir)) { + fs.mkdirSync(libDir); + } + if (!fs.existsSync(esDir)) { + fs.mkdirSync(esDir, { recursive: true }); + } + if (!fs.existsSync(cjsDir)) { + fs.mkdirSync(cjsDir, { recursive: true }); } if (!fs.existsSync(workerSaveDir)) { fs.mkdirSync(workerSaveDir); } + + // 保存文件 fs.writeFileSync(path.join(workerSaveDir, "worker.js"), workerRaw, 'utf8'); - fs.writeFileSync(path.join(saveDir, "index.js"), finalRaw, 'utf8'); + fs.writeFileSync(path.join(esDir, "index.js"), finalRawESM, 'utf8'); + fs.writeFileSync(path.join(cjsDir, "index.js"), finalRawCJS, 'utf8'); }; compile();