You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.4 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _importsToResolve = _interopRequireDefault(require("./importsToResolve"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name PromisedResolve
  11. * @type {Function}
  12. * @param {string} dir
  13. * @param {string} request
  14. * @returns Promise
  15. */
  16. /**
  17. * @name Importer
  18. * @type {Function}
  19. * @param {string} url
  20. * @param {string} prev
  21. * @param {Function<Error, string>} done
  22. */
  23. const matchCss = /\.css$/;
  24. /**
  25. * Returns an importer that uses webpack's resolving algorithm.
  26. *
  27. * It's important that the returned function has the correct number of arguments
  28. * (based on whether the call is sync or async) because otherwise node-sass doesn't exit.
  29. *
  30. * @param {string} resourcePath
  31. * @param {PromisedResolve} resolve
  32. * @param {Function<string>} addNormalizedDependency
  33. * @returns {Importer}
  34. */
  35. function webpackImporter(resourcePath, resolve, addNormalizedDependency) {
  36. function dirContextFrom(fileContext) {
  37. return _path.default.dirname( // The first file is 'stdin' when we're using the data option
  38. fileContext === 'stdin' ? resourcePath : fileContext);
  39. } // eslint-disable-next-line no-shadow
  40. function startResolving(dir, importsToResolve) {
  41. return importsToResolve.length === 0 ? Promise.reject() : resolve(dir, importsToResolve[0]).then(resolvedFile => {
  42. // Add the resolvedFilename as dependency. Although we're also using stats.includedFiles, this might come
  43. // in handy when an error occurs. In this case, we don't get stats.includedFiles from node-sass.
  44. addNormalizedDependency(resolvedFile);
  45. return {
  46. // By removing the CSS file extension, we trigger node-sass to include the CSS file instead of just linking it.
  47. file: resolvedFile.replace(matchCss, '')
  48. };
  49. }, () => {
  50. const [, ...tailResult] = importsToResolve;
  51. return startResolving(dir, tailResult);
  52. });
  53. }
  54. return (url, prev, done) => {
  55. startResolving(dirContextFrom(prev), (0, _importsToResolve.default)(url)) // Catch all resolving errors, return the original file and pass responsibility back to other custom importers
  56. .catch(() => {
  57. return {
  58. file: url
  59. };
  60. }).then(done);
  61. };
  62. }
  63. var _default = webpackImporter;
  64. exports.default = _default;