last-built-plugin.js 1.0 KB

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