npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reorient-css

v0.3.0

Published

Updates relative asset URLs within a string of CSS to reflect a relocation of the CSS file.

Downloads

16,576

Readme

reorient-css Build Status

NPM

Updates relative asset URLs (e.g. url(../images/something.png)) within a string of CSS to reflect a relocation of the CSS file.

Example use case: if you were inlining CSS from styles/main.css into a <style> element in index.html (as part of a build task aiming to reduce HTTP round trips), you could use this function to rewrite all the image/font URLs within the CSS so they still work from the new relative base – in this example, it would prepend all file-relative URLs with style/ (and resolve any .. etc).

Usage

npm install reorient-css

var reorientCSS = require('reorient-css');

var result = reorientCSS(
  'body { background: url(something.png); }', // css string
  'some/old/stylesheet/file.css',             // old css location
  'some/new/one.css'                          // new css location (this could even be an .html file,
);                                            // if you're inlining the CSS into a <style> element
                                              // - the basename is ignored anyway)

console.log(result.css);
// > body { background: url('../old/stylesheet/something.png'); }

Notes

  • It doesn't care whether files exist at the old/new paths – it just uses these path strings to determine the relative path from the new location back to the old location, so it can know how to rewrite the asset URLs.
  • Only file-relative URLs are rewritten. Root-relative, absolute and data: URLs will be left untouched, as these are expected to work the same regardless of the CSS file's location.
  • behavior properties (a proprietary IE thing) will be left untouched, because any URLs in these properties are relative to the HTML document, not the CSS file, so in theory they should never change.
    • The only exception is if you are moving CSS from one HTML document to another (ie, in <style> elements). If both the new and old CSS locations you provide are .html files, behavior properties will be reoriented.
  • Whitespace and formatting are left untouched.
  • The output is an object with a property named css, which contains the reoriented CSS string. This object might also contain a map property, if you have specified that option (see below).

Options

The function accepts an optional fourth argument: an object of extra options to pass to PostCSS. For example, {map: true} will tell it to generate a source map (which will be available in the map key of the result object). See the PostCSS docs for more details.

PostCSS

reorient-css can also be used as a PostCSS processor.

var postcss = require('postcss'),
	reorientCSS = require('reorient-css');

var result = postcss()
	.use( reorientCSS.processor( 'some/old/stylesheet/file.css', 'some/new/one.css' ) )
	.process( 'body { background: url(something.png); }' );

console.log(result.css);
// > body { background: url('../old/stylesheet/something.png'); }

License

Copyright (c) 2014 Callum Locke. Licensed under the MIT license.