index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //modified https://github.com/grieve/webpack-glsl-loader/blob/master/index.js
  2. 'use strict';
  3. var fs = require('fs');
  4. var path = require('path');
  5. function parse(loader, source, context, cb) {
  6. var imports = [];
  7. var importPattern = /@import ([.\/\w_-]+);/gi;
  8. var match = importPattern.exec(source);
  9. while (match != null) {
  10. imports.push({
  11. key: match[1],
  12. target: match[0],
  13. content: ''
  14. });
  15. match = importPattern.exec(source);
  16. }
  17. source = uncomment(source);
  18. processImports(loader, source, context, imports, cb);
  19. }
  20. var singleComment = 1;
  21. var multiComment = 2;
  22. function uncomment(str) {
  23. var currentChar;
  24. var nextChar;
  25. var insideString = false;
  26. var insideComment = 0;
  27. var offset = 0;
  28. var ret = '';
  29. str = str.replace(/\r\n/g, '\n');
  30. str = str.replace(/[ \f\t\v]+/g, ' ');
  31. str = str.replace(/^\s*\n/gm, '');
  32. str = str.replace(/ \+ /g, '+');
  33. str = str.replace(/ \- /g, '-');
  34. str = str.replace(/ \/ /g, '/');
  35. str = str.replace(/ \* /g, '*');
  36. str = str.replace(/ > /g, '>');
  37. str = str.replace(/ < /g, '<');
  38. str = str.replace(/ >= /g, '>=');
  39. str = str.replace(/ <= /g, '<=');
  40. str = str.replace(/ \+= /g, '+=');
  41. str = str.replace(/ \-= /g, '-=');
  42. str = str.replace(/ \/= /g, '/=');
  43. str = str.replace(/ \*= /g, '*=');
  44. str = str.replace(/ = /g, '=');
  45. str = str.replace(/, /g, ',');
  46. str = str.replace(/\n\n/g, '\n');
  47. str = str.replace(/\n /g, '\n');
  48. for (var i = 0; i < str.length; i++) {
  49. currentChar = str[i];
  50. nextChar = str[i + 1];
  51. if (!insideComment && currentChar === '"') {
  52. var escaped = str[i - 1] === '\\' && str[i - 2] !== '\\';
  53. if (!escaped) {
  54. insideString = !insideString;
  55. }
  56. }
  57. if (insideString) {
  58. continue;
  59. }
  60. if (!insideComment && currentChar + nextChar === '//') {
  61. ret += str.slice(offset, i);
  62. offset = i;
  63. insideComment = singleComment;
  64. i++;
  65. } else if (insideComment === singleComment && currentChar === '\n') {
  66. insideComment = 0;
  67. offset = i;
  68. } else if (!insideComment && currentChar + nextChar === '/*') {
  69. ret += str.slice(offset, i);
  70. offset = i;
  71. insideComment = multiComment;
  72. i++;
  73. continue;
  74. } else if (insideComment === multiComment && currentChar + nextChar === '*/') {
  75. i++;
  76. insideComment = 0;
  77. offset = i + 1;
  78. continue;
  79. }
  80. }
  81. return ret + (insideComment ? '' : str.substr(offset));
  82. }
  83. function processImports(loader, source, context, imports, cb) {
  84. if (imports.length === 0) {
  85. return cb(null, source);
  86. }
  87. var imp = imports.pop();
  88. loader.resolve(context, imp.key + '.fx', function (err, resolved) {
  89. if (err) {
  90. return cb(err);
  91. }
  92. loader.addDependency(resolved);
  93. fs.readFile(resolved, 'utf-8', function (err, src) {
  94. if (err) {
  95. return cb(err);
  96. }
  97. parse(loader, src, path.dirname(resolved), function (err, bld) {
  98. if (err) {
  99. return cb(err);
  100. }
  101. source = source.replace(imp.target, bld);
  102. processImports(loader, source, context, imports, cb);
  103. });
  104. });
  105. });
  106. }
  107. module.exports = function (source) {
  108. this.cacheable();
  109. var cb = this.async();
  110. parse(this, source, this.context, function (err, bld) {
  111. if (err) {
  112. return cb(err);
  113. }
  114. cb(null, 'module.exports = ' + JSON.stringify(bld));
  115. });
  116. };