executable.js 820 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. const fs = require("fs");
  3. const path = require("path");
  4. module.exports = function() {
  5. let banner;
  6. let entry;
  7. return {
  8. options(options) {
  9. entry = path.resolve(options.entry);
  10. return options;
  11. },
  12. load(id) {
  13. if (id !== entry) {
  14. return;
  15. }
  16. const source = fs.readFileSync(id, "utf-8");
  17. const match = source.match(/^\s*(#!.*)/);
  18. if (match) {
  19. banner = match[1];
  20. return (
  21. source.slice(0, match.index) +
  22. source.slice(match.index + banner.length)
  23. );
  24. }
  25. },
  26. transformBundle(code) {
  27. if (banner) {
  28. return { code: banner + "\n" + code };
  29. }
  30. },
  31. onwrite(bundle) {
  32. if (banner) {
  33. fs.chmodSync(bundle.dest, 0o755 & ~process.umask());
  34. }
  35. }
  36. };
  37. };