You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

esbuild.mjs 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import * as esbuild from 'esbuild';
  2. import path from 'path';
  3. import { readFile } from 'fs/promises';
  4. const argv = process.argv.slice(2);
  5. const json = await readFile(new URL('./package.json', import.meta.url), { encoding: 'utf8' });
  6. const pack = JSON.parse(json);
  7. const production = (pack.version.indexOf('-dev') < 0);
  8. const minify = (argv.indexOf('--minify') >= 0);
  9. const serve = (argv.indexOf('--serve') >= 0);
  10. const options = {
  11. //entryPoints: ['src/main.ts'], // AR.default
  12. stdin: {
  13. contents: 'import AR from "./main.ts";\nmodule.exports = AR;',
  14. resolveDir: 'src',
  15. sourcefile: 'index.ts',
  16. },
  17. bundle: true,
  18. minify: minify,
  19. target: ['es2020'],
  20. format: 'iife',
  21. globalName: 'AR',
  22. define: {
  23. __AR_VERSION__: JSON.stringify(pack.version),
  24. __AR_WEBSITE__: JSON.stringify(pack.homepage),
  25. },
  26. legalComments: 'inline',
  27. banner: { js: generateBanner() },
  28. footer: { js: serve ? generateLiveReloadCode() : '' },
  29. outfile: 'www/dist/' + (minify ? 'encantar.min.js' : 'encantar.js'),
  30. sourcemap: !production && 'linked',
  31. logLevel: 'info',
  32. };
  33. if(!serve) {
  34. await esbuild.build(options);
  35. process.exit(0);
  36. }
  37. const ctx = await esbuild.context(options);
  38. await ctx.watch();
  39. await ctx.serve({
  40. host: '0.0.0.0',
  41. port: 8000,
  42. servedir: 'www',
  43. keyfile: path.join(import.meta.dirname, '.local-server.key'),
  44. certfile: path.join(import.meta.dirname, '.local-server.cert'),
  45. });
  46. function generateBanner()
  47. {
  48. const { version, description, homepage, license } = pack;
  49. const author = pack.author.replace('@', '(at)');
  50. const year = new Date().getFullYear();
  51. const date = new Date().toISOString();
  52. return [
  53. `/*!`,
  54. ` * encantar.js version ${version}`,
  55. ` * ${description}`,
  56. ` * Copyright 2022-${year} ${author}`,
  57. ` * ${homepage}`,
  58. ` *`,
  59. ` * @license ${license}`,
  60. ` * Date: ${date}`,
  61. `*/`
  62. ].join('\n');
  63. }
  64. function generateLiveReloadCode()
  65. {
  66. return `
  67. (function liveReload() {
  68. new EventSource('/esbuild').
  69. addEventListener('change', () => location.reload());
  70. })();
  71. `;
  72. }