last-built-plugin.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import fs from 'node:fs';
  2. import path from 'node:path';
  3. import type webpack from 'webpack';
  4. type Options = {
  5. basePath: string;
  6. };
  7. class LastBuiltPlugin {
  8. basePath: string;
  9. isWatchMode: boolean;
  10. constructor({basePath}: Options) {
  11. this.basePath = basePath;
  12. this.isWatchMode = false;
  13. }
  14. apply(compiler: webpack.Compiler) {
  15. compiler.hooks.watchRun.tapAsync('LastBuiltPlugin', (_, callback) => {
  16. this.isWatchMode = true;
  17. callback();
  18. });
  19. compiler.hooks.done.tapAsync('LastBuiltPlugin', (_, callback) => {
  20. // If this is in watch mode, then assets will *NOT* be written to disk
  21. // We only want to record when we write to disk since this plugin is for
  22. // our acceptance test (which require assets to be on fs)
  23. if (this.isWatchMode) {
  24. callback();
  25. return;
  26. }
  27. fs.writeFile(
  28. path.join(this.basePath, '.webpack.meta'),
  29. JSON.stringify({
  30. // in seconds
  31. built: Math.floor(new Date(new Date().toUTCString()).getTime() / 1000),
  32. }),
  33. callback
  34. );
  35. });
  36. }
  37. }
  38. export default LastBuiltPlugin;