precompiler-hook.js 894 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const fs = require('fs');
  3. const sourceMapSupport = require('source-map-support');
  4. const installPrecompiler = require('require-precompiled');
  5. const options = require('./options').get();
  6. function installSourceMapSupport() {
  7. sourceMapSupport.install({
  8. environment: 'node',
  9. handleUncaughtExceptions: false,
  10. retrieveSourceMap(url) {
  11. const precompiled = options.precompiled[url];
  12. if (!precompiled) {
  13. return null;
  14. }
  15. try {
  16. const map = fs.readFileSync(`${precompiled}.map`, 'utf8');
  17. return {url, map};
  18. } catch (err) {
  19. if (err.code === 'ENOENT') {
  20. return null;
  21. }
  22. throw err;
  23. }
  24. }
  25. });
  26. }
  27. function install() {
  28. installSourceMapSupport();
  29. installPrecompiler(filename => {
  30. const precompiled = options.precompiled[filename];
  31. return precompiled ?
  32. fs.readFileSync(precompiled, 'utf8') :
  33. null;
  34. });
  35. }
  36. exports.install = install;