bootstrap.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /* global window, document */
  7. import defaultOptions from '../less/default-options';
  8. import addDefaultOptions from './add-default-options';
  9. import root from './index';
  10. const options = defaultOptions();
  11. if (window.less) {
  12. for (const key in window.less) {
  13. if (window.less.hasOwnProperty(key)) {
  14. options[key] = window.less[key];
  15. }
  16. }
  17. }
  18. addDefaultOptions(window, options);
  19. options.plugins = options.plugins || [];
  20. if (window.LESS_PLUGINS) {
  21. options.plugins = options.plugins.concat(window.LESS_PLUGINS);
  22. }
  23. const less = root(window, options);
  24. export default less;
  25. window.less = less;
  26. let css;
  27. let head;
  28. let style;
  29. // Always restore page visibility
  30. function resolveOrReject(data) {
  31. if (data.filename) {
  32. console.warn(data);
  33. }
  34. if (!options.async) {
  35. head.removeChild(style);
  36. }
  37. }
  38. if (options.onReady) {
  39. if (/!watch/.test(window.location.hash)) {
  40. less.watch();
  41. }
  42. // Simulate synchronous stylesheet loading by hiding page rendering
  43. if (!options.async) {
  44. css = 'body { display: none !important }';
  45. head = document.head || document.getElementsByTagName('head')[0];
  46. style = document.createElement('style');
  47. style.type = 'text/css';
  48. if (style.styleSheet) {
  49. style.styleSheet.cssText = css;
  50. } else {
  51. style.appendChild(document.createTextNode(css));
  52. }
  53. head.appendChild(style);
  54. }
  55. less.registerStylesheetsImmediately();
  56. less.pageLoadFinished = less.refresh(less.env === 'development').then(resolveOrReject, resolveOrReject);
  57. }