Giter VIP home page Giter VIP logo

postcss-middleware's Introduction

postcss-middleware

NPM version npm license Travis Build Status AppVeyor Build Status

npm

PostCSS middleware for Connect and Express frameworks.

Installation

$ npm install postcss-middleware

Usage

JavaScript

var postcssMiddleware = require('postcss-middleware');

TypeScript

import * as postcssMiddleware from 'postcss-middleware';

Connect

const connect = require('connect');
const app = connect();
app.use('/css', postcssMiddleware(/* options */));

Express

const express = require('express');
const app = express();
app.use('/css', postcssMiddleware(/* options */));

Options

plugins

Type: Array Required: true

An array of PostCSS plugins.

options

Type: Object Required: false

PostCSS options such as syntax, parser or map.

app.use(postcssMiddleware({
	plugins: [/* plugins */],
	options: {
		parser: require('sugarss'),
		map: { inline: false }
	}
}
});

src

Type: (request) => string|string[] Required: false Default: req => path.join(__dirname, req.url)

A callback function that will be provided the Express app's request object. Use this object to build the file path to the source file(s) you wish to read. The callback can return a glob string or an array of glob strings. All files matched will be concatenated in the response.

var path = require('path');
app.use('/css', postcssMiddleware({
	src: function(req) {
		return path.join('styles', req.path);
	},
	plugins: [/* plugins */]
});

The above example will match requests to /css. If /css/foo.css were requested, the middleware would read /styles/foo.css in the context of your application.

Using a regular expression route path, we can back-reference a capture group and use it as a folder name.

var path = require('path');
app.use(/^\/css\/([a-z-]+)\.css$/, postcssMiddleware({
	src: function(req) {
		var folder = req.params[0];
		return path.join('styles', folder, '*.css');
	},
	plugins: [/* plugins */]
});

If you were to request /css/foo-bar.css in the above example, the middleware would concatenate all CSS files in the /styles/foo-bar folder in the response.

inlineSourcemaps

Type: Boolean Required: false Default: undefined

Generate inlined sourcemaps.

postcss-middleware's People

Contributors

jednano avatar jedmao avatar maistho avatar nitive avatar

Watchers

James Cloos avatar

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.