Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/semi-json-viewer-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 24 additions & 8 deletions packages/semi-json-viewer-core/script/compileLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const compileWorker = async ()=>{
};


const buildMain = async ()=>{
const buildMain = async (format)=>{
const mainEntry = path.join(__dirname, "..", "src/index.ts");

const result = await esbuild.build({
entryPoints: [mainEntry],
bundle: true,
packages: 'external',
write: false,
format: 'esm'
format: format
});
return result.outputFiles[0].text;

Expand All @@ -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();
Loading