index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = stylusLoader;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _options = _interopRequireDefault(require("./options.json"));
  8. var _utils = require("./utils");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. async function stylusLoader(source) {
  11. const options = this.getOptions(_options.default);
  12. const callback = this.async();
  13. const implementation = (0, _utils.getStylusImplementation)(this, options.implementation);
  14. if (!implementation) {
  15. callback();
  16. return;
  17. }
  18. let data = source;
  19. if (typeof options.additionalData !== "undefined") {
  20. data = typeof options.additionalData === "function" ? await options.additionalData(data, this) : `${options.additionalData}\n${data}`;
  21. }
  22. const stylusOptions = (0, _utils.getStylusOptions)(this, options);
  23. const styl = implementation(data, stylusOptions); // include regular CSS on @import
  24. if (stylusOptions.includeCSS) {
  25. styl.set("include css", true);
  26. }
  27. if (stylusOptions.hoistAtrules) {
  28. styl.set("hoist atrules", true);
  29. }
  30. if (stylusOptions.lineNumbers) {
  31. styl.set("linenos", true);
  32. }
  33. if (stylusOptions.disableCache) {
  34. styl.set("cache", false);
  35. }
  36. const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
  37. if (stylusOptions.sourcemap || useSourceMap) {
  38. styl.set("sourcemap", useSourceMap ? {
  39. comment: false,
  40. sourceRoot: stylusOptions.dest,
  41. basePath: this.rootContext
  42. } : stylusOptions.sourcemap);
  43. }
  44. if (typeof stylusOptions.use !== "undefined" && stylusOptions.use.length > 0) {
  45. let {
  46. length
  47. } = stylusOptions.use; // eslint-disable-next-line no-plusplus
  48. while (length--) {
  49. let [item] = stylusOptions.use.splice(length, 1);
  50. if (typeof item === "string") {
  51. try {
  52. const resolved = require.resolve(item); // eslint-disable-next-line import/no-dynamic-require, global-require
  53. item = require(resolved)(stylusOptions);
  54. } catch (error) {
  55. callback(`Failed to load "${item}" Stylus plugin. Are you sure it's installed?\n${error}`);
  56. return;
  57. }
  58. }
  59. styl.use(item);
  60. }
  61. }
  62. if (typeof stylusOptions.import !== "undefined") {
  63. for (const imported of stylusOptions.import) {
  64. styl.import(imported);
  65. }
  66. }
  67. if (typeof stylusOptions.include !== "undefined") {
  68. for (const included of stylusOptions.include) {
  69. styl.include(included);
  70. }
  71. }
  72. if (stylusOptions.resolveURL !== false) {
  73. styl.define("url", (0, _utils.urlResolver)(stylusOptions.resolveURL));
  74. }
  75. const shouldUseWebpackImporter = typeof options.webpackImporter === "boolean" ? options.webpackImporter : true;
  76. if (shouldUseWebpackImporter) {
  77. styl.set("Evaluator", await (0, _utils.createEvaluator)(this, source, stylusOptions));
  78. }
  79. if (typeof stylusOptions.define !== "undefined") {
  80. const definitions = Array.isArray(stylusOptions.define) ? stylusOptions.define : Object.entries(stylusOptions.define);
  81. for (const defined of definitions) {
  82. styl.define(...defined);
  83. }
  84. }
  85. styl.render(async (error, css) => {
  86. if (error) {
  87. if (error.filename) {
  88. this.addDependency(_path.default.normalize(error.filename));
  89. }
  90. callback(error);
  91. return;
  92. } // eslint-disable-next-line no-underscore-dangle
  93. if (stylusOptions._imports.length > 0) {
  94. // eslint-disable-next-line no-underscore-dangle
  95. for (const importData of stylusOptions._imports) {
  96. if (_path.default.isAbsolute(importData.path)) {
  97. this.addDependency(_path.default.normalize(importData.path));
  98. } else {
  99. this.addDependency(_path.default.resolve(process.cwd(), importData.path));
  100. }
  101. }
  102. }
  103. let map = styl.sourcemap;
  104. if (map && useSourceMap) {
  105. map = (0, _utils.normalizeSourceMap)(map, stylusOptions.dest);
  106. try {
  107. map.sourcesContent = await Promise.all(map.sources.map(async file => (await (0, _utils.readFile)(this.fs, file)).toString()));
  108. } catch (fsError) {
  109. callback(fsError);
  110. return;
  111. }
  112. }
  113. callback(null, css, map);
  114. });
  115. }