compile.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const chalk = require('chalk');
  2. const log = require('./log');
  3. const fs = require('fs');
  4. const postcss = require('postcss');
  5. const postcssCustomProperties = require('postcss-custom-properties');
  6. const postcssCalc = require("postcss-calc");
  7. const postcssImport = require('postcss-import');
  8. const autoprefixer = require('autoprefixer');
  9. const postcssHeader = require('postcss-header');
  10. module.exports = (filePath, callback) => {
  11. // Transform the file.
  12. fs.readFile(filePath, (err, css) => {
  13. postcss([
  14. postcssImport(),
  15. postcssCustomProperties({
  16. // Remove converted properties from the generated code. This needs to be
  17. // set to ensure that CSS minifiers don't remove the generated values.
  18. preserve: false,
  19. }),
  20. postcssCalc,
  21. autoprefixer({
  22. // Output without visual cascade for more consistency with existing
  23. // Drupal CSS.
  24. cascade: false
  25. }),
  26. postcssHeader({
  27. header: `/*\n * DO NOT EDIT THIS FILE.\n * See the following change record for more information,\n * https://www.drupal.org/node/2815083\n * @preserve\n */\n`,
  28. }),
  29. ])
  30. .process(css, { from: filePath })
  31. .then(result => {
  32. callback(result.css);
  33. })
  34. .catch(error => {
  35. log(chalk.red(error));
  36. process.exitCode = 1;
  37. });
  38. });
  39. };