bootstrap.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // TODO - consider switching this out for a recommendation for this polyfill?
  8. // <script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
  9. // Browsers have good Promise support
  10. require('promise/polyfill');
  11. var options = require('../less/default-options')();
  12. if (window.less) {
  13. for (key in window.less) {
  14. if (window.less.hasOwnProperty(key)) {
  15. options[key] = window.less[key];
  16. }
  17. }
  18. }
  19. require('./add-default-options')(window, options);
  20. options.plugins = options.plugins || [];
  21. if (window.LESS_PLUGINS) {
  22. options.plugins = options.plugins.concat(window.LESS_PLUGINS);
  23. }
  24. var less = module.exports = require('./index')(window, options);
  25. window.less = less;
  26. var css, head, style;
  27. // Always restore page visibility
  28. function resolveOrReject(data) {
  29. if (data.filename) {
  30. console.warn(data);
  31. }
  32. if (!options.async) {
  33. head.removeChild(style);
  34. }
  35. }
  36. if (options.onReady) {
  37. if (/!watch/.test(window.location.hash)) {
  38. less.watch();
  39. }
  40. // Simulate synchronous stylesheet loading by hiding page rendering
  41. if (!options.async) {
  42. css = 'body { display: none !important }';
  43. head = document.head || document.getElementsByTagName('head')[0];
  44. style = document.createElement('style');
  45. style.type = 'text/css';
  46. if (style.styleSheet) {
  47. style.styleSheet.cssText = css;
  48. } else {
  49. style.appendChild(document.createTextNode(css));
  50. }
  51. head.appendChild(style);
  52. }
  53. less.registerStylesheetsImmediately();
  54. less.pageLoadFinished = less.refresh(less.env === 'development').then(resolveOrReject, resolveOrReject);
  55. }