last-built-plugin.ts 1.2 KB

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