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

gulp-css-adjust-url-path

v1.1.0

Published

Adjust absolute CSS url-properties so that they become relative upon build.

Downloads

33

Readme

gulp-css-adjust-url-path

Correct the url-property inside CSS to make absolute paths relative, no matter where the CSS files are located.

Imagine, if you will, the following structure of your application:

/root
  assets
      fonts
          font.woff
          ...
  module
    module.scss
    
    submodule
      submodule.scss
  styles.scss

Using this structure it would be easy to define in the styles.scss that the fonts are located in

@font-face {
    font-family: FONT; 
    src: url(/assets/fonts/font.woff);
}

Up until here: No problem, all is good, no plugin needed.

Now imagine further, if you will, that you push your application to another environment which might look something like this:

/root
  websites
    customer
      project
        assets
            fonts
                font.woff
                ...
        module
          module.scss
          
          submodule
            submodule.scss
        _fonts.scss
        styles.scss

If you don't have access to the server config and if you have to accept that the url of your index file will be http://server.dev/websites/customer/project/index.html instead of http://server.dev/index.html then you are going to have a bad time with your absolutely referenced uris.

Generally it would be best to avoid using absolute uris. But some benefits from css preprocessing would be lost, if one had to avoid absolute uris altogether.

This is where this plugin comes in. It adjusts your (compiled) css so that all findings of url:(...) are corrected in a way that they will all be granted a relative uri where an absolute uri has been.

For example: The above mentioned files might have contained something like this:

_fonts.scss:
@font-face {
    font-family: FONT; 
    src: url(assets/fonts/font.woff);
}

module.scss:
@import '../fonts';

submodule.scss:
@import '../../fonts';

The compiled css would contain something like this:

module.css:
@font-face {
    font-family: FONT; 
    src: url(assets/fonts/font.woff); <- WRONG, assets folder is not on same level as module.scss
}

submodule.css:
@font-face {
    font-family: FONT; 
    src: url(assets/fonts/font.woff); <- WRONG, assets folder is not on same level as submodule.scss
}

If the font were to be defined with an absolute uri then a problem would arise with aforementioned server structure because root would then be on a totally different position.

Using this plugin the output of the css would become this:

module.css:
@font-face {
    font-family: FONT; 
    src: url(../assets/fonts/font.woff); <- CORRECT
}

submodule.css:
@font-face {
    font-family: FONT; 
    src: url(../../assets/fonts/font.woff); <- CORRECT
}

Install

$ npm install --save-dev gulp-css-adjust-url-path

Usage

To make this plugin work you must create a valid regex object that describes your url.

In your gulpfile you should pipe the module like this:

var cssAdjustUrlPath = require('gulp-css-adjust-url-path');

[some gulp task]
  .pipe(cssAdjustUrlPath(/(url\(['"]?)[/]?(assets)/g))

As you can see: The regex defines two capturing groups. One group grabs the first part url( (with or without quotation marks) and the second part grabs something after that, something that makes it clear for the plugin which url-definitions to mutate. Of course, if all uris are to be changed, then the second capturing group could contain nothing.

Beware of using the g modifier. Without it only the first finding inside a file would be corrected.

License

MIT © Tobias Winkler