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.

81 lines
2.9 KiB

4 years ago
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const postcss = require('postcss');
  7. const trim_1 = __importDefault(require("./stylePlugins/trim"));
  8. const scoped_1 = __importDefault(require("./stylePlugins/scoped"));
  9. const styleProcessors_1 = require("./styleProcessors");
  10. function compileStyle(options) {
  11. return doCompileStyle(Object.assign({}, options, { isAsync: false }));
  12. }
  13. exports.compileStyle = compileStyle;
  14. function compileStyleAsync(options) {
  15. return Promise.resolve(doCompileStyle(Object.assign({}, options, { isAsync: true })));
  16. }
  17. exports.compileStyleAsync = compileStyleAsync;
  18. function doCompileStyle(options) {
  19. const { filename, id, scoped = true, trim = true, preprocessLang, postcssOptions, postcssPlugins } = options;
  20. const preprocessor = preprocessLang && styleProcessors_1.processors[preprocessLang];
  21. const preProcessedSource = preprocessor && preprocess(options, preprocessor);
  22. const map = preProcessedSource ? preProcessedSource.map : options.map;
  23. const source = preProcessedSource ? preProcessedSource.code : options.source;
  24. const plugins = (postcssPlugins || []).slice();
  25. if (trim) {
  26. plugins.push(trim_1.default());
  27. }
  28. if (scoped) {
  29. plugins.push(scoped_1.default(id));
  30. }
  31. const postCSSOptions = Object.assign({}, postcssOptions, { to: filename, from: filename });
  32. if (map) {
  33. postCSSOptions.map = {
  34. inline: false,
  35. annotation: false,
  36. prev: map
  37. };
  38. }
  39. let result, code, outMap;
  40. const errors = [];
  41. if (preProcessedSource && preProcessedSource.errors.length) {
  42. errors.push(...preProcessedSource.errors);
  43. }
  44. try {
  45. result = postcss(plugins).process(source, postCSSOptions);
  46. // In async mode, return a promise.
  47. if (options.isAsync) {
  48. return result
  49. .then((result) => ({
  50. code: result.css || '',
  51. map: result.map && result.map.toJSON(),
  52. errors,
  53. rawResult: result
  54. }))
  55. .catch((error) => ({
  56. code: '',
  57. map: undefined,
  58. errors: [...errors, error.message],
  59. rawResult: undefined
  60. }));
  61. }
  62. // force synchronous transform (we know we only have sync plugins)
  63. code = result.css;
  64. outMap = result.map;
  65. }
  66. catch (e) {
  67. errors.push(e);
  68. }
  69. return {
  70. code: code || ``,
  71. map: outMap && outMap.toJSON(),
  72. errors,
  73. rawResult: result
  74. };
  75. }
  76. exports.doCompileStyle = doCompileStyle;
  77. function preprocess(options, preprocessor) {
  78. return preprocessor.render(options.source, options.map, Object.assign({
  79. filename: options.filename
  80. }, options.preprocessOptions));
  81. }