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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.endsWith('-dev');
  8. const minify = (argv.indexOf('--minify') >= 0);
  9. const serve = (argv.indexOf('--serve') >= 0);
  10. const options = {
  11. //entryPoints: ['src/main.ts'], // AR.AR
  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. __AR_FLAGS__ : String(process.env.AR_FLAGS ?? 0),
  26. },
  27. legalComments: 'inline',
  28. banner: { js: generateBanner() },
  29. footer: { js: serve ? generateLiveReloadCode() : '' },
  30. outfile: 'www/dist/' + (minify ? 'encantar.min.js' : 'encantar.js'),
  31. sourcemap: !production && 'linked',
  32. logLevel: 'info',
  33. };
  34. if(!serve) {
  35. await esbuild.build(options);
  36. process.exit(0);
  37. }
  38. const ctx = await esbuild.context(options);
  39. await ctx.watch();
  40. await ctx.serve({
  41. host: '0.0.0.0',
  42. port: 8000,
  43. servedir: 'www',
  44. keyfile: path.join(import.meta.dirname, '.local-server.key'),
  45. certfile: path.join(import.meta.dirname, '.local-server.cert'),
  46. });
  47. function generateBanner()
  48. {
  49. const { version, description, homepage, license } = pack;
  50. const author = pack.author.replace('@', '(at)');
  51. const year = new Date().getFullYear();
  52. const date = new Date().toISOString();
  53. return [
  54. `/*!`,
  55. ` * encantar.js version ${version}`,
  56. ` * ${description}`,
  57. ` * Copyright 2022-${year} ${author}`,
  58. ` * ${homepage}`,
  59. ` *`,
  60. ` * @license ${license}`,
  61. ` * Date: ${date}`,
  62. `*/`
  63. ].join('\n');
  64. }
  65. function generateLiveReloadCode()
  66. {
  67. return `
  68. (function liveReload() {
  69. new EventSource('/esbuild').
  70. addEventListener('change', () => location.reload());
  71. })();
  72. `;
  73. }