read-file-wrapper.js 908 B

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. /*
  3. Copyright 2018 Google LLC
  4. Use of this source code is governed by an MIT-style
  5. license that can be found in the LICENSE file or at
  6. https://opensource.org/licenses/MIT.
  7. */
  8. /**
  9. * A wrapper that calls readFileFn and returns a promise for the contents of
  10. * filePath.
  11. *
  12. * readFileFn is expected to be set to compiler.inputFileSystem.readFile, to
  13. * ensure compatibility with webpack dev server's in-memory filesystem.
  14. *
  15. * @param {Function} readFileFn The function to use for readFile.
  16. * @param {string} filePath The path to the file to read.
  17. * @return {Promise<string>} The contents of the file.
  18. * @private
  19. */
  20. function readFileWrapper(readFileFn, filePath) {
  21. return new Promise((resolve, reject) => {
  22. readFileFn(filePath, (error, data) => {
  23. if (error) {
  24. return reject(error);
  25. }
  26. resolve(data);
  27. });
  28. });
  29. }
  30. module.exports = readFileWrapper;