match-files.js 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.matchFiles = void 0;
  4. const tslib_1 = require("tslib");
  5. const path_1 = (0, tslib_1.__importDefault)(require("path"));
  6. const util_1 = (0, tslib_1.__importDefault)(require("util"));
  7. const glob_1 = (0, tslib_1.__importDefault)(require("glob"));
  8. const type_guards_1 = require("./type-guards");
  9. const isLegacyWebpack = (ctx) => !!ctx.options;
  10. const getRootContext = (ctx) => {
  11. if (isLegacyWebpack(ctx)) {
  12. return ctx.options.context;
  13. }
  14. return ctx.rootContext;
  15. };
  16. const flatten = (items) => {
  17. const emptyItems = [];
  18. return emptyItems.concat(...items);
  19. };
  20. const matchFiles = async (ctx, options) => {
  21. const { patterns, globOptions } = options;
  22. const files = await Promise.all(patterns.map(async (pattern) => {
  23. const rootContext = getRootContext(ctx);
  24. const absolutePattern = path_1.default.isAbsolute(pattern) ? pattern : path_1.default.resolve(rootContext, pattern);
  25. const partialFiles = await util_1.default.promisify(glob_1.default)(absolutePattern, globOptions);
  26. return partialFiles.filter(type_guards_1.isStyleFile);
  27. }));
  28. return [...new Set(flatten(files))].map(file => path_1.default.resolve(file));
  29. };
  30. exports.matchFiles = matchFiles;
  31. //# sourceMappingURL=match-files.js.map