2024-09-23 14:25:23 +02:00
|
|
|
import fs from 'node:fs';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { dirname } from 'node:path';
|
|
|
|
import * as esbuild from 'esbuild';
|
|
|
|
import { build } from 'esbuild';
|
|
|
|
import { globSync } from 'glob';
|
|
|
|
import { execa } from 'execa';
|
2024-03-30 07:28:19 +01:00
|
|
|
|
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
|
|
|
const _dirname = dirname(_filename);
|
|
|
|
const _package = JSON.parse(fs.readFileSync(_dirname + '/package.json', 'utf-8'));
|
2024-01-22 22:36:44 +01:00
|
|
|
|
2024-09-23 14:25:23 +02:00
|
|
|
const entryPoints = globSync('./src/**/**.{ts,tsx}');
|
2024-01-22 22:36:44 +01:00
|
|
|
|
|
|
|
/** @type {import('esbuild').BuildOptions} */
|
|
|
|
const options = {
|
2024-03-30 07:28:19 +01:00
|
|
|
entryPoints,
|
|
|
|
minify: process.env.NODE_ENV === 'production',
|
2024-09-23 14:25:23 +02:00
|
|
|
outdir: './built',
|
|
|
|
target: 'es2022',
|
|
|
|
platform: 'browser',
|
|
|
|
format: 'esm',
|
2024-03-30 07:28:19 +01:00
|
|
|
sourcemap: 'linked',
|
2024-01-22 22:36:44 +01:00
|
|
|
};
|
|
|
|
|
2024-03-30 07:28:19 +01:00
|
|
|
// built配下をすべて削除する
|
|
|
|
fs.rmSync('./built', { recursive: true, force: true });
|
|
|
|
|
2024-09-23 14:25:23 +02:00
|
|
|
if (process.argv.map(arg => arg.toLowerCase()).includes('--watch')) {
|
2024-03-30 07:28:19 +01:00
|
|
|
await watchSrc();
|
|
|
|
} else {
|
|
|
|
await buildSrc();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildSrc() {
|
|
|
|
console.log(`[${_package.name}] start building...`);
|
|
|
|
|
|
|
|
await build(options)
|
2024-09-23 14:25:23 +02:00
|
|
|
.then(() => {
|
2024-03-30 07:28:19 +01:00
|
|
|
console.log(`[${_package.name}] build succeeded.`);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
process.stderr.write(err.stderr);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
console.log(`[${_package.name}] skip building d.ts because NODE_ENV is production.`);
|
|
|
|
} else {
|
|
|
|
await buildDts();
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`[${_package.name}] finish building.`);
|
2024-01-22 22:36:44 +01:00
|
|
|
}
|
|
|
|
|
2024-03-30 07:28:19 +01:00
|
|
|
function buildDts() {
|
|
|
|
return execa(
|
|
|
|
'tsc',
|
|
|
|
[
|
|
|
|
'--project', 'tsconfig.json',
|
|
|
|
'--outDir', 'built',
|
|
|
|
'--declaration', 'true',
|
|
|
|
'--emitDeclarationOnly', 'true',
|
|
|
|
],
|
|
|
|
{
|
|
|
|
stdout: process.stdout,
|
|
|
|
stderr: process.stderr,
|
2024-09-23 14:25:23 +02:00
|
|
|
},
|
2024-03-30 07:28:19 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function watchSrc() {
|
|
|
|
const plugins = [{
|
|
|
|
name: 'gen-dts',
|
|
|
|
setup(build) {
|
|
|
|
build.onStart(() => {
|
|
|
|
console.log(`[${_package.name}] detect changed...`);
|
|
|
|
});
|
|
|
|
build.onEnd(async result => {
|
|
|
|
if (result.errors.length > 0) {
|
|
|
|
console.error(`[${_package.name}] watch build failed:`, result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await buildDts();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}];
|
|
|
|
|
2024-09-23 14:25:23 +02:00
|
|
|
console.log(`[${_package.name}] start watching...`);
|
2024-03-30 07:28:19 +01:00
|
|
|
|
|
|
|
const context = await esbuild.context({ ...options, plugins });
|
|
|
|
await context.watch();
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
process.on('SIGHUP', resolve);
|
|
|
|
process.on('SIGINT', resolve);
|
|
|
|
process.on('SIGTERM', resolve);
|
|
|
|
process.on('uncaughtException', reject);
|
|
|
|
process.on('exit', resolve);
|
|
|
|
}).finally(async () => {
|
|
|
|
await context.dispose();
|
|
|
|
console.log(`[${_package.name}] finish watching.`);
|
|
|
|
});
|
|
|
|
}
|