1.0.4 • Published 8 years ago

rewrite-css-urls v1.0.4

Weekly downloads
276
License
ISC
Repository
github
Last release
8 years ago

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

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

Find & Replace

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:

ArgumentJavaScript TypeRequired / OptionalDescription
cssCodeStringrequiredThe raw CSS code
optionsObjectrequiredCollection of options
options.replaceUrlFunction(UrlRef)optionalThis 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.replaceImportUrlFunction(UrlRef)optionalThis function will be called to replace @import "*" URLs. Return undefined to not perform subsitution.
options.replaceAssetUrlFunction(UrlRef)optionalThis 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)

ArgumentJavaScript TypeRequired / OptionalDescription
cssCodeStringrequiredThe raw CSS code
forEachFnFunction(UrlRef)requiredFunction that will be called for each URL

Type: UrlRef

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

PropertyJavaScript TypeDescription
urlStringThe actual URL
startNumberStart character position
endNumberEnd character position
typeNumberEither require('rewrite-css-urls').UrlType.IMPORT_URL (@import URL) or require('rewrite-css-urls').UrlType.ASSET_URL (image or font URL)
1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago