sass.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*! sass.js - v0.11.1 (f286436) - built 2019-10-20
  2. providing libsass 3.6.2 (4da7c4bd)
  3. via emscripten 1.38.31 (040e49a)
  4. */
  5. (function (root, factory) {
  6. "use strict";
  7. if (typeof define === "function" && define.amd) {
  8. define([], factory);
  9. } else if (typeof exports === "object") {
  10. module.exports = factory();
  11. } else {
  12. root.Sass = factory();
  13. }
  14. }(this, function () {/*global document*/
  15. // identify the path sass.js is located at in case we're loaded by a simple
  16. // <script src="path/to/sass.js"></script>
  17. // this path can be used to identify the location of
  18. // * sass.worker.js from sass.js
  19. // * libsass.js.mem from sass.sync.js
  20. // see https://github.com/medialize/sass.js/pull/32#issuecomment-103142214
  21. // see https://github.com/medialize/sass.js/issues/33
  22. var SASSJS_RELATIVE_PATH = (function () {
  23. "use strict";
  24. // in Node things are rather simple
  25. var hasDir = typeof __dirname !== "undefined";
  26. if (hasDir) {
  27. return __dirname;
  28. }
  29. // we can only run this test in the browser,
  30. // so make sure we actually have a DOM to work with.
  31. if (typeof document === "undefined" || !document.getElementsByTagName) {
  32. return null;
  33. }
  34. // http://www.2ality.com/2014/05/current-script.html
  35. var currentScript = document.currentScript || (function () {
  36. var scripts = document.getElementsByTagName("script");
  37. return scripts[scripts.length - 1];
  38. })();
  39. var path = currentScript && currentScript.src;
  40. if (!path) {
  41. return null;
  42. }
  43. // [worker] make sure we're not running in some concatenated thing
  44. if (path.slice(-8) === "/sass.js") {
  45. return path.slice(0, -8);
  46. }
  47. // [sync] make sure we're not running in some concatenated thing
  48. if (path.slice(-13) === "/sass.sync.js") {
  49. return path.slice(0, -13);
  50. }
  51. return null;
  52. })() || ".";
  53. /*global Worker, SASSJS_RELATIVE_PATH*/
  54. "use strict";
  55. var noop = function () {
  56. };
  57. var slice = [].slice;
  58. // defined upon first Sass.initialize() call
  59. var globalWorkerUrl;
  60. function Sass(workerUrl) {
  61. if (!workerUrl && !globalWorkerUrl) {
  62. /*jshint laxbreak:true */
  63. throw new Error(
  64. "Sass needs to be initialized with the URL of sass.worker.js - "
  65. + "either via Sass.setWorkerUrl(url) or by new Sass(url)"
  66. );
  67. /*jshint laxbreak:false */
  68. }
  69. if (!globalWorkerUrl) {
  70. globalWorkerUrl = workerUrl;
  71. }
  72. // bind all functions
  73. // we're doing this because we used to have a single hard-wired instance that allowed
  74. // [].map(Sass.removeFile) and we need to maintain that for now (at least until 1.0.0)
  75. for (var key in this) {
  76. if (typeof this[key] === "function") {
  77. this[key] = this[key].bind(this);
  78. }
  79. }
  80. this._callbacks = {};
  81. this._worker = new Worker(workerUrl || globalWorkerUrl);
  82. this._worker.addEventListener("message", this._handleWorkerMessage, false);
  83. }
  84. // allow setting the workerUrl before the first Sass instance is initialized,
  85. // where registering the global workerUrl would've happened automatically
  86. Sass.setWorkerUrl = function (workerUrl) {
  87. globalWorkerUrl = workerUrl;
  88. };
  89. Sass.style = {
  90. nested: 0,
  91. expanded: 1,
  92. compact: 2,
  93. compressed: 3
  94. };
  95. Sass.comments = {
  96. "none": 0,
  97. "default": 1
  98. };
  99. Sass.prototype = {
  100. style: Sass.style,
  101. comments: Sass.comments,
  102. destroy: function () {
  103. this._worker && this._worker.terminate();
  104. this._worker = null;
  105. this._callbacks = {};
  106. this._importer = null;
  107. },
  108. _handleWorkerMessage: function (event) {
  109. if (event.data.command) {
  110. this[event.data.command](event.data.args);
  111. }
  112. this._callbacks[event.data.id] && this._callbacks[event.data.id](event.data.result);
  113. delete this._callbacks[event.data.id];
  114. },
  115. _dispatch: function (options, callback) {
  116. if (!this._worker) {
  117. throw new Error("Sass worker has been terminated");
  118. }
  119. options.id = "cb" + Date.now() + Math.random();
  120. this._callbacks[options.id] = callback;
  121. this._worker.postMessage(options);
  122. },
  123. _importerInit: function (args) {
  124. // importer API done callback pushing results
  125. // back to the worker
  126. var done = function done(result) {
  127. this._worker.postMessage({
  128. command: "_importerFinish",
  129. args: [result]
  130. });
  131. }.bind(this);
  132. try {
  133. this._importer(args[0], done);
  134. } catch (e) {
  135. done({error: e.message});
  136. throw e;
  137. }
  138. },
  139. importer: function (importerCallback, callback) {
  140. if (typeof importerCallback !== "function" && importerCallback !== null) {
  141. throw new Error("importer callback must either be a function or null");
  142. }
  143. // callback is executed in the main EventLoop
  144. this._importer = importerCallback;
  145. // tell worker to activate importer callback
  146. this._worker.postMessage({
  147. command: "importer",
  148. args: [Boolean(importerCallback)]
  149. });
  150. callback && callback();
  151. }
  152. };
  153. var commands = "writeFile readFile listFiles removeFile clearFiles lazyFiles preloadFiles options compile compileFile";
  154. commands.split(" ").forEach(function (command) {
  155. Sass.prototype[command] = function () {
  156. var callback = slice.call(arguments, -1)[0];
  157. var args = slice.call(arguments, 0, -1);
  158. if (typeof callback !== "function") {
  159. args.push(callback);
  160. callback = noop;
  161. }
  162. this._dispatch({
  163. command: command,
  164. args: args
  165. }, callback);
  166. };
  167. });
  168. // automatically set the workerUrl in case we're loaded by a simple
  169. // <script src="path/to/sass.js"></script>
  170. // see https://github.com/medialize/sass.js/pull/32#issuecomment-103142214
  171. Sass.setWorkerUrl(SASSJS_RELATIVE_PATH + "/sass.worker.js");
  172. return Sass;
  173. }));