index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _path = require('path');
  6. var _path2 = _interopRequireDefault(_path);
  7. var _postcss = require('postcss');
  8. var _postcss2 = _interopRequireDefault(_postcss);
  9. var _cosmiconfig = require('cosmiconfig');
  10. var _cosmiconfig2 = _interopRequireDefault(_cosmiconfig);
  11. var _isResolvable = require('is-resolvable');
  12. var _isResolvable2 = _interopRequireDefault(_isResolvable);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. const cssnano = 'cssnano';
  15. function initializePlugin(plugin, css, result) {
  16. if (Array.isArray(plugin)) {
  17. const [processor, opts] = plugin;
  18. if (typeof opts === 'undefined' || typeof opts === 'object' && !opts.exclude || typeof opts === 'boolean' && opts === true) {
  19. return Promise.resolve(processor(opts)(css, result));
  20. }
  21. } else {
  22. return Promise.resolve(plugin()(css, result));
  23. }
  24. // Handle excluded plugins
  25. return Promise.resolve();
  26. }
  27. /*
  28. * preset can be one of four possibilities:
  29. * preset = 'default'
  30. * preset = ['default', {}]
  31. * preset = function <- to be invoked
  32. * preset = {plugins: []} <- already invoked function
  33. */
  34. function resolvePreset(preset) {
  35. let fn, options;
  36. if (Array.isArray(preset)) {
  37. fn = preset[0];
  38. options = preset[1];
  39. } else {
  40. fn = preset;
  41. options = {};
  42. }
  43. // For JS setups where we invoked the preset already
  44. if (preset.plugins) {
  45. return Promise.resolve(preset.plugins);
  46. }
  47. // Provide an alias for the default preset, as it is built-in.
  48. if (fn === 'default') {
  49. return Promise.resolve(require('cssnano-preset-default')(options).plugins);
  50. }
  51. // For non-JS setups; we'll need to invoke the preset ourselves.
  52. if (typeof fn === 'function') {
  53. return Promise.resolve(fn(options).plugins);
  54. }
  55. // Try loading a preset from node_modules
  56. if ((0, _isResolvable2.default)(fn)) {
  57. return Promise.resolve(require(fn)(options).plugins);
  58. }
  59. const sugar = `cssnano-preset-${fn}`;
  60. // Try loading a preset from node_modules (sugar)
  61. if ((0, _isResolvable2.default)(sugar)) {
  62. return Promise.resolve(require(sugar)(options).plugins);
  63. }
  64. // If all else fails, we probably have a typo in the config somewhere
  65. throw new Error(`Cannot load preset "${fn}". Please check your configuration for errors and try again.`);
  66. }
  67. /*
  68. * cssnano will look for configuration firstly as options passed
  69. * directly to it, and failing this it will use cosmiconfig to
  70. * load an external file.
  71. */
  72. function resolveConfig(css, result, options) {
  73. if (options.preset) {
  74. return resolvePreset(options.preset);
  75. }
  76. const inputFile = css.source && css.source.input && css.source.input.file;
  77. let searchPath = inputFile ? _path2.default.dirname(inputFile) : process.cwd();
  78. let configPath = null;
  79. if (options.configFile) {
  80. searchPath = null;
  81. configPath = _path2.default.resolve(process.cwd(), options.configFile);
  82. }
  83. const configExplorer = (0, _cosmiconfig2.default)(cssnano);
  84. const searchForConfig = configPath ? configExplorer.load(configPath) : configExplorer.search(searchPath);
  85. return searchForConfig.then(config => {
  86. if (config === null) {
  87. return resolvePreset('default');
  88. }
  89. return resolvePreset(config.config.preset || config.config);
  90. });
  91. }
  92. exports.default = _postcss2.default.plugin(cssnano, (options = {}) => {
  93. return (css, result) => {
  94. return resolveConfig(css, result, options).then(plugins => {
  95. return plugins.reduce((promise, plugin) => {
  96. return promise.then(initializePlugin.bind(null, plugin, css, result));
  97. }, Promise.resolve());
  98. });
  99. };
  100. });
  101. module.exports = exports['default'];