mirror of
https://github.com/miro0o/miniWorldMap.git
synced 2026-07-22 07:46:00 +00:00
100 lines
2.6 KiB
JavaScript
100 lines
2.6 KiB
JavaScript
import esbuild from 'esbuild';
|
||
import process from 'process';
|
||
import fs from 'node:fs';
|
||
import path from 'node:path';
|
||
import { builtinModules } from 'node:module';
|
||
|
||
const banner = `/*
|
||
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
|
||
if you want to view the source, please visit the github repository of this plugin
|
||
*/
|
||
`;
|
||
|
||
const prod = process.argv[2] === 'production';
|
||
|
||
// dev 构建直接输出到本地 dev vault 的插件目录(可用 MINI_WORLD_MAP_OUTDIR 覆盖);
|
||
// prod 输出到 ./dist 作为 release 资产。
|
||
const outDir = prod
|
||
? path.resolve('./dist')
|
||
: (process.env.MINI_WORLD_MAP_OUTDIR ??
|
||
path.resolve('./dev-vault/.obsidian/plugins/mini-world-map'));
|
||
|
||
fs.mkdirSync(outDir, { recursive: true });
|
||
|
||
// 'worker:' 前缀导入 → 独立打包成 IIFE 文本(Blob URL Worker 用)
|
||
const inlineWorker = {
|
||
name: 'inline-worker',
|
||
setup(build) {
|
||
build.onResolve({ filter: /^worker:/ }, (args) => ({
|
||
path: path.resolve(path.dirname(args.importer), args.path.slice('worker:'.length)),
|
||
namespace: 'inline-worker',
|
||
}));
|
||
build.onLoad({ filter: /.*/, namespace: 'inline-worker' }, async (args) => {
|
||
const result = await esbuild.build({
|
||
entryPoints: [args.path],
|
||
bundle: true,
|
||
write: false,
|
||
format: 'iife',
|
||
target: 'es2021',
|
||
minify: prod,
|
||
});
|
||
return { contents: result.outputFiles[0].text, loader: 'text' };
|
||
});
|
||
},
|
||
};
|
||
|
||
const copyAssets = {
|
||
name: 'copy-assets',
|
||
setup(build) {
|
||
build.onEnd((result) => {
|
||
if (result.errors.length > 0) return;
|
||
fs.copyFileSync('manifest.json', path.join(outDir, 'manifest.json'));
|
||
fs.copyFileSync('styles.css', path.join(outDir, 'styles.css'));
|
||
fs.writeFileSync(path.join(outDir, 'package.json'), `${JSON.stringify({ type: 'commonjs' }, null, 2)}\n`);
|
||
if (!prod) {
|
||
// pjeby/hot-reload 监听该标记文件所在的插件目录
|
||
fs.writeFileSync(path.join(outDir, '.hotreload'), '');
|
||
}
|
||
});
|
||
},
|
||
};
|
||
|
||
const context = await esbuild.context({
|
||
banner: {
|
||
js: banner,
|
||
},
|
||
entryPoints: ['src/main.ts'],
|
||
bundle: true,
|
||
external: [
|
||
'obsidian',
|
||
'electron',
|
||
'@codemirror/autocomplete',
|
||
'@codemirror/collab',
|
||
'@codemirror/commands',
|
||
'@codemirror/language',
|
||
'@codemirror/lint',
|
||
'@codemirror/search',
|
||
'@codemirror/state',
|
||
'@codemirror/view',
|
||
'@lezer/common',
|
||
'@lezer/highlight',
|
||
'@lezer/lr',
|
||
...builtinModules,
|
||
],
|
||
format: 'cjs',
|
||
target: 'es2021',
|
||
define: { __GALAXY_DEV__: prod ? 'false' : 'true' },
|
||
logLevel: 'info',
|
||
sourcemap: prod ? false : 'inline',
|
||
treeShaking: true,
|
||
outfile: path.join(outDir, 'main.js'),
|
||
minify: prod,
|
||
plugins: [inlineWorker, copyAssets],
|
||
});
|
||
|
||
if (prod) {
|
||
await context.rebuild();
|
||
process.exit(0);
|
||
} else {
|
||
await context.watch();
|
||
}
|