Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

esbuild.mjs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import * as esbuild from 'esbuild';
  2. import { readFile } from 'fs/promises';
  3. const argv = process.argv.slice(2);
  4. const json = await readFile(new URL('package.json', import.meta.url));
  5. const pack = JSON.parse(json);
  6. const production = (argv.indexOf('--dev') < 0);
  7. const minify = (argv.indexOf('--minify') >= 0);
  8. const serve = (argv.indexOf('--serve') >= 0);
  9. const options = {
  10. //entryPoints: ['src/main.ts'], // AR.default
  11. stdin: {
  12. contents: 'import AR from "./main.ts";\nmodule.exports = AR;',
  13. resolveDir: 'src',
  14. sourcefile: '__main.ts',
  15. },
  16. bundle: true,
  17. minify: minify,
  18. target: ['es2020'],
  19. format: 'iife',
  20. globalName: 'AR',
  21. define: {
  22. __AR_VERSION__: JSON.stringify(pack.version),
  23. __AR_DEVELOPMENT_MODE__: String(!production),
  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. });
  44. function generateBanner()
  45. {
  46. const { version, description, homepage, license } = pack;
  47. const author = pack.author.replace('@', '(at)');
  48. const year = new Date().getFullYear();
  49. const date = new Date().toISOString();
  50. return [
  51. `/*!`,
  52. ` * encantar.js version ${version}`,
  53. ` * ${description}`,
  54. ` * Copyright 2022-${year} ${author}`,
  55. ` * ${homepage}`,
  56. ` *`,
  57. ` * @license ${license}`,
  58. ` * Date: ${date}`,
  59. `*/`
  60. ].join('\n');
  61. }
  62. function generateLiveReloadCode()
  63. {
  64. return `
  65. (function liveReload() {
  66. new EventSource('/esbuild').
  67. addEventListener('change', () => location.reload());
  68. })();
  69. `;
  70. }