parse.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var contexts_1 = tslib_1.__importDefault(require("./contexts"));
  5. var parser_1 = tslib_1.__importDefault(require("./parser/parser"));
  6. var plugin_manager_1 = tslib_1.__importDefault(require("./plugin-manager"));
  7. var less_error_1 = tslib_1.__importDefault(require("./less-error"));
  8. var utils = tslib_1.__importStar(require("./utils"));
  9. function default_1(environment, ParseTree, ImportManager) {
  10. var parse = function (input, options, callback) {
  11. if (typeof options === 'function') {
  12. callback = options;
  13. options = utils.copyOptions(this.options, {});
  14. }
  15. else {
  16. options = utils.copyOptions(this.options, options || {});
  17. }
  18. if (!callback) {
  19. var self_1 = this;
  20. return new Promise(function (resolve, reject) {
  21. parse.call(self_1, input, options, function (err, output) {
  22. if (err) {
  23. reject(err);
  24. }
  25. else {
  26. resolve(output);
  27. }
  28. });
  29. });
  30. }
  31. else {
  32. var context_1;
  33. var rootFileInfo = void 0;
  34. var pluginManager_1 = new plugin_manager_1.default(this, !options.reUsePluginManager);
  35. options.pluginManager = pluginManager_1;
  36. context_1 = new contexts_1.default.Parse(options);
  37. if (options.rootFileInfo) {
  38. rootFileInfo = options.rootFileInfo;
  39. }
  40. else {
  41. var filename = options.filename || 'input';
  42. var entryPath = filename.replace(/[^\/\\]*$/, '');
  43. rootFileInfo = {
  44. filename: filename,
  45. rewriteUrls: context_1.rewriteUrls,
  46. rootpath: context_1.rootpath || '',
  47. currentDirectory: entryPath,
  48. entryPath: entryPath,
  49. rootFilename: filename
  50. };
  51. // add in a missing trailing slash
  52. if (rootFileInfo.rootpath && rootFileInfo.rootpath.slice(-1) !== '/') {
  53. rootFileInfo.rootpath += '/';
  54. }
  55. }
  56. var imports_1 = new ImportManager(this, context_1, rootFileInfo);
  57. this.importManager = imports_1;
  58. // TODO: allow the plugins to be just a list of paths or names
  59. // Do an async plugin queue like lessc
  60. if (options.plugins) {
  61. options.plugins.forEach(function (plugin) {
  62. var evalResult, contents;
  63. if (plugin.fileContent) {
  64. contents = plugin.fileContent.replace(/^\uFEFF/, '');
  65. evalResult = pluginManager_1.Loader.evalPlugin(contents, context_1, imports_1, plugin.options, plugin.filename);
  66. if (evalResult instanceof less_error_1.default) {
  67. return callback(evalResult);
  68. }
  69. }
  70. else {
  71. pluginManager_1.addPlugin(plugin);
  72. }
  73. });
  74. }
  75. new parser_1.default(context_1, imports_1, rootFileInfo)
  76. .parse(input, function (e, root) {
  77. if (e) {
  78. return callback(e);
  79. }
  80. callback(null, root, imports_1, options);
  81. }, options);
  82. }
  83. };
  84. return parse;
  85. }
  86. exports.default = default_1;
  87. ;
  88. //# sourceMappingURL=parse.js.map