bootstrap.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Kicks off less and compiles any stylesheets
  3. * used in the browser distributed version of less
  4. * to kick-start less using the browser api
  5. */
  6. import defaultOptions from '../less/default-options.js';
  7. import addDefaultOptions from './add-default-options.js';
  8. import root from './index.js';
  9. const options = defaultOptions();
  10. if (window.less) {
  11. for (const key in window.less) {
  12. if (Object.prototype.hasOwnProperty.call(window.less, key)) {
  13. options[key] = window.less[key];
  14. }
  15. }
  16. }
  17. addDefaultOptions(window, options);
  18. options.plugins = options.plugins || [];
  19. if (window.LESS_PLUGINS) {
  20. options.plugins = options.plugins.concat(window.LESS_PLUGINS);
  21. }
  22. const less = root(window, options);
  23. export default less;
  24. window.less = less;
  25. let css;
  26. let head;
  27. let style;
  28. // Always restore page visibility
  29. function resolveOrReject(data) {
  30. if (data.filename) {
  31. console.warn(data);
  32. }
  33. if (!options.async) {
  34. head.removeChild(style);
  35. }
  36. }
  37. if (options.onReady) {
  38. if (/!watch/.test(window.location.hash)) {
  39. less.watch();
  40. }
  41. // Simulate synchronous stylesheet loading by hiding page rendering
  42. if (!options.async) {
  43. css = 'body { display: none !important }';
  44. head = document.head || document.getElementsByTagName('head')[0];
  45. style = document.createElement('style');
  46. style.type = 'text/css';
  47. if (style.styleSheet) {
  48. style.styleSheet.cssText = css;
  49. } else {
  50. style.appendChild(document.createTextNode(css));
  51. }
  52. head.appendChild(style);
  53. }
  54. less.registerStylesheetsImmediately();
  55. less.pageLoadFinished = less.refresh(less.env === 'development').then(resolveOrReject, resolveOrReject);
  56. }