resolver.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // This implementation of the url resolver adds awareness for webpack loaders
  2. /*
  3. Modified from the stylus implementation:
  4. (The MIT License)
  5. Copyright (c) 2010–2014 LearnBoost <dev@learnboost.com>
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. 'Software'), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /*jshint laxcomma:true */
  24. var Stylus = require('stylus')
  25. , Compiler = Stylus.Compiler
  26. , nodes = Stylus.nodes
  27. , utils = Stylus.utils
  28. , parse = require('url').parse
  29. , relative = require('path').relative
  30. , dirname = require('path').dirname
  31. , extname = require('path').extname
  32. , sep = require('path').sep;
  33. /**
  34. * Return a url() function with the given `options`.
  35. *
  36. * Options:
  37. *
  38. * - `paths` resolution path(s), merged with general lookup paths
  39. *
  40. * Examples:
  41. *
  42. * stylus(str)
  43. * .set('filename', __dirname + '/css/test.styl')
  44. * .define('url', stylus.resolver({ paths: [__dirname + '/public'] }))
  45. * .render(function(err, css){ ... })
  46. *
  47. * @param {Object} options
  48. * @return {Function}
  49. * @api public
  50. */
  51. module.exports = function(options) {
  52. options = options || {};
  53. var _paths = options.paths || [];
  54. function url(url) {
  55. var paths = _paths.concat(this.paths),
  56. filename = this.filename;
  57. // Compile the url
  58. var compiler = new Compiler(url);
  59. compiler.isURL = true;
  60. url = url.nodes.map(function(node){
  61. return compiler.visit(node);
  62. }).join('');
  63. function resolveComponent(url) {
  64. if (!url) {
  65. return url;
  66. }
  67. url = parse(url);
  68. if (url.protocol) {
  69. return url.href;
  70. }
  71. // Lookup
  72. var found = utils.lookup(url.pathname, paths, '', true);
  73. if (!found) {
  74. return url.href;
  75. }
  76. var tail = '';
  77. if (url.search) {
  78. tail += url.search;
  79. }
  80. if (url.hash) {
  81. tail += url.hash;
  82. }
  83. var res = relative(dirname(filename), found) + tail;
  84. if ('\\' == sep) res = res.replace(/\\/g, '/');
  85. return res;
  86. }
  87. var components = url.split(/!/g);
  88. components = components.map(resolveComponent);
  89. return new nodes.Literal('url("' + components.join('!') + '")');
  90. }
  91. url.raw = true;
  92. return url;
  93. };