loader.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require("postcss");
  6. var _postcss2 = _interopRequireDefault(_postcss);
  7. var _fs = require("fs");
  8. var _fs2 = _interopRequireDefault(_fs);
  9. var _path = require("path");
  10. var _path2 = _interopRequireDefault(_path);
  11. var _parser = require("./parser");
  12. var _parser2 = _interopRequireDefault(_parser);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. // Copied from https://github.com/css-modules/css-modules-loader-core
  15. class Core {
  16. constructor(plugins) {
  17. this.plugins = plugins || Core.defaultPlugins;
  18. }
  19. load(sourceString, sourcePath, trace, pathFetcher) {
  20. let parser = new _parser2.default(pathFetcher, trace);
  21. return (0, _postcss2.default)(this.plugins.concat([parser.plugin()])).process(sourceString, { from: "/" + sourcePath }).then(result => {
  22. return {
  23. injectableSource: result.css,
  24. exportTokens: parser.exportTokens
  25. };
  26. });
  27. }
  28. }
  29. // Sorts dependencies in the following way:
  30. // AAA comes before AA and A
  31. // AB comes after AA and before A
  32. // All Bs come after all As
  33. // This ensures that the files are always returned in the following order:
  34. // - In the order they were required, except
  35. // - After all their dependencies
  36. const traceKeySorter = (a, b) => {
  37. if (a.length < b.length) {
  38. return a < b.substring(0, a.length) ? -1 : 1;
  39. } else if (a.length > b.length) {
  40. return a.substring(0, b.length) <= b ? -1 : 1;
  41. } else {
  42. return a < b ? -1 : 1;
  43. }
  44. };
  45. class FileSystemLoader {
  46. constructor(root, plugins) {
  47. this.root = root;
  48. this.sources = {};
  49. this.traces = {};
  50. this.importNr = 0;
  51. this.core = new Core(plugins);
  52. this.tokensByFile = {};
  53. }
  54. fetch(_newPath, relativeTo, _trace) {
  55. let newPath = _newPath.replace(/^["']|["']$/g, ""),
  56. trace = _trace || String.fromCharCode(this.importNr++);
  57. return new Promise((resolve, reject) => {
  58. let relativeDir = _path2.default.dirname(relativeTo),
  59. rootRelativePath = _path2.default.resolve(relativeDir, newPath),
  60. fileRelativePath = _path2.default.resolve(_path2.default.join(this.root, relativeDir), newPath);
  61. // if the path is not relative or absolute, try to resolve it in node_modules
  62. if (newPath[0] !== "." && newPath[0] !== "/") {
  63. try {
  64. fileRelativePath = require.resolve(newPath);
  65. } catch (e) {
  66. // noop
  67. }
  68. }
  69. const tokens = this.tokensByFile[fileRelativePath];
  70. if (tokens) {
  71. return resolve(tokens);
  72. }
  73. _fs2.default.readFile(fileRelativePath, "utf-8", (err, source) => {
  74. if (err) reject(err);
  75. this.core.load(source, rootRelativePath, trace, this.fetch.bind(this)).then(({ injectableSource, exportTokens }) => {
  76. this.sources[fileRelativePath] = injectableSource;
  77. this.traces[trace] = fileRelativePath;
  78. this.tokensByFile[fileRelativePath] = exportTokens;
  79. resolve(exportTokens);
  80. }, reject);
  81. });
  82. });
  83. }
  84. get finalSource() {
  85. const traces = this.traces;
  86. const sources = this.sources;
  87. let written = new Set();
  88. return Object.keys(traces).sort(traceKeySorter).map(key => {
  89. const filename = traces[key];
  90. if (written.has(filename)) {
  91. return null;
  92. }
  93. written.add(filename);
  94. return sources[filename];
  95. }).join("");
  96. }
  97. }
  98. exports.default = FileSystemLoader;