vendor-assets.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env node
  2. /**
  3. * Copies third-party browser libraries out of node_modules into public/vendor/.
  4. *
  5. * Workers Static Assets are served verbatim — nothing in public/ goes through a
  6. * bundler — so a library from npm has to be physically present there. Keeping
  7. * it a copy step (rather than a checked-in blob or a CDN <script>) means the
  8. * version is pinned by package.json, there is no third-party origin at runtime,
  9. * and the CSP can stay `script-src 'self'`.
  10. *
  11. * public/vendor/ is gitignored; `npm run dev` and `npm run deploy` both run this
  12. * first, so it is always present and always matches the lockfile.
  13. */
  14. import { copyFileSync, mkdirSync, existsSync } from 'node:fs';
  15. import { dirname, join } from 'node:path';
  16. import { fileURLToPath } from 'node:url';
  17. const root = dirname(dirname(fileURLToPath(import.meta.url)));
  18. const vendorDir = join(root, 'public', 'vendor');
  19. const FILES = [
  20. ['node_modules/chart.js/dist/chart.umd.js', 'chart.umd.js'],
  21. ['node_modules/chart.js/LICENSE.md', 'chart.js-LICENSE.md'],
  22. ];
  23. mkdirSync(vendorDir, { recursive: true });
  24. for (const [from, to] of FILES) {
  25. const source = join(root, from);
  26. if (!existsSync(source)) {
  27. console.error(`vendor-assets: missing ${from} — run \`npm install\` first`);
  28. process.exit(1);
  29. }
  30. copyFileSync(source, join(vendorDir, to));
  31. }
  32. console.log(`vendor-assets: copied ${FILES.length} file(s) into public/vendor/`);