compile.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const chalk = require('chalk');
  2. const log = require('./log');
  3. const fs = require('fs');
  4. const postcss = require('postcss');
  5. const postcssCalc = require("postcss-calc");
  6. const postcssImport = require('postcss-import');
  7. const postcssHeader = require('postcss-header');
  8. const postcssUrl = require('postcss-url');
  9. const postcssPresetEnv = require('postcss-preset-env');
  10. // cspell:ignore pxtorem
  11. const postcssPixelsToRem = require('postcss-pxtorem');
  12. const stylelint = require('stylelint');
  13. module.exports = (filePath, callback) => {
  14. // Transform the file.
  15. fs.readFile(filePath, (err, css) => {
  16. postcss([
  17. postcssImport({
  18. plugins: [
  19. // On import, remove the comments from variables.pcss.css so they don't
  20. // appear as useless comments at the top files that import these
  21. // variables.
  22. postcss.plugin('remove-unwanted-comments-from-variables', (options) => {
  23. return css => {
  24. if (css.source.input.file.indexOf('variables.pcss.css') !== -1) {
  25. css.walk(node => {
  26. if (node.type === 'comment') {
  27. node.remove();
  28. }
  29. });
  30. }
  31. };
  32. }),
  33. ],
  34. }),
  35. postcssPresetEnv({
  36. stage: 1,
  37. preserve: false,
  38. autoprefixer: {
  39. cascade: false,
  40. grid: 'no-autoplace',
  41. },
  42. features: {
  43. 'blank-pseudo-class': false,
  44. 'focus-visible-pseudo-class': false,
  45. 'focus-within-pseudo-class': false,
  46. 'has-pseudo-class': false,
  47. 'image-set-function': false,
  48. 'prefers-color-scheme-query': false,
  49. }
  50. }),
  51. postcssCalc,
  52. postcssPixelsToRem({
  53. propList: [
  54. '*',
  55. '!background-position',
  56. '!border',
  57. '!border-width',
  58. '!box-shadow',
  59. '!border-top*',
  60. '!border-right*',
  61. '!border-bottom*',
  62. '!border-left*',
  63. '!border-start*',
  64. '!border-end*',
  65. '!outline*',
  66. ],
  67. mediaQuery: true,
  68. minPixelValue: 3,
  69. }),
  70. postcssHeader({
  71. header: `/*\n * DO NOT EDIT THIS FILE.\n * See the following change record for more information,\n * https://www.drupal.org/node/3084859\n * @preserve\n */\n`,
  72. }),
  73. postcssUrl({
  74. filter: '**/*.svg',
  75. url: 'inline',
  76. optimizeSvgEncode: true,
  77. })
  78. ])
  79. .process(css, { from: filePath })
  80. .then(result => {
  81. return stylelint.lint({
  82. code: result.css,
  83. fix: true
  84. });
  85. })
  86. .then(result => {
  87. callback(result.output);
  88. })
  89. .catch(error => {
  90. log(chalk.red(error));
  91. process.exitCode = 1;
  92. });
  93. });
  94. };