css-builder.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. define(['require', './normalize'], function(req, normalize) {
  2. var cssAPI = {};
  3. var isWindows = !!process.platform.match(/^win/);
  4. function compress(css) {
  5. if (config.optimizeCss == 'none') {
  6. return css;
  7. }
  8. if (typeof process !== "undefined" && process.versions && !!process.versions.node && require.nodeRequire) {
  9. try {
  10. var csso = require.nodeRequire('csso');
  11. }
  12. catch(e) {
  13. console.log('Compression module not installed. Use "npm install csso -g" to enable.');
  14. return css;
  15. }
  16. var csslen = css.length;
  17. try {
  18. css = csso.justDoIt(css);
  19. }
  20. catch(e) {
  21. console.log('Compression failed due to a CSS syntax error.');
  22. return css;
  23. }
  24. console.log('Compressed CSS output to ' + Math.round(css.length / csslen * 100) + '%.');
  25. return css;
  26. }
  27. console.log('Compression not supported outside of nodejs environments.');
  28. return css;
  29. }
  30. //load file code - stolen from text plugin
  31. function loadFile(path) {
  32. if (typeof process !== "undefined" && process.versions && !!process.versions.node && require.nodeRequire) {
  33. var fs = require.nodeRequire('fs');
  34. var file = fs.readFileSync(path, 'utf8');
  35. if (file.indexOf('\uFEFF') === 0)
  36. return file.substring(1);
  37. return file;
  38. }
  39. else {
  40. var file = new java.io.File(path),
  41. lineSeparator = java.lang.System.getProperty("line.separator"),
  42. input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), 'utf-8')),
  43. stringBuffer, line;
  44. try {
  45. stringBuffer = new java.lang.StringBuffer();
  46. line = input.readLine();
  47. if (line && line.length() && line.charAt(0) === 0xfeff)
  48. line = line.substring(1);
  49. stringBuffer.append(line);
  50. while ((line = input.readLine()) !== null) {
  51. stringBuffer.append(lineSeparator).append(line);
  52. }
  53. return String(stringBuffer.toString());
  54. }
  55. finally {
  56. input.close();
  57. }
  58. }
  59. }
  60. function saveFile(path, data) {
  61. if (typeof process !== "undefined" && process.versions && !!process.versions.node && require.nodeRequire) {
  62. var fs = require.nodeRequire('fs');
  63. fs.writeFileSync(path, data, 'utf8');
  64. }
  65. else {
  66. var content = new java.lang.String(data);
  67. var output = new java.io.BufferedWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(path), 'utf-8'));
  68. try {
  69. output.write(content, 0, content.length());
  70. output.flush();
  71. }
  72. finally {
  73. output.close();
  74. }
  75. }
  76. }
  77. //when adding to the link buffer, paths are normalised to the baseUrl
  78. //when removing from the link buffer, paths are normalised to the output file path
  79. function escape(content) {
  80. return content.replace(/(["'\\])/g, '\\$1')
  81. .replace(/[\f]/g, "\\f")
  82. .replace(/[\b]/g, "\\b")
  83. .replace(/[\n]/g, "\\n")
  84. .replace(/[\t]/g, "\\t")
  85. .replace(/[\r]/g, "\\r");
  86. }
  87. // NB add @media query support for media imports
  88. var importRegEx = /@import\s*(url)?\s*(('([^']*)'|"([^"]*)")|\(('([^']*)'|"([^"]*)"|([^\)]*))\))\s*;?/g;
  89. var absUrlRegEx = /^([^\:\/]+:\/)?\//;
  90. // Write Css module definition
  91. var writeCSSDefinition = "define('@writecss', function() {return function writeCss(c) {var d=document,a='appendChild',i='styleSheet',s=d.createElement('style');s.type='text/css';d.getElementsByTagName('head')[0][a](s);s[i]?s[i].cssText=c:s[a](d.createTextNode(c));};});";
  92. var siteRoot;
  93. var baseParts = req.toUrl('base_url').split('/');
  94. baseParts[baseParts.length - 1] = '';
  95. var baseUrl = baseParts.join('/');
  96. var curModule = 0;
  97. var config;
  98. var writeCSSForLayer = true;
  99. var layerBuffer = [];
  100. var cssBuffer = {};
  101. cssAPI.load = function(name, req, load, _config) {
  102. //store config
  103. config = config || _config;
  104. if (!siteRoot) {
  105. siteRoot = path.resolve(config.dir || path.dirname(config.out), config.siteRoot || '.') + '/';
  106. if (isWindows)
  107. siteRoot = siteRoot.replace(/\\/g, '/');
  108. }
  109. //external URLS don't get added (just like JS requires)
  110. if (name.match(absUrlRegEx))
  111. return load();
  112. var fileUrl = req.toUrl(name + '.css');
  113. if (isWindows)
  114. fileUrl = fileUrl.replace(/\\/g, '/');
  115. // rebase to the output directory if based on the source directory;
  116. // baseUrl points always to the output directory, fileUrl only if
  117. // it is not prefixed by a computed path (relative too)
  118. var fileSiteUrl = fileUrl;
  119. if (fileSiteUrl.indexOf(baseUrl) < 0) {
  120. var appRoot = req.toUrl(config.appDir);
  121. if (isWindows)
  122. appRoot = appRoot.replace(/\\/g, '/');
  123. if (fileSiteUrl.indexOf(appRoot) == 0)
  124. fileSiteUrl = siteRoot + fileSiteUrl.substring(appRoot.length);
  125. }
  126. //add to the buffer
  127. cssBuffer[name] = normalize(loadFile(fileUrl), fileSiteUrl, siteRoot);
  128. load();
  129. }
  130. cssAPI.normalize = function(name, normalize) {
  131. if (name.substr(name.length - 4, 4) == '.css')
  132. name = name.substr(0, name.length - 4);
  133. return normalize(name);
  134. }
  135. cssAPI.write = function(pluginName, moduleName, write, parse) {
  136. var cssModule;
  137. //external URLS don't get added (just like JS requires)
  138. if (moduleName.match(absUrlRegEx))
  139. return;
  140. layerBuffer.push(cssBuffer[moduleName]);
  141. if (!global._requirejsCssData) {
  142. global._requirejsCssData = {
  143. usedBy: {css: true},
  144. css: ''
  145. }
  146. } else {
  147. global._requirejsCssData.usedBy.css = true;
  148. }
  149. if (config.buildCSS != false) {
  150. var style = cssBuffer[moduleName];
  151. if (config.writeCSSModule && style) {
  152. if (writeCSSForLayer) {
  153. writeCSSForLayer = false;
  154. write(writeCSSDefinition);
  155. }
  156. cssModule = 'define(["@writecss"], function(writeCss){\n writeCss("'+ escape(compress(style)) +'");\n})';
  157. }
  158. else {
  159. cssModule = 'define(function(){})';
  160. }
  161. write.asModule(pluginName + '!' + moduleName, cssModule);
  162. }
  163. }
  164. cssAPI.onLayerEnd = function(write, data) {
  165. if (config.separateCSS && config.IESelectorLimit)
  166. throw 'RequireCSS: separateCSS option is not compatible with ensuring the IE selector limit';
  167. if (config.separateCSS) {
  168. var outPath = data.path.replace(/(\.js)?$/, '.css');
  169. console.log('Writing CSS! file: ' + outPath + '\n');
  170. var css = layerBuffer.join('');
  171. process.nextTick(function() {
  172. if (global._requirejsCssData) {
  173. css = global._requirejsCssData.css = css + global._requirejsCssData.css;
  174. delete global._requirejsCssData.usedBy.css;
  175. if (Object.keys(global._requirejsCssData.usedBy).length === 0) {
  176. delete global._requirejsCssData;
  177. }
  178. }
  179. saveFile(outPath, compress(css));
  180. });
  181. }
  182. else if (config.buildCSS != false && config.writeCSSModule != true) {
  183. var styles = config.IESelectorLimit ? layerBuffer : [layerBuffer.join('')];
  184. for (var i = 0; i < styles.length; i++) {
  185. if (styles[i] == '')
  186. return;
  187. write(
  188. "(function(c){var d=document,a='appendChild',i='styleSheet',s=d.createElement('style');s.type='text/css';d.getElementsByTagName('head')[0][a](s);s[i]?s[i].cssText=c:s[a](d.createTextNode(c));})\n"
  189. + "('" + escape(compress(styles[i])) + "');\n"
  190. );
  191. }
  192. }
  193. //clear layer buffer for next layer
  194. layerBuffer = [];
  195. writeCSSForLayer = true;
  196. }
  197. return cssAPI;
  198. });