forked from postcss/postcss-url
/
index.js
57 lines (45 loc) · 1.46 KB
/
index.js
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
'use strict';
const path = require('path');
const declProcessor = require('./lib/decl-processor').declProcessor;
const plugin = (options) => {
options = options || {};
return {
postcssPlugin: 'postcss-url',
Once(styles, { result }) {
const promises = [];
const opts = result.opts;
const from = opts.from ? path.dirname(opts.from) : '.';
const to = opts.to ? path.dirname(opts.to) : from;
styles.walkDecls((decl) =>
promises.push(declProcessor(from, to, options, result, decl))
);
return Promise.all(promises);
}
};
};
plugin.postcss = true;
module.exports = plugin;
/**
* @callback PostcssUrl~UrlProcessor
* @param {String} from from
* @param {String} dirname to dirname
* @param {String} oldUrl url
* @param {String} to destination
* @param {Object} options plugin options
* @param {Object} decl postcss declaration
* @return {String|undefined} new url or undefined if url is old
*/
/**
* @typedef {Object} PostcssUrl~HashOptions
* @property {Function|String} [method=^xxhash32|xxhash64] - hash name or custom function, accepting file content
* @see https://github.com/pierrec/js-xxhash
* @property {Number} [shrink=8] - shrink hash string
*/
/**
* @typedef {Object} Decl - postcss decl
* @see http://api.postcss.org/Declaration.html
*/
/**
* @typedef {Object} Result - postcss result
* @see http://api.postcss.org/Result.html
*/