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.

87 lines
3.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 _loaderUtils = _interopRequireDefault(require("loader-utils"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. // Examples:
  10. // - ~package
  11. // - ~package/
  12. // - ~@org
  13. // - ~@org/
  14. // - ~@org/package
  15. // - ~@org/package/
  16. const matchModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
  17. /**
  18. * When libsass tries to resolve an import, it uses a special algorithm.
  19. * Since the sass-loader uses webpack to resolve the modules, we need to simulate that algorithm. This function
  20. * returns an array of import paths to try. The last entry in the array is always the original url
  21. * to enable straight-forward webpack.config aliases.
  22. *
  23. * @param {string} url
  24. * @returns {Array<string>}
  25. */
  26. function importsToResolve(url) {
  27. const request = _loaderUtils.default.urlToRequest(url); // Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
  28. // @see https://github.com/webpack-contrib/sass-loader/issues/167
  29. const ext = _path.default.extname(request); // In case there is module request, send this to webpack resolver
  30. if (matchModuleImport.test(url)) {
  31. return [request, url];
  32. } // Because @import is also defined in CSS, Sass needs a way of compiling plain CSS @imports without trying to import the files at compile time.
  33. // To accomplish this, and to ensure SCSS is as much of a superset of CSS as possible, Sass will compile any @imports with the following characteristics to plain CSS imports:
  34. // - imports where the URL ends with .css.
  35. // - imports where the URL begins http:// or https://.
  36. // - imports where the URL is written as a url().
  37. // - imports that have media queries.
  38. //
  39. // The `node-sass` package sends `@import` ending on `.css` to importer, it is bug, so we skip resolve
  40. if (ext === '.css') {
  41. return [];
  42. }
  43. const dirname = _path.default.dirname(request);
  44. const basename = _path.default.basename(request); // In case there is file extension:
  45. //
  46. // 1. Try to resolve `_` file.
  47. // 2. Try to resolve file without `_`.
  48. // 3. Send a original url to webpack resolver, maybe it is alias.
  49. if (['.scss', '.sass'].includes(ext)) {
  50. return [`${dirname}/_${basename}`, `${dirname}/${basename}`, url];
  51. } // In case there is no file extension and filename starts with `_`:
  52. //
  53. // 1. Try to resolve files with `scss`, `sass` and `css` extensions.
  54. // 2. Try to resolve directory with `_index` or `index` filename.
  55. // 3. Send the original url to webpack resolver, maybe it's alias.
  56. if (basename.charAt(0) === '_') {
  57. return [`${request}.scss`, `${request}.sass`, `${request}.css`, request, url];
  58. } // In case there is no file extension and filename doesn't start with `_`:
  59. //
  60. // 1. Try to resolve file starts with `_` and with extensions
  61. // 2. Try to resolve file with extensions
  62. // 3. Try to resolve directory with `_index` or `index` filename.
  63. // 4. Send a original url to webpack resolver, maybe it is alias.
  64. return [`${dirname}/_${basename}.scss`, `${dirname}/_${basename}.sass`, `${dirname}/_${basename}.css`, `${request}.scss`, `${request}.sass`, `${request}.css`, request, url];
  65. }
  66. var _default = importsToResolve;
  67. exports.default = _default;