Skip to content

Commit de83e06

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 960383b + ccf977a commit de83e06

File tree

7 files changed

+108
-102
lines changed

7 files changed

+108
-102
lines changed

README.md

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -66,55 +66,27 @@ Type: `Result`
6666

6767
## Result
6868

69-
### url
69+
Properties and methods:
7070

71-
Type: `String`
72-
73-
### from
74-
75-
The CSS source path
76-
77-
### to
78-
79-
The CSS destination path
80-
81-
### file
82-
83-
The asset source path
84-
85-
### asset
86-
87-
Type: `Asset`
88-
89-
### isRelative()
90-
91-
### Result.getFile(url, opts)
92-
93-
### Result.dataUrl(file)
71+
* `url`: `String`
72+
* `from`: The CSS source path
73+
* `to`: The CSS destination path
74+
* `file`: The asset source path
75+
* `asset`: `Asset`.
76+
* `isRelative()`: `Function`
77+
* `Result.getFile(url, opts)`. Return the `file` property.
78+
* `Result.dataUrl(file)`
9479

9580
## Asset
9681

97-
### mimeType
98-
99-
### size()
82+
Properties and methods:
10083

101-
Return a promise to get the size of the asset
102-
103-
### data()
104-
105-
Return a promise to get the contents of the asset
106-
107-
### shasum()
108-
109-
Return a promise to get the sha1 of the contents of the asset
110-
111-
### base64()
112-
113-
Return a promise to get the base64 of the contents of the asset
114-
115-
### dataUrl()
116-
117-
Return a promise to get the data url of the asset
84+
* `mimeType`
85+
* `size()`: Return a promise to get the size of the asset
86+
* `data()`: Return a promise to get the contents of the asset
87+
* `shasum()`: Return a promise to get the sha1 of the contents of the asset
88+
* `base64()`: Return a promise to get the base64 of the contents of the asset
89+
* `dataUrl()`: Return a promise to get the data url of the asset
11890

11991
## util
12092

@@ -124,57 +96,61 @@ A group of transform methods.
12496

12597
Transform `result.url` according to `result.to`
12698

127-
12899
### inline
129100

130101
Transform `result.url` to data-url.
131102

132103
Options:
133104

134-
#### maxSize
135-
136-
Type: `Number`
137-
138-
Default: `10`
139-
140-
Specify the maximum file size to inline (in kbytes)
105+
* `maxSize`: `Number`, default: `10`. Specify the maximum file size to inline (in kbytes)
141106

142107
### copy
143108

144109
Copy asset files to proper destinations and transform `result.url`.
145110

146111
Options:
147112

148-
#### useHash
113+
* `useHash`: `Boolean`, default: `false`. If `true`, assets are renamed by their sha1 hashes.
114+
* `name`: `String`, default: `null`. Specify how to rename the asset. It only works when `useHash` is false. Special patterns:
115+
* `[name]`: replaced with the basename of the asset, without the extension.
116+
* `[hash]`: replaced with the hash
117+
* `assetOutFolder`: `String`, `Function`. Specify the destination where assets should be copied.
118+
If `Function`, it receives the asset file path and `result.opts`, and should return the output folder.
119+
Urls are transformed based on the destination, unless `baseUrl` is specified.
120+
* `baseUrl`: `String`, default: `null`. Specify the base url for assets.
149121

150-
Type: `Boolean`
151-
152-
Default: `false`
153-
154-
If `true`, assets are renamed by their sha1 hashes
155-
156-
#### name
157-
Specify how to rename the asset.
158-
159-
It only works when `useHash` is false.
160-
161-
Type: `String`
162-
163-
Special patterns:
164-
165-
* `[name]`: replaced with the basename of the asset, without the extension.
166-
* `[hash]`: replaced with the hash
167-
168-
For example, you can specify `[name].[hash]` to keep the original asset name while using its hash.
169-
170-
#### assetOutFolder
171-
172-
Type: `String`, `Function`
173-
174-
Specify the destination where assets should be copied
122+
```js
123+
var url = require('postcss-custom-url')
124+
var postcss = require('postcss')
175125

176-
If `Function`, it receives the asset file path and `result.opts`,
177-
and should return the output folder.
126+
postcss(url([
127+
[
128+
'copy',
129+
{
130+
// copy images to /path/to/build/images
131+
// since the final css file is /path/to/build/css/style.css,
132+
// url is set to ../images/octocat_setup.84f6371.png
133+
assetOutFolder: '/path/to/build/images',
134+
135+
// rename images like octocat_setup.84f6371.png
136+
name: '[name].[hash]',
137+
138+
// if set to true, all images are named after `[hash]`
139+
// useHash: true,
140+
141+
// if specified, reset url to i/octocat_setup.84f6371.png
142+
// baseUrl: 'i',
143+
},
144+
],
145+
]))
146+
.process(
147+
'.a{ background-image: url(images/octocat_setup.png); }',
148+
{ from: '/path/to/src/style.css', to: '/path/to/build/css/style.css' }
149+
)
150+
.then(function (result) {
151+
console.log(result.css)
152+
// '.a{ background-image: url(../images/octocat_setup.png); }'
153+
})
178154

179-
Urls are transformed based on the destination.
155+
```
180156

changelog.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
<!-- 5d24916 1460952667000 -->
1+
<!-- 4cfed5e 1460974453000 -->
2+
3+
## [v4.2.0](https://github.com/reducejs/postcss-custom-url/commit/4cfed5e) (2016-04-18)
4+
5+
* [[`22b2b06`](https://github.com/reducejs/postcss-custom-url/commit/22b2b06)] readme
6+
7+
* [[`2e6101f`](https://github.com/reducejs/postcss-custom-url/commit/2e6101f)] copy, support specifying baseUrl
8+
9+
* [[`0add06b`](https://github.com/reducejs/postcss-custom-url/commit/0add06b)] changelog
210

311
## [v4.1.0](https://github.com/reducejs/postcss-custom-url/commit/5d24916) (2016-04-18)
412

lib/result.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ class Result {
4646
}
4747
})
4848
return trs.reduce(function (p, tr) {
49-
return p.then(function () {
49+
return p.then(function (next) {
50+
if (next === false) {
51+
return false
52+
}
5053
if (typeof tr[0] === 'string') {
5154
tr[0] = util[tr[0]]
5255
}
53-
let r = tr[0].apply(null, tr.slice(1))
54-
if (r && typeof r.then === 'function') {
55-
return r
56-
}
57-
return Promise.resolve()
56+
return tr[0].apply(null, tr.slice(1))
5857
})
5958
}, Promise.resolve())
6059
}

lib/util.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ const promisify = require('node-promisify')
77
const fsWrite = promisify(fs.writeFile)
88
const mkdirp = promisify(require('mkdirp'))
99

10-
exports.rebase = function (result) {
10+
function rebase(result) {
1111
if (result.isRelative()) {
1212
result.url = path.relative(
1313
path.dirname(result.to), result.file
1414
)
1515
}
1616
}
1717

18-
exports.inline = function (result, opts) {
18+
function inline(result, opts) {
1919
opts = opts || {}
2020
let asset = result.asset
2121
if (!result.isRelative() || !asset.mimeType) {
@@ -36,7 +36,7 @@ exports.inline = function (result, opts) {
3636
})
3737
}
3838

39-
exports.copy = function (result, opts) {
39+
function copy(result, opts) {
4040
if (!result.isRelative()) return
4141

4242
opts = opts || {}
@@ -71,12 +71,22 @@ exports.copy = function (result, opts) {
7171
})
7272
.then(function (basename) {
7373
let assetFile = path.join(assetFolder, basename)
74-
result.url = path.relative(
75-
path.dirname(result.to), assetFile
76-
)
74+
if (opts.baseUrl) {
75+
result.url = path.join(opts.baseUrl, basename)
76+
} else {
77+
result.url = path.relative(
78+
path.dirname(result.to), assetFile
79+
)
80+
}
7781
return result.asset.data().then(function (buf) {
7882
return fsWrite(assetFile, buf)
7983
})
8084
})
8185
}
8286

87+
module.exports = {
88+
rebase: rebase,
89+
inline: inline,
90+
copy: copy,
91+
}
92+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-custom-url",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"description": "Transform `url()` in css",
55
"main": "index.js",
66
"scripts": {

test/custom.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ const fixtures = path.resolve.bind(path, __dirname, 'fixtures')
1313
test('custom', function(t) {
1414
let url = custom([
1515
[ util.inline, { maxSize: 10 } ],
16-
[function (result, base) {
17-
if (result.url.slice(0, 5) === 'data:') return
18-
19-
result.url = path.join(base, path.basename(result.url))
20-
}, 'i'],
21-
'copy',
16+
['copy', { assetOutFolder: fixtures('build', 'css', 'assets'), baseUrl: 'i' }],
2217
])
2318
let body = '.a{ background-image: url(images/octocat_setup.png) url(images/octocat_fork.png); }'
2419
let expectedBody = [ '.a{background-image:url(', ')url(i/octocat_fork.png);}' ]
@@ -38,7 +33,7 @@ test('custom', function(t) {
3833
.then(function (result) {
3934
t.equal(result.css.replace(/\s+/g, ''), expectedBody, 'should inline and transform url')
4035
return Promise.all([
41-
Result.dataUrl(fixtures('build', 'css/i', 'octocat_fork.png')),
36+
Result.dataUrl(fixtures('build', 'css/assets', 'octocat_fork.png')),
4237
Result.dataUrl(fixtures('images', 'octocat_fork.png')),
4338
])
4439
})

test/result.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const Result = require('../lib/result')
5+
6+
test('return false', function (t) {
7+
var result = new Result('i/a.png', { from: '/path/to/style.css' })
8+
return result.transform([
9+
r => { r.url = 'x/a.png' },
10+
r => {
11+
r.url = 'y/a.png'
12+
return false
13+
},
14+
r => { r.url = 'z/a.png' },
15+
])
16+
.then(() => t.equal(result.url, 'y/a.png'))
17+
})
18+

0 commit comments

Comments
 (0)