项目作者: geut

项目描述 :
An async postcss plugin to copy all assets referenced in CSS files to a custom destination folder and updating the URLs.
高级语言: JavaScript
项目地址: git://github.com/geut/postcss-copy.git
创建时间: 2015-08-25T13:50:26Z
项目社区:https://github.com/geut/postcss-copy

开源协议:MIT License

下载


postcss-copy

Build Status
Build status
Coverage Status
Dependency Status
devDependency Status

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

Install" class="reference-link"> Install

With npm do:

  1. $ npm install postcss-copy

Quick Start" class="reference-link"> Quick Start

Using postcss-cli

  1. // postcss.config.js
  2. module.exports = {
  3. plugins: [
  4. require('postcss-copy')({
  5. dest: 'dist'
  6. })
  7. ]
  8. };
  1. $ postcss src/index.css

Using Gulp

  1. var gulp = require('gulp');
  2. var postcss = require('gulp-postcss');
  3. var postcssCopy = require('postcss-copy');
  4. gulp.task('buildCss', function () {
  5. var processors = [
  6. postcssCopy({
  7. basePath: ['src', 'otherSrc']
  8. dest: 'dist'
  9. })
  10. ];
  11. return gulp
  12. .src(['src/**/*.css', 'otherSrc/**/*.css'])
  13. .pipe(postcss(processors))
  14. .pipe(gulp.dest('dist'));
  15. });

Options" class="reference-link"> Options

basePath ({string|array} default = process.cwd())

Define one/many base path for your CSS files.

dest ({string} required)

Define the dest path of your CSS files and assets.

template ({string | function} default = ‘[hash].[ext][query]’)

Define a template name for your final url assets.

  • string template
    • [hash]: Let you use a hash name based on the contents of the file.
    • [name]: Real name of your asset.
    • [path]: Original relative path of your asset.
    • [ext]: Extension of the asset.
    • [query]: Query string.
    • [qparams]: Query string params without the ?.
    • [qhash]: Query string hash without the #.
  • function template
    1. var copyOpts = {
    2. ...,
    3. template(fileMeta) {
    4. return 'assets/custom-name-' + fileMeta.name + '.' + fileMeta.ext;
    5. }
    6. }

preservePath ({boolean} default = false)

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'))

ignore ({string | string[] | function} default = [])

Option to ignore assets in your CSS file.

Using the ! key in your CSS:
  1. .btn {
  2. background-image: url('!images/button.jpg');
  3. }
  4. .background {
  5. background-image: url('!images/background.jpg');
  6. }
Using a string or array with micromatch support to ignore files:
  1. // ignore with string
  2. var copyOpts = {
  3. ...,
  4. ignore: 'images/*.jpg'
  5. }
  6. // ignore with array
  7. var copyOpts = {
  8. ...,
  9. ignore: ['images/button.+(jpg|png)', 'images/background.jpg']
  10. }
Using a custom function:
  1. // ignore function
  2. var copyOpts = {
  3. ...,
  4. ignore(fileMeta, opts) {
  5. return (fileMeta.filename.indexOf('images/button.jpg') ||
  6. fileMeta.filename.indexOf('images/background.jpg'));
  7. }
  8. }

hashFunction" class="reference-link"> hashFunction

Define a custom function to create the hash name.

  1. var 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. };

transform" class="reference-link"> transform

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).

  1. var Imagemin = require('imagemin');
  2. var imageminJpegtran = require('imagemin-jpegtran');
  3. var imageminPngquant = require('imagemin-pngquant');
  4. var copyOpts = {
  5. ...,
  6. transform(fileMeta) {
  7. if (['jpg', 'png'].indexOf(fileMeta.ext) === -1) {
  8. return fileMeta;
  9. }
  10. return Imagemin.buffer(fileMeta.contents, {
  11. plugins: [
  12. imageminPngquant(),
  13. imageminJpegtran({
  14. progressive: true
  15. })
  16. ]
  17. })
  18. .then(result => {
  19. fileMeta.contents = result;
  20. return fileMeta; // <- important
  21. });
  22. }
  23. };

Using copy with postcss-import" class="reference-link"> Using copy with postcss-import

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:

  1. myProject/
  2. |-- node_modules/
  3. |-- dest/
  4. |-- src/

Full example

  1. var gulp = require('gulp');
  2. var postcss = require('gulp-postcss');
  3. var postcssCopy = require('postcss-copy');
  4. var postcssImport = require('postcss-import');
  5. var path = require('path');
  6. gulp.task('buildCss', function () {
  7. var processors = [
  8. postcssImport(),
  9. postcssCopy({
  10. basePath: ['src', 'node_modules'],
  11. preservePath: true,
  12. dest: 'dist'
  13. })
  14. ];
  15. return gulp
  16. .src('src/**/*.css')
  17. .pipe(postcss(processors, {to: 'dist/css/index.css'}))
  18. .pipe(gulp.dest('dist/css'));
  19. });

About lifecyle and the fileMeta object" class="reference-link"> About lifecyle and the fileMeta object

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:

  1. Detect the url in the CSS files
  2. Validate url
  3. 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. }
  4. Check ignore function
  5. Read the asset file (using a cache buffer if exists)
  6. Add content property in the fileMeta object
  7. Execute custom transform
  8. Create hash name based on the custom transform
  9. Add hash property in the fileMeta object
  10. Define template for the new asset
  11. Add resultAbsolutePath and extra properties in the fileMeta object
  12. Write in destination
  13. Write the new URL in the PostCSS node value.

On roadmap" class="reference-link"> On roadmap

nothing for now :)

Credits" class="reference-link"> Credits

License

MIT