# rewrite-css-urls

> Parses CSS file and finds URL references so that they can be rewritten

Latest version **1.0.4** (published 2016-03-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install rewrite-css-urls
pnpm add rewrite-css-urls
yarn add rewrite-css-urls
bun add rewrite-css-urls
```

## 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.0.4 |
| Published | 2016-03-14 |
| First published | 2016-01-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Phillip Gates-Idem |
| Maintainers | philidem |
| Keywords | CSS, URL, rewrite, proxy |

## Links

- npm: https://www.npmjs.com/package/rewrite-css-urls
- Repository: https://github.com/philidem/rewrite-css-urls
- Homepage: https://github.com/philidem/rewrite-css-urls#readme
- Issues: https://github.com/philidem/rewrite-css-urls/issues
- npm.io page: https://npm.io/package/rewrite-css-urls

## 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.0.4 (latest) — 2016-03-14
- 1.0.3 — 2016-03-11
- 1.0.2 — 2016-03-10
- 1.0.1 — 2016-01-05
- 1.0.0 — 2016-01-05

## README

rewrite-css-urls
================

This module is used to assist with rewriting URLs inside CSS code.
It looks for URL patterns and provides convenience functions for
replacing the URLs that were found.

To reduce complexity and code size, this utility does not use
a fully CSS-compliant parser. In fact, the _parser_ simply uses
a couple of regular expressions and ignores comment blocks.

This utility searches for `url(*)` and `@import "*"` patterns specifically.

## Installation

`npm install rewrite-css-urls --save`

## Usage

### Find
```javascript
require('rewrite-css-urls').find(cssCode, function(urlRef) {
    console.log('Found URL: ' + urlRef.url);
});
```

### Find & Replace
```javascript
var transformedCssCode = require('rewrite-css-urls').findAndReplace(cssCode, {
    replaceUrl: function(urlRef) {
        return urlRef.url.replace('blah.com', 'mycompany.com');
    }
});
```

## JavaScript API

The `require('rewrite-css-urls')` module exports the properties below.

### Method: `findAndReplace(cssCode, options)`

#### Arguments:

| Argument                   | JavaScript Type    | Required / Optional | Description |
|----------------------------|--------------------|-------------------|---------------|
| `cssCode`                  | `String`           | required          | The raw CSS code |
| `options`                  | `Object`           | required          | Collection of options |
| `options.replaceUrl`       | `Function(UrlRef)` | optional          | This function will be called to replace all types of URLs and will be used when a more specific `options.replaceImportUrl` or `options. replaceAssetUrl ` is not provided. Return `undefined` to not perform subsitution. |
| `options.replaceImportUrl` | `Function(UrlRef)` | optional          | This function will be called to replace `@import "*"` URLs. Return `undefined` to not perform subsitution. |
| `options.replaceAssetUrl`  | `Function(UrlRef)` | optional          | This function will be called to replace `url(*)` URLs. Return `undefined` to not perform subsitution. |

#### Returns:

`String` The transformed CSS code after URL substitutions.

### Method: `find(cssCode, forEachFn)`

| Argument                   | JavaScript Type    | Required / Optional | Description |
|----------------------------|--------------------|---------------------|---------------|
| `cssCode`                  | `String`           | required            | The raw CSS code |
| `forEachFn`                | `Function(UrlRef)` | required            | Function that will be called for each URL |

### Type: `UrlRef`

Instances of `UrlRef` will be passed to the `replaceUrl`, `replaceImportUrl`,
and `replaceAssetUrl` functions.

| Property    | JavaScript Type    | Description |
|-------------|--------------------|-------------|
| `url`       | `String`           | The actual URL
| `start`     | `Number`           | Start character position |
| `end`       | `Number`           | End character position |
| `type`      | `Number`           | Either `require('rewrite-css-urls').UrlType.IMPORT_URL` (@import URL) or `require('rewrite-css-urls').UrlType.ASSET_URL` (image or font URL) |

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