last-built-plugin.ts 1.1 KB

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