index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _browserslist = _interopRequireDefault(require("browserslist"));
  7. var _plugins = _interopRequireDefault(require("./plugins"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function pluginCreator(opts = {}) {
  10. return {
  11. postcssPlugin: 'stylehacks',
  12. OnceExit(css, {
  13. result
  14. }) {
  15. const resultOpts = result.opts || {};
  16. const browsers = (0, _browserslist.default)(null, {
  17. stats: resultOpts.stats,
  18. path: __dirname,
  19. env: resultOpts.env
  20. });
  21. const processors = _plugins.default.reduce((list, Plugin) => {
  22. const hack = new Plugin(result);
  23. const applied = browsers.some(browser => {
  24. return hack.targets.some(target => browser === target);
  25. });
  26. if (applied) {
  27. return list;
  28. }
  29. return [...list, hack];
  30. }, []);
  31. css.walk(node => {
  32. processors.forEach(proc => {
  33. if (!~proc.nodeTypes.indexOf(node.type)) {
  34. return;
  35. }
  36. if (opts.lint) {
  37. return proc.detectAndWarn(node);
  38. }
  39. return proc.detectAndResolve(node);
  40. });
  41. });
  42. }
  43. };
  44. }
  45. pluginCreator.detect = node => {
  46. return _plugins.default.some(Plugin => {
  47. const hack = new Plugin();
  48. return hack.any(node);
  49. });
  50. };
  51. pluginCreator.postcss = true;
  52. var _default = pluginCreator;
  53. exports.default = _default;
  54. module.exports = exports.default;