Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
postcss-copy
Advanced tools
A postcss plugin to copy all assets referenced in CSS to a custom destination folder and updating the URLs.
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
// postcss.config.js
module.exports = {
plugins: [
require('postcss-copy')({
dest: 'dist'
})
]
};
$ postcss src/index.css
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var postcssCopy = require('postcss-copy');
gulp.task('buildCss', function () {
var processors = [
postcssCopy({
basePath: ['src', 'otherSrc']
dest: 'dist'
})
];
return gulp
.src(['src/**/*.css', 'otherSrc/**/*.css'])
.pipe(postcss(processors))
.pipe(gulp.dest('dist'));
});
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.
?
.#
.var copyOpts = {
...,
template(fileMeta) {
return 'assets/custom-name-' + fileMeta.name + '.' + fileMeta.ext;
}
}
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:.btn {
background-image: url('!images/button.jpg');
}
.background {
background-image: url('!images/background.jpg');
}
// ignore with string
var copyOpts = {
...,
ignore: 'images/*.jpg'
}
// ignore with array
var copyOpts = {
...,
ignore: ['images/button.+(jpg|png)', 'images/background.jpg']
}
// ignore function
var copyOpts = {
...,
ignore(fileMeta, opts) {
return (fileMeta.filename.indexOf('images/button.jpg') ||
fileMeta.filename.indexOf('images/background.jpg'));
}
}
Define a custom function to create the hash name.
var copyOpts = {
...,
hashFunction(contents) {
// borschik
return crypto.createHash('sha1')
.update(contents)
.digest('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '')
.replace(/^[+-]+/g, '');
}
};
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)
.
var Imagemin = require('imagemin');
var imageminJpegtran = require('imagemin-jpegtran');
var imageminPngquant = require('imagemin-pngquant');
var copyOpts = {
...,
transform(fileMeta) {
if (['jpg', 'png'].indexOf(fileMeta.ext) === -1) {
return fileMeta;
}
return Imagemin.buffer(fileMeta.contents, {
plugins: [
imageminPngquant(),
imageminJpegtran({
progressive: true
})
]
})
.then(result => {
fileMeta.contents = result;
return fileMeta; // <- important
});
}
};
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/
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var postcssCopy = require('postcss-copy');
var postcssImport = require('postcss-import');
var path = require('path');
gulp.task('buildCss', function () {
var processors = [
postcssImport(),
postcssCopy({
basePath: ['src', 'node_modules'],
preservePath: true,
dest: 'dist'
})
];
return gulp
.src('src/**/*.css')
.pipe(postcss(processors, {to: 'dist/css/index.css'}))
.pipe(gulp.dest('dist/css'));
});
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:
{
sourceInputFile, // path to the origin CSS file
sourceValue, // origin asset value taked from the CSS file
filename, // filename normalized without query string
absolutePath, // absolute path of the asset file
fullName, // name of the asset file
path, // relative path of the asset file
name, // name without extension
ext, // extension name
query, // full query string
qparams, // query string params without the char '?'
qhash, // query string hash without the char '#'
basePath // basePath found
}
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
[7.1.0] - 2017-06-30
FAQs
A postcss plugin to copy all assets referenced in CSS to a custom destination folder and updating the URLs.
We found that postcss-copy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.