# rework-assets

> Copy all assets referenced to a folder

Latest version **1.1.1** (published 2016-02-03) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install rework-assets
pnpm add rework-assets
yarn add rework-assets
bun add rework-assets
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2016-02-03 |
| First published | 2014-02-04 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 19 |
| Author | Conrad Zimmerman |
| Maintainers | conradz |
| Keywords | rework, css, assets, build, static, files |

## Links

- npm: https://www.npmjs.com/package/rework-assets
- Repository: https://github.com/conradz/rework-assets
- Issues: https://github.com/conradz/rework-assets/issues
- npm.io page: https://npm.io/package/rework-assets

## Dependencies (2)

- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [rework-plugin-function](https://npm.io/package/rework-plugin-function.md) ^1.0.2

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2016-02-03
- 1.1.0 — 2015-02-05
- 1.0.0 — 2014-08-01
- 0.2.0 — 2014-06-04
- 0.1.1 — 2014-03-07
- 0.1.0 — 2014-02-08
- 0.0.2 — 2014-02-04
- 0.0.1 — 2014-02-04

## README

# rework-assets

[![Build Status](https://travis-ci.org/conradz/rework-assets.svg?branch=master)](https://travis-ci.org/conradz/rework-assets)

Copy all assets referenced by the CSS document to a folder, updating the CSS
references.

## Example

```js
var rework = require('rework'),
    assets = require('rework-assets');

var css = ...;

css = rework(css)
    .use(assets({
        src: 'src',
        dest: 'assets'
    }))
    .toString();

// all assets are copied to the `assets` folder, which is referenced by all
// `url(...)` calls in the generated CSS
```

## Reference

### `assets(options)`

Returns a new rework plugin function that will copy all the assets referenced
by the CSS document to a folder. `options` may contain the following values:

 * `src`: The directory where the CSS source files are located. Defaults to the
   current directory.
 * `dest`: The output folder that will contain the copied assets. Defaults to
   the current directory.
 * `prefix`: The URL that is used to prefix the urls from the generated CSS.
   Defaults to empty.
 * `retainName`: Append hash to the original asset name to make identification easier,
   rather than naming solely based on hash.
   Defaults to true.
 * `onError`: A function that is called whenever an error occurs whil reading a
   file. This function can simply ignore the error if desired, which causes the
   URL to be unchanged from the source file. The default function throws the
   error.
 * `onFile`: A function that is called whenever a file is included. It is called
   with the file path. This can be used for tracking which files are referenced
   from the stylesheet.
 * `func`: The name of the CSS function that references an asset in the input.
   Defaults to `'url'`. For example, if `func: 'asset'` is specified, all
   `asset(...)` calls will be found in the input and the copied to the output.
   The output CSS will always use `url(...)` to reference the copied assets in
   the build output.

The path to each asset source is determined by the `src` directory and the
`position.source` property of each node that is set when parsing with
[css-parse](https://github.com/reworkcss/css-parse) if position tracking is
enabled (this is the same information used for generating source maps). This
means that even if the source document is parsed from several files (for
example when using [rework-npm](https://github.com/conradz/rework-npm)), it
will still resolve the referenced asset using the source file path.

The destination file name is derived from a hash of the source file with the
source extension appended. This allows multiple files to reference assets with
the same name but different path. It also deduplicates files originating from
multiple sources but containing the same content.

## Example build script

This is an example build script, showing how to use `rework-assets` in
conjunction with other rework plugins. You can create your own build script
using Node.js directory or you can use a build tool such as
[Grunt](http://gruntjs.com/) or [Gulp](http://gulpjs.com/).

```js
#!/usr/bin/env node

var readSync = require('fs').readFileSync,
    path = require('path'),
    rework = require('rework');

// Rework plugins
var imports = require('rework-npm'),
    assets = require('rework-assets'),
    inherit = require('rework-inherit');

// `$ build-css src/index.css > build/build.css`
process.stdout.write(build(process.argv[2]));

function build (file) {
    var styles = readSync(file, 'utf8'),
        dir = path.dirname(file);

    return rework(styles)
        .use(imports({ dir: dir }))
        .use(assets({
            src: dir,
            dest: path.join(__dirname, '../build/assets'),
            prefix: 'assets/',
            onError: console.error
        }))
        .use(inherit())
        .toString();
}
```

---
_Source: https://npm.io/package/rework-assets · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
