@csstools/postcss-rewrite-url
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

PostCSS Rewrite URL PostCSS Logo

npm install @csstools/postcss-rewrite-url --save-dev

PostCSS Rewrite URL lets you rewrite url values in CSS.

.foo {
	background: rewrite-url('foo.png');
}

@font-face {
	font-family: "Trickster";
	src:
		local("Trickster"),
		rewrite-url("trickster-COLRv1.otf") format("opentype");
}

/* becomes */

.foo {
	background: url("foo.png#modified");
}

@font-face {
	font-family: "Trickster";
	src:
		local("Trickster"),
		url("trickster-COLRv1.otf#modified") format("opentype");
}

Usage

Add PostCSS Rewrite URL to your project:

npm install postcss @csstools/postcss-rewrite-url --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssRewriteURL = require('@csstools/postcss-rewrite-url');

postcss([
	postcssRewriteURL(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);

Options

rewriter

Determine how urls are rewritten with the rewriter callback.

export interface ValueToRewrite {
	url: string
}

export interface RewriteContext {
	type: 'declaration-value' | 'at-rule-prelude';
	from: string | undefined;
	rootFrom: string | undefined;
	property?: string;
	atRuleName?: string;
}

export type Rewriter = (value: ValueToRewrite, context: RewriteContext) => ValueToRewrite | false;

/** postcss-rewrite-url plugin options */
export type pluginOptions = {
	rewriter: Rewriter;
};
postcssRewriteURL({
	rewriter: (value, context) => {
		if (value.url === 'ignore-me') {
			// return `false` to ignore this url and preserve `rewrite-url()` in the output
			return false;
		}

		console.log(context); // for extra conditional logic
		return {
			url: value.url + '#modified',
		};
	},
})

Package Sidebar

Install

npm i @csstools/postcss-rewrite-url

Weekly Downloads

85

Version

2.0.4

License

MIT-0

Unpacked Size

9.37 kB

Total Files

7

Last publish

Collaborators

  • romainmenke
  • alaguna
  • jonathantneal