Gathering detailed insights and metrics for postcss-copy
Gathering detailed insights and metrics for postcss-copy
Gathering detailed insights and metrics for postcss-copy
Gathering detailed insights and metrics for postcss-copy
postcss-copy-assets
PostCSS plugin to copy assets referenced by relative url()s into a build directory.
postcss-copy-class
PostCSS plugin to apply classes as mixins
@flatjs/forge-plugin-postcss-assets
A plugin for PostCSS that used to base64, copy assets on url()
postcss-px-to-viewport-copy
A CSS post-processor that converts px to viewport units (vw, vh, vmin, vmax).
An async postcss plugin to copy all assets referenced in CSS files to a custom destination folder and updating the URLs.
npm install postcss-copy
Typescript
Module System
Node Version
NPM Version
70.4
Supply Chain
87.7
Quality
72.2
Maintenance
50
Vulnerability
99.6
License
JavaScript (96.53%)
CSS (3.47%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
38 Stars
244 Commits
4 Forks
3 Watchers
5 Branches
7 Contributors
Updated on Jul 18, 2022
Latest Version
7.1.0
Package Id
postcss-copy@7.1.0
Size
9.69 kB
NPM Version
5.0.0
Node Version
8.0.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
An async postcss plugin to copy all assets referenced in CSS files to a custom destination folder and updating the URLs.
Sections |
---|
Install |
Quick Start |
Options |
Custom Hash Function |
Transform |
Using postcss-import |
About lifecyle and the fileMeta object |
Roadmap |
Credits |
With npm do:
$ npm install postcss-copy
1// postcss.config.js 2module.exports = { 3 plugins: [ 4 require('postcss-copy')({ 5 dest: 'dist' 6 }) 7 ] 8};
1$ postcss src/index.css
1var gulp = require('gulp'); 2var postcss = require('gulp-postcss'); 3var postcssCopy = require('postcss-copy'); 4 5gulp.task('buildCss', function () { 6 var processors = [ 7 postcssCopy({ 8 basePath: ['src', 'otherSrc'] 9 dest: 'dist' 10 }) 11 ]; 12 13 return gulp 14 .src(['src/**/*.css', 'otherSrc/**/*.css']) 15 .pipe(postcss(processors)) 16 .pipe(gulp.dest('dist')); 17});
Define one/many base path for your CSS files.
Define the dest path of your CSS files and assets.
Define a template name for your final url assets.
?
.#
.1var copyOpts = { 2 ..., 3 template(fileMeta) { 4 return 'assets/custom-name-' + fileMeta.name + '.' + fileMeta.ext; 5 } 6}
Flag option to notify to postcss-copy that your CSS files destination are going to preserve the directory structure.
It's helpful if you are using postcss-cli
with the --base option or gulp-postcss
with multiple files (e.g: gulp.src('src/**/*.css')
)
Option to ignore assets in your CSS file.
!
key in your CSS:1.btn { 2 background-image: url('!images/button.jpg'); 3} 4.background { 5 background-image: url('!images/background.jpg'); 6}
1// ignore with string 2var copyOpts = { 3 ..., 4 ignore: 'images/*.jpg' 5} 6// ignore with array 7var copyOpts = { 8 ..., 9 ignore: ['images/button.+(jpg|png)', 'images/background.jpg'] 10}
1// ignore function
2var copyOpts = {
3 ...,
4 ignore(fileMeta, opts) {
5 return (fileMeta.filename.indexOf('images/button.jpg') ||
6 fileMeta.filename.indexOf('images/background.jpg'));
7 }
8}
Define a custom function to create the hash name.
1var copyOpts = { 2 ..., 3 hashFunction(contents) { 4 // borschik 5 return crypto.createHash('sha1') 6 .update(contents) 7 .digest('base64') 8 .replace(/\+/g, '-') 9 .replace(/\//g, '_') 10 .replace(/=/g, '') 11 .replace(/^[+-]+/g, ''); 12 } 13};
Extend the copy method to apply a transform in the contents (e.g: optimize images).
IMPORTANT: The function must return the fileMeta (modified) or a promise using resolve(fileMeta)
.
1var Imagemin = require('imagemin'); 2var imageminJpegtran = require('imagemin-jpegtran'); 3var imageminPngquant = require('imagemin-pngquant'); 4 5var copyOpts = { 6 ..., 7 transform(fileMeta) { 8 if (['jpg', 'png'].indexOf(fileMeta.ext) === -1) { 9 return fileMeta; 10 } 11 return Imagemin.buffer(fileMeta.contents, { 12 plugins: [ 13 imageminPngquant(), 14 imageminJpegtran({ 15 progressive: true 16 }) 17 ] 18 }) 19 .then(result => { 20 fileMeta.contents = result; 21 return fileMeta; // <- important 22 }); 23 } 24};
postcss-import is a great plugin that allow us work our css files in a modular way with the same behavior of CommonJS.
One thing more...
postcss-import has the ability of load files from node_modules. If you are using a custom basePath
and you want to
track your assets from node_modules
you need to add the node_modules
folder in the basePath
option:
myProject/
|-- node_modules/
|-- dest/
|-- src/
1var gulp = require('gulp'); 2var postcss = require('gulp-postcss'); 3var postcssCopy = require('postcss-copy'); 4var postcssImport = require('postcss-import'); 5var path = require('path'); 6 7gulp.task('buildCss', function () { 8 var processors = [ 9 postcssImport(), 10 postcssCopy({ 11 basePath: ['src', 'node_modules'], 12 preservePath: true, 13 dest: 'dist' 14 }) 15 ]; 16 17 return gulp 18 .src('src/**/*.css') 19 .pipe(postcss(processors, {to: 'dist/css/index.css'})) 20 .pipe(gulp.dest('dist/css')); 21});
The fileMeta is a literal object with meta information about the copy process. Its information grows with the progress of the copy process.
The lifecyle of the copy process is:
Detect the url in the CSS files
Validate url
Initialize the fileMeta:
1{ 2 sourceInputFile, // path to the origin CSS file 3 sourceValue, // origin asset value taked from the CSS file 4 filename, // filename normalized without query string 5 absolutePath, // absolute path of the asset file 6 fullName, // name of the asset file 7 path, // relative path of the asset file 8 name, // name without extension 9 ext, // extension name 10 query, // full query string 11 qparams, // query string params without the char '?' 12 qhash, // query string hash without the char '#' 13 basePath // basePath found 14}
Check ignore function
Read the asset file (using a cache buffer if exists)
Add content
property in the fileMeta object
Execute custom transform
Create hash name based on the custom transform
Add hash
property in the fileMeta object
Define template for the new asset
Add resultAbsolutePath
and extra
properties in the fileMeta object
Write in destination
Write the new URL in the PostCSS node value.
nothing for now :)
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/29 approved changesets -- 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
Score
Last Scanned on 2025-07-07
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