提交学习笔记专用
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.

171 lines
4.3 KiB

  1. 'use strict';
  2. const path = require('path');
  3. /** @type {any} */
  4. const postcss = require('postcss');
  5. const yaml = require('yaml');
  6. const { lilconfigSync } = require('lilconfig');
  7. const cssnano = 'cssnano';
  8. /** @typedef {{preset?: any, plugins?: any[], configFile?: string}} Options */
  9. /**
  10. * @param {string} moduleId
  11. * @returns {boolean}
  12. */
  13. function isResolvable(moduleId) {
  14. try {
  15. require.resolve(moduleId);
  16. return true;
  17. } catch (e) {
  18. return false;
  19. }
  20. }
  21. /**
  22. * preset can be one of four possibilities:
  23. * preset = 'default'
  24. * preset = ['default', {}]
  25. * preset = function <- to be invoked
  26. * preset = {plugins: []} <- already invoked function
  27. *
  28. * @param {any} preset
  29. * @return {[import('postcss').PluginCreator<any>, boolean | Record<string, any> | undefined][]}}
  30. */
  31. function resolvePreset(preset) {
  32. let fn, options;
  33. if (Array.isArray(preset)) {
  34. fn = preset[0];
  35. options = preset[1];
  36. } else {
  37. fn = preset;
  38. options = {};
  39. }
  40. // For JS setups where we invoked the preset already
  41. if (preset.plugins) {
  42. return preset.plugins;
  43. }
  44. // Provide an alias for the default preset, as it is built-in.
  45. if (fn === 'default') {
  46. return require('cssnano-preset-default')(options).plugins;
  47. }
  48. // For non-JS setups; we'll need to invoke the preset ourselves.
  49. if (typeof fn === 'function') {
  50. return fn(options).plugins;
  51. }
  52. // Try loading a preset from node_modules
  53. if (isResolvable(fn)) {
  54. return require(fn)(options).plugins;
  55. }
  56. const sugar = `cssnano-preset-${fn}`;
  57. // Try loading a preset from node_modules (sugar)
  58. if (isResolvable(sugar)) {
  59. return require(sugar)(options).plugins;
  60. }
  61. // If all else fails, we probably have a typo in the config somewhere
  62. throw new Error(
  63. `Cannot load preset "${fn}". Please check your configuration for errors and try again.`
  64. );
  65. }
  66. /**
  67. * cssnano will look for configuration firstly as options passed
  68. * directly to it, and failing this it will use lilconfig to
  69. * load an external file.
  70. * @param {Options} options
  71. */
  72. function resolveConfig(options) {
  73. if (options.preset) {
  74. return resolvePreset(options.preset);
  75. }
  76. /** @type {string | undefined} */
  77. let searchPath = process.cwd();
  78. let configPath = undefined;
  79. if (options.configFile) {
  80. searchPath = undefined;
  81. configPath = path.resolve(process.cwd(), options.configFile);
  82. }
  83. const configExplorer = lilconfigSync(cssnano, {
  84. searchPlaces: [
  85. 'package.json',
  86. '.cssnanorc',
  87. '.cssnanorc.json',
  88. '.cssnanorc.yaml',
  89. '.cssnanorc.yml',
  90. '.cssnanorc.js',
  91. 'cssnano.config.js',
  92. ],
  93. loaders: {
  94. '.yaml': (filepath, content) => yaml.parse(content),
  95. '.yml': (filepath, content) => yaml.parse(content),
  96. },
  97. });
  98. const config = configPath
  99. ? configExplorer.load(configPath)
  100. : configExplorer.search(searchPath);
  101. if (config === null) {
  102. return resolvePreset('default');
  103. }
  104. return resolvePreset(config.config.preset || config.config);
  105. }
  106. /**
  107. * @type {import('postcss').PluginCreator<Options>}
  108. * @param {Options=} options
  109. * @return {import('postcss').Processor}
  110. */
  111. function cssnanoPlugin(options = {}) {
  112. if (Array.isArray(options.plugins)) {
  113. if (!options.preset || !options.preset.plugins) {
  114. options.preset = { plugins: [] };
  115. }
  116. options.plugins.forEach((plugin) => {
  117. if (Array.isArray(plugin)) {
  118. const [pluginDef, opts = {}] = plugin;
  119. if (typeof pluginDef === 'string' && isResolvable(pluginDef)) {
  120. options.preset.plugins.push([require(pluginDef), opts]);
  121. } else {
  122. options.preset.plugins.push([pluginDef, opts]);
  123. }
  124. } else if (typeof plugin === 'string' && isResolvable(plugin)) {
  125. options.preset.plugins.push([require(plugin), {}]);
  126. } else {
  127. options.preset.plugins.push([plugin, {}]);
  128. }
  129. });
  130. }
  131. const plugins = [];
  132. const nanoPlugins = resolveConfig(options);
  133. for (const nanoPlugin of nanoPlugins) {
  134. if (Array.isArray(nanoPlugin)) {
  135. const [processor, opts] = nanoPlugin;
  136. if (
  137. typeof opts === 'undefined' ||
  138. (typeof opts === 'object' && !opts.exclude) ||
  139. (typeof opts === 'boolean' && opts === true)
  140. ) {
  141. plugins.push(processor(opts));
  142. }
  143. } else {
  144. plugins.push(nanoPlugin);
  145. }
  146. }
  147. return postcss(plugins);
  148. }
  149. cssnanoPlugin.postcss = true;
  150. module.exports = cssnanoPlugin;