Gathering detailed insights and metrics for postcss-url
Gathering detailed insights and metrics for postcss-url
Gathering detailed insights and metrics for postcss-url
Gathering detailed insights and metrics for postcss-url
PostCSS plugin to rebase url(), inline or copy asset.
npm install postcss-url
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
377 Stars
255 Commits
58 Forks
8 Watching
10 Branches
46 Contributors
Updated on 05 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.3%
211,736
Compared to previous day
Last week
0.4%
1,073,268
Compared to previous week
Last month
6.7%
4,617,082
Compared to previous month
Last year
-17.9%
61,175,667
Compared to previous year
PostCSS plugin to rebase, inline or copy on url().
1$ npm install postcss postcss-url
1// dependencies 2const fs = require("fs") 3const postcss = require("postcss") 4const url = require("postcss-url") 5 6// css to be processed 7const css = fs.readFileSync("input.css", "utf8") 8 9// process css 10const output = postcss() 11 .use(url({ 12 url: "rebase" 13 })) 14 .process(css, { 15 from: "src/stylesheet/index.css", 16 to: "dist/index.css" 17 })
before:
1.element { 2 background: url('images/sprite.png'); 3}
after:
1.element { 2 /* rebasing path by new destination */ 3 background: url('../src/stylesheet/images/sprite.png'); 4}
1// postcss-url options 2const options = { 3 url: 'inline' 4}; 5 6postcss() 7 .use(url(options)) 8 .process(css, { 9 from: "src/stylesheet/index.css", 10 to: "dist/index.css" 11 })
before:
1.element { 2 background: url('/images/sprite.png'); 3 filter: url('/images/circle.svg'); 4}
after:
1.element { 2 /* inlined png as base64 */ 3 background: url('data:image/png;base64,R0lGODlhAQABAJH/AP///wAAAP///wAAACH/C0FET0JFOklSMS4'); 4 /* inlined svg as encodeURIComponent */ 5 filter: url('data:image/svg+xml,%3Csvg xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%2F%3E'); 6}
1// postcss-url options 2const options = { 3 url: 'copy', 4 // base path to search assets from 5 basePath: path.resolve('node_modules/bootstrap'), 6 // dir to copy assets 7 assetsPath: 'img', 8 // using hash names for assets (generates from asset content) 9 useHash: true 10}; 11 12postcss() 13 .use(url(options)) 14 .process(css, { 15 from: "src/stylesheet/index.css", 16 to: "dist/index.css" 17 })
before:
1.element { 2 background: url('/images/sprite.png'); 3}
after:
1.element { 2 /* copy 'sprite.png' from 'node_modules/bootstrap/images/' to 'dist/img/' */ 3 /* and rename it by hash function */ 4 background: url('img/a2ds3kfu.png'); 5}
process first matched option by default.
multi: true
in custom
will processing with other options
1const options = [ 2 { filter: '**/assets/copy/*.png', url: 'copy', assetsPath: 'img', useHash: true }, 3 { filter: '**/assets/inline/*.svg', url: 'inline' }, 4 { filter: '**/assets/**/*.gif', url: 'rebase' }, 5 // using custom function to build url 6 { filter: 'cdn/**/*', url: (asset) => `https://cdn.url/${asset.url}` } 7]; 8 9postcss().use(url(options))
Checkout tests for examples.
rebase
- default
assetsPath
- directory to copy assets (relative to to
or absolute)inline
basePath
- path or array of paths to search assets (relative to from
, or absolute)encodeType
- base64
, encodeURI
, encodeURIComponent
includeUriFragment
- include the fragment identifer at the end of the URImaxSize
- file size in kbytesfallback
- copy
, rebase
or custom function for files > maxSize
ignoreFragmentWarning
- do not warn when an SVG URL with a fragment is inlinedoptimizeSvgEncode
- reduce size of inlined svg (IE9+, Android 3+)copy
basePath
- path or array of paths to search assets (relative to from
, or absolute)assetsPath
- directory to copy assets (relative to to
or absolute)useHash
- use filehash(xxhash) for naminghashOptions
- options for hash functioncustom {Function}
multi
- processing with other optionsurl
rebase
- (default)Allow you to fix url()
according to postcss to
and/or from
options (rebase to to
first if available, otherwise from
or process.cwd()
).
inline
Allow you to inline assets using base64 encoding. Can use postcss from
option to find ressources.
copy
Allow you to copy and rebase assets according to postcss to
, assetsPath
and from
options (assetsPath
is relative to the option to
).
url: {Function}
Custom transform function. Takes following arguments:
asset
url
- original urlpathname
- url pathname (url without search or hash)absolutePath
- absolute path to assetrelativePath
- current relative path to assetsearch
- search from url
, ex. ?query=1
from ./image.png?query=1
hash
- hash from url
, ex. #spriteLink
from ../asset.svg#spriteLink
dir
from
- postcss option fromto
- postcss option tofile
- decl file pathoptions
- postcss-url matched optionsdecl
- related postcss declaration objectwarn
- wrapped function result.warn
for current decl
result
– postcss result objectAnd should return the transformed url. You can use this option to adjust urls for CDN.
maxSize
Specify the maximum file size to inline (in kbytes)
ignoreFragmentWarning
(default: false
)
Do not warn when an SVG URL with a fragment is inlined. PostCSS-URL does not support partial inlining. The entire SVG file will be inlined. By default a warning will be issued when this occurs.
NOTE: Only files less than the maximum size will be inlined.
filter
A regular expression e.g. /\.svg$/
, a minimatch string e.g. '**/*.svg'
or a custom filter function to determine wether a file should be inlined.
fallback
The url fallback method to use if max size is exceeded or url contains a hash. Custom transform functions are supported.
includeUriFragment
(default: false
)
Specifies whether the URL's fragment identifer value, if present, will be added to the inlined data URI.
basePath
Specify the base path or list of base paths where to search images from
assetsPath
(default: false
)
If you specify an assetsPath
, the assets files will be copied in that
destination
useHash
(default: false
)
If set to true
the copy method is going to rename the path of the files by a hash name
hashOptions
method
(default: xxhash32
)
Hash method xxhash32|xxhash64
or custom function (accept file buffer)
shrink
(default: 8
)
Result hash shrink count
append
(default: false
)
Prepend the original filename in resulting filename
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
1$ git clone https://github.com/postcss/postcss-url.git 2$ git checkout -b patch-1 3$ npm install 4$ npm test
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/22 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
22 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More