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.

66 lines
1.7 KiB

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