GithubHelp home page GithubHelp logo

frandiox / postcss-url-resolver Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 49 KB

PostCSS plugin that resolves urls (CSS imports and images) via http requests. Isomorphic (node + browser).

License: MIT License

JavaScript 98.20% CSS 1.80%

postcss-url-resolver's Introduction

PostCSS Url Resolver Build Status

PostCSS plugin that resolves urls (CSS imports and images) via http requests.

This plugin is a combination of postcss-import-url, postcss-url-mapper and postcss-base64. It borrows code from all of them and adds some extra features.

Features:

  • Recursively resolves @import and url(...) of remote files.
  • Optionally inlines images in base64.
  • Isomorphic. Works in Node and the browser.
  • HTTP client agnostic. It must be provided as a parameter. This also allows providing custom cache or headers.

Requirements:

  • Native Promise or a polyfill.

Examples:

/* http://some.remote/file.css */

@import "http://fonts.googleapis.com/css?family=Tangerine";

.bar {
  color: green;
  background-image: url('./img/logo.svg');
}
/* Input example */

.foo {
  color: red;
}

@import url('http://some.remote/file.css');

.baz {
  color: blue;
}
/* Output example */

.foo {
  color: red;
}

@font-face {
  font-family: 'Tangerine';
  font-style: normal;
  font-weight: 400;
  src: local('Tangerine'), url(http://fonts.gstatic.com/s/tangerine/v7/HGfsyCL5WASpHOFnouG-RKCWcynf_cDxXwCLxiixG1c.ttf) format('truetype')
}

.bar {
  color: green;
  background-image: url('http://some.remote/img/logo.svg');
}

.baz {
  color: blue;
}

If options.base64 was specified (true), the background-image would look like:

.bar {
  color: green;
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHNZz4=');
}

Usage

This plugin is isomorphic (Node + browser environments). For sake of versatility, it does not bundle any http-request package. Therefore, it must be provided as a parameter.

Node

var postcss = require('postcss');
var urlResolver = require('postcss-url-resolver');
var hh = require('http-https');

postcss([urlResolver({
  exclude: /theme.css$/,
  base64: true,

  request: function(opt) {
    return new Promise(function executor(resolve, reject) {
      var req = hh.get(reqOptions, function(res) {
          var body = '';

          res.on('data', function(chunk) {
            body += chunk.toString();
          });

          res.on('end', function() {
            resolve(body);
          });
      });

      req.on('error', reject);
      req.end();
    });
  }
})])

Browser (Webpack)

import postcss from 'postcss';
import urlResolver from 'postcss-url-resolver';
import axios from 'axios';

postcss([ urlResolver({
  exclude: /theme.css$/,
  base64: true,

  request: function(opt) {
    return axios.get(opt.href)
      .then(res => res.data);
  }
})])

Note: axios can also be used in Node environments.

Parameters

  • request: Function called to make an HTTP request. It gets a parsed URL object as its only parameter. Must return a promise which resolves to the response body (content). Required.
  • recursive: Whether @import should be resolved recursively. Default true.
  • base64: Resolves and inlines images in base 64. Default false.
  • exclude: A RegExp matching urls that won't be resolved. Default null.

See PostCSS docs for examples for your environment.

postcss-url-resolver's People

Contributors

frandiox avatar

Stargazers

James Gillmore avatar

postcss-url-resolver's Issues

Doesn't resolve 'url()' in top-level stylesheet

I'm trying to post-process stylesheets produced by the Rails asset pipeline. I have multiple top-level stylesheets that I want to have inlined (base64-encoded) url()s. This plugin almost does what I need.

As far as I can tell, the plugin currently won't process url()s unless they're inside a stylesheet that's @imported into the top-level stylesheet โ€” and that @import has to reference a remote URL (something starting with at least //).

Given

.foo {
  background-image: url(//example.org/assets.shim.gif);
}

I'd like to get

.foo {
  background-image: url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7);
}

without needing an @import.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.