postcss-custom-url

1.1.0 • Public • Published

postcss-custom-url

Transform url() in css

npm

version status coverage dependencies devDependencies

Usage

var url = require('postcss-custom-url')
var custom = url.custom
var util = url.util

var postcss = require('postcss')
var path = require('path')
var fixtures = path.resolve.bind(path, __dirname, 'fixtures')

postcss(url({
  maxSize: 5,
  assetOutFolder: fixtures('build', 'images'),
}))
.process(
  '.a{ background-image: url(images/octocat_setup.png); }',
  { from: fixtures('a.css'), to: fixtures('build', 'css', 'a.css') }
)
.then(function (result) {
  console.log(result.css)
  // '.a{ background-image: url(../images/octocat_setup.png); }'
})

url

The same with custom.bind([ util.inline, util.copy ])

plugin = url.plugin(name, customTransforms)

url.plugin will create a postcss-plugin, with the function custom.bind(customTransforms)

If name not string, it is treated as customTransforms

If customTransforms falsy, [ util.rebase ] is used.

var rebase = url.plugin()
postcss(rebase())

plugin = custom.bind(customTransforms)

plugin

Type: Function

Your postcss-plugin function

customTransforms

Type: Array

An array of transform functions.

The transform function signature: transformFn(result)

result

Type: Object

  • result.url: the url in css. Transforms should modify this field
  • result.file: the asset file path, if result.url is relative
  • result.opts: options mixed from both the plugin and postcss. It always inludes from and to.

transformFn

Type: Function

It could be synchronous, or made asynchronous in the gulp-way:

  • accept a callback
  • return a promise
  • return a stream

NOTE: usually, you should check result.url before you do your transformations.

util

A group of methods.

inline

Transform result.url to data-url.

Options:

maxSize

Type: Number

Default: 10

Specify the maximum file size to inline (in kbytes)

copy

Copy asset files to proper destinations and transform result.url.

Options:

useHash

Type: Boolean

Default: false

If true, assets are renamed by their sha1 hashes

assetOutFolder

Type: String, Function

Specify the destination where assets should be copied

If Function, it receives the asset file path and result.opts, and should return the output folder.

Urls are transformed based on the destination.

Package Sidebar

Install

npm i postcss-custom-url@1.1.0

Version

1.1.0

License

MIT

Last publish

Collaborators

  • zoubin