Blame view

node_modules/stylus/lib/functions/resolver.js 2.11 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  /**
   * Module dependencies.
   */
  
  var Compiler = require('../visitor/compiler')
    , nodes = require('../nodes')
    , parse = require('url').parse
    , relative = require('path').relative
    , join = require('path').join
    , dirname = require('path').dirname
    , extname = require('path').extname
    , sep = require('path').sep;
  
  /**
   * Return a url() function which resolves urls.
   *
   * Options:
   *
   *    - `paths` resolution path(s), merged with general lookup paths
   *    - `nocheck` don't check file existence
   *
   * Examples:
   *
   *    stylus(str)
   *      .set('filename', __dirname + '/css/test.styl')
   *      .define('url', stylus.resolver({ nocheck: true }))
   *      .render(function(err, css){ ... })
   *
   * @param {Object} [options]
   * @return {Function}
   * @api public
   */
  
  module.exports = function(options) {
    options = options || {};
  
    function resolver(url) {
      // Compile the url
      var compiler = new Compiler(url)
        , filename = url.filename;
      compiler.isURL = true;
      url = parse(url.nodes.map(function(node){
        return compiler.visit(node);
      }).join(''));
  
      // Parse literal 
      var literal = new nodes.Literal('url("' + url.href + '")')
        , path = url.pathname
        , dest = this.options.dest
        , tail = ''
        , res;
  
      // Absolute or hash
      if (url.protocol || !path || '/' == path[0]) return literal;
  
      // Check that file exists
      if (!options.nocheck) {
        var _paths = options.paths || [];
        path = require('../utils').lookup(path, _paths.concat(this.paths));
        if (!path) return literal;
      }
  
      if (this.includeCSS && extname(path) == '.css')
        return new nodes.Literal(url.href);
  
      if (url.search) tail += url.search;
      if (url.hash) tail += url.hash;
  
      if (dest && extname(dest) == '.css')
        dest = dirname(dest);
  
      res = relative(dest || dirname(this.filename), options.nocheck
        ? join(dirname(filename), path)
        : path) + tail;
  
      if ('\\' == sep) res = res.replace(/\\/g, '/');
  
      return new nodes.Literal('url("' + res + '")');
    };
  
    // Expose options to Evaluator
    resolver.options = options;
    resolver.raw = true;
    return resolver;
  };