compile.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. plugins: [
  16. // On import, remove the comments from variables.pcss.css so they don't
  17. // appear as useless comments at the top files that import these
  18. // variables.
  19. postcss.plugin('remove-unwanted-comments-from-variables', (options) => {
  20. return css => {
  21. if (css.source.input.file.indexOf('variables.pcss.css') !== -1) {
  22. css.walk(node => {
  23. if (node.type === 'comment') {
  24. node.remove();
  25. }
  26. });
  27. }
  28. };
  29. }),
  30. ],
  31. }),
  32. postcssCustomProperties({
  33. // Remove converted properties from the generated code. This needs to be
  34. // set to ensure that CSS minifiers don't remove the generated values.
  35. preserve: false,
  36. }),
  37. postcssCalc,
  38. autoprefixer({
  39. // Output without visual cascade for more consistency with existing
  40. // Drupal CSS.
  41. cascade: false
  42. }),
  43. postcssHeader({
  44. 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`,
  45. }),
  46. ])
  47. .process(css, { from: filePath })
  48. .then(result => {
  49. callback(result.css);
  50. })
  51. .catch(error => {
  52. log(chalk.red(error));
  53. process.exitCode = 1;
  54. });
  55. });
  56. };