css-base.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. // css base code, injected by the css-loader
  6. module.exports = function(useSourceMap) {
  7. var list = [];
  8. // return the list of modules as css string
  9. list.toString = function toString() {
  10. return this.map(function (item) {
  11. var content = cssWithMappingToString(item, useSourceMap);
  12. if(item[2]) {
  13. return "@media " + item[2] + "{" + content + "}";
  14. } else {
  15. return content;
  16. }
  17. }).join("");
  18. };
  19. // import a list of modules into the list
  20. list.i = function(modules, mediaQuery) {
  21. if(typeof modules === "string")
  22. modules = [[null, modules, ""]];
  23. var alreadyImportedModules = {};
  24. for(var i = 0; i < this.length; i++) {
  25. var id = this[i][0];
  26. if(typeof id === "number")
  27. alreadyImportedModules[id] = true;
  28. }
  29. for(i = 0; i < modules.length; i++) {
  30. var item = modules[i];
  31. // skip already imported module
  32. // this implementation is not 100% perfect for weird media query combinations
  33. // when a module is imported multiple times with different media queries.
  34. // I hope this will never occur (Hey this way we have smaller bundles)
  35. if(typeof item[0] !== "number" || !alreadyImportedModules[item[0]]) {
  36. if(mediaQuery && !item[2]) {
  37. item[2] = mediaQuery;
  38. } else if(mediaQuery) {
  39. item[2] = "(" + item[2] + ") and (" + mediaQuery + ")";
  40. }
  41. list.push(item);
  42. }
  43. }
  44. };
  45. return list;
  46. };
  47. function cssWithMappingToString(item, useSourceMap) {
  48. var content = item[1] || '';
  49. var cssMapping = item[3];
  50. if (!cssMapping) {
  51. return content;
  52. }
  53. if (useSourceMap && typeof btoa === 'function') {
  54. var sourceMapping = toComment(cssMapping);
  55. var sourceURLs = cssMapping.sources.map(function (source) {
  56. return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'
  57. });
  58. return [content].concat(sourceURLs).concat([sourceMapping]).join('\n');
  59. }
  60. return [content].join('\n');
  61. }
  62. // Adapted from convert-source-map (MIT)
  63. function toComment(sourceMap) {
  64. // eslint-disable-next-line no-undef
  65. var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
  66. var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
  67. return '/*# ' + data + ' */';
  68. }