import * as esbuild from 'esbuild';
import * as fs from 'node:fs';
import http from 'node:http';
import path from 'node:path';
const colors = {
reset: '\x1b[0m',
dim: '\x1b[2m',
bright: '\x1b[1m',
normal: '\x1b[22m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
}
let version;
const importSvg = {
name: 'importSvg',
setup(build) {
const regex = new RegExp(/\.svg$/);
build.onResolve({ filter: /\*\.svg$/ }, args => {
console.log('onresolve', args);
return {
path: path.join('public', 'assets', args.path),
namespace: 'svg-stub'
}
});
build.onLoad({ filter: /\.svg$/, namespace: 'svg-stub' }, async (args) => {
console.log('onload', args);
const svgs = fs.readdirSync(path.resolve(path.dirname(args.path))).filter(fn => regex.test(fn));;
console.log(svgs);
console.log('resolved path', path.join(path.dirname(args.path), 'mapsheets.svg'));
console.log('cwd', process.cwd());
// const contents = `import mapsheets from ./${path.join(path.dirname(args.path), 'mapsheets.svg')};
// console.log('mapsheets', mapsheets);
// export default mapsheets;`;
// const contents = `
// import svg from '/usr/src/app/public/assets/images/scenario-side_show.svg';
// export default svg;
// `;
// const contents = `
// import svg from '/usr/src/app/public/assets/images/scenario-side_show.svg';
// export default svg;
// `;
// const contents = `
// export { default as scenario_sideShow } from '/usr/src/app/public/assets/images/scenario-side_show.svg';
// export { default as mapsheets } from '/usr/src/app/public/assets/images/mapsheets.svg';
// `;
const contents = `
// export { default as countorLines } from './contour-lines.svg';
export { default as mapsheets } from './mapsheets.svg';
`;
console.log('resolveDir', path.dirname(args.path));
console.log('contents', contents);
return {
contents: contents,
resolveDir: path.dirname(args.path) //'./public/assets/images'
}
});
// build.onLoad({ filter: /.*/, namespace: 'svg-stub' }, async (args) => ({
// contents: `import svg from ${JSON.stringify(args.path)}
// export default (imports) =>
// WebAssembly.instantiate(wasm, imports).then(
// result => result.instance.exports)`,
// }));
}
};
const svgUseCacheBust = {
name: 'svgUseCacheBust',
setup(build) {
const regex = new RegExp('mapsheets\..+\.svg');
build.onStart(() => {
console.log("BUILD START");
// const version = Date.now();
// const file = fs.readFileSync('./src/scenario-side_show.svg', { encoding: 'utf-8' });
// const newFile = file.replaceAll('%%VERSION%%', version);
// fs.writeFileSync('./public/assets/images/scenario-side_show.svg', newFile);
// const files = fs.readdirSync('./public/assets/images').filter(fn => regex.test(fn));
// files.forEach(fn => fs.unlinkSync(`./public/assets/images/${fn}`));
// fs.copyFileSync('./public/assets/images/mapsheets.svg', `./public/assets/images/mapsheets.${version}.svg`);
})
}
};
const paths = ['/esbuild', '/index.js', '/map.js', '/soldier_record_block.js'];
const ctx = await esbuild.context({
entryPoints: ['src/*.js'],
bundle: true,
outdir: 'build',
plugins: [svgUseCacheBust, importSvg],
loader: {
'.svg': 'file'
},
});
await ctx.watch();
const { host, port } = await ctx.serve({
servedir: 'build',
port: 3000,
onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
let statusColor = colors.red;
if (status >= 200 && status <= 299) {
statusColor = colors.green;
} else if (status >= 300 && status <= 399) {
statusColor = colors.yellow;
}
console.log(`${colors.dim}${remoteAddress} - "${method} ${path}" ${colors.normal}${statusColor}${status}${colors.reset}${colors.dim} [${timeInMS}ms]${colors.reset}`);
},
});
http.createServer((req, res) => {
const options = {
hostname: host,
port: port,
path: req.url,
method: req.method,
headers: req.headers,
}
if (paths.includes(req.url)) {
const proxyReq = http.request(options, proxyRes => {
for (const k in proxyRes.headers) {
res.setHeader(k, proxyRes.headers[k]);
}
res.writeHead(proxyRes.statusCode, { 'Cache-Control': 'no-cache, no-store, must-revalidate' });
proxyRes.pipe(res, { end: true });
})
return req.pipe(proxyReq, { end: true });
}
console.log(host, port, req.method, req.url, res.statusCode);
const serverUrl = `http://localhost:${port}`;
const url = new URL(`${serverUrl}${req.url}`);
const dir = 'public';
const filePath = path.normalize(
path.join(dir, url.pathname === '/' ? 'index.html' : url.pathname)
);
console.log('filepath', filePath);
for (const k in res.headers) {
res.setHeader(k, res.headers[k]);
}
let contentType;
if (req.url.endsWith('.svg')) {
contentType = 'image/svg+xml';
} else if (req.url.endsWith('.png')) {
contentType = 'image/png';
} else if (req.url.endsWith('.jpg')) {
contentType = 'image/jpeg';
} else if (req.url.endsWith('.css')) {
contentType = 'text/css';
} else {
contentType = 'text/html';
}
res.writeHead(res.statusCode, {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Content-Type': contentType
});
const readStream = fs.createReadStream(filePath, { autoClose: true });
readStream.pipe(res, { end: true });
}).listen(8080);