# absolutize-css-resources

> Convert css images and other resources into absolute paths

Latest version **1.2.3** (published 2016-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install absolutize-css-resources
pnpm add absolutize-css-resources
yarn add absolutize-css-resources
bun add absolutize-css-resources
```

## 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.2.3 |
| Published | 2016-03-03 |
| First published | 2015-10-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.11.0 |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | neuron-js |
| Maintainers | kael |
| Keywords | absolutize-css-resources, absolute, css, images, url |

## Links

- npm: https://www.npmjs.com/package/absolutize-css-resources
- Repository: https://github.com/kaelzhang/absolutize-css-resources
- Homepage: https://github.com/kaelzhang/absolutize-css-resources#readme
- Issues: https://github.com/kaelzhang/absolutize-css-resources/issues
- npm.io page: https://npm.io/package/absolutize-css-resources

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^1.4.2
- [chalk](https://npm.io/package/chalk.md) ^1.1.1
- [print-code](https://npm.io/package/print-code.md) ^1.0.1
- [wrap-as-async](https://npm.io/package/wrap-as-async.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.2.3 (latest) — 2016-03-03
- 1.2.2 — 2016-03-03
- 1.2.1 — 2016-01-08
- 1.2.0 — 2015-12-24
- 1.1.10 — 2015-11-14
- 1.1.9 — 2015-11-01
- 1.0.9 — 2015-10-26
- 1.0.8 — 2015-10-25
- 1.0.7 — 2015-10-25
- 1.0.6 — 2015-10-25
- 1.0.5 — 2015-10-25
- 1.0.4 — 2015-10-25
- 1.0.3 — 2015-10-24
- 1.0.2 — 2015-10-24
- 1.0.1 — 2015-10-24
- … 1 more at https://npm.io/package/absolutize-css-resources/versions

## README

[![Build Status](https://travis-ci.org/neuron-js/absolutize-css-resources.svg?branch=master)](https://travis-ci.org/neuron-js/absolutize-css-resources)

<!-- [![NPM version](https://badge.fury.io/js/absolutize-css-resources.svg)](http://badge.fury.io/js/absolutize-css-resources)
[![npm module downloads per month](http://img.shields.io/npm/dm/absolutize-css-resources.svg)](https://www.npmjs.org/package/absolutize-css-resources) -->
<!-- [![Dependency Status](https://david-dm.org/neuron-js/absolutize-css-resources.svg)](https://david-dm.org/neuron-js/absolutize-css-resources) -->

# absolutize-css-resources

Converts the links of all css images, fonts and other resources into absolute paths from a css file, and you are free to worry about improper replacement.

## Install

```sh
$ npm install absolutize-css-resources --save
```

## Usage

```js
var absolutize = require('absolutize-css-resources');
var url = require('url');

absolutize(file_content, {
  filename: '/path/to/style.css',
  filebase: '/path',
  resolve: function(path){
    return url.resolve('http://mydomain.com/static/', path); 
  }
}, function(err, parsed_content){
  parsed_content;
});
```

##### If we have a css file:

```css
body {
  background: url(img/pic.png)
}
```

Then the `parsed_content` will be:

```css
body {
  background: url(http://mydomain.com/static/to/img/pic.png)
}
```

### absolutize(file_content, options, callback [, callback_on_found])

- **file_content** `String` the content of the css file
- **options** `Object`
  - **filename** `path` the absolute path of the css file
  - **filebase** `path`
  - **resolve** `function(relative_path)` the method to resolve the `relative_path` to an absolute url
    - relative_path the path of each css image or resource which is relative to `filebase` 
  - **allow_absolute_url** `Boolean=true` whether allow absolute url of css images, such as `background: url(/a.png)` which is a bad practice.
- **callback** `function()` the error(if exists) and the parsed content will pass to the callback function.
- **callback_on_found** `function(path, relative_path)=` called if a css resource is found. Optional.
  - **path** the original pathname of the resource
  - **relative_path** the pathname relative to the `filebase`

#### options.resolve(relative_path)

`options.resolve` can be a synchronous method or an asynchronous one by using the common [`this.async()`](https://www.npmjs.com/package/wrap-as-async) style.

Sometimes, we need to invoke an asynchronous process to fetch the version info of a image, querying version from db or digesting md5 from the file content, for example.

```js

// options
{
  resolve: function(relative_path){
    // Turns the method into an async method
    var done = this.async();

    my_method_2_get_md5(relative_path, function(err, md5){
      if (err) {
        return done(err);
      }

      var REGEX_EXTENSION = /\.[a-z0-9]$/i;

      // Inserts md5 string into the path
      relative_path = relative_path.replace(REGEX_EXTENSION, function(match){
        return '-' + md5.slice(0, 7) + match;
      });

      // Converts the relative path into an url,
      // The converted url might be something like:
      // -> http://mydomain.com/static/to/pic-9f9dd65.png
      var resolved = require('url').resolve('http://mydomain.com/static/', relative_path);

      done(null, resolved);
    });
  }
}
```

## License

MIT

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