Normalize wrapping quotes for CSS string literals in PostCSS pipelines.
npx @tessl/cli install tessl/npm-postcss-normalize-string@7.0.0PostCSS Normalize String is a PostCSS plugin that normalizes CSS string literals by standardizing their wrapping quotes. It optimizes quote usage for better CSS compression, handles escaped quotes within string content, and provides configurable quote preference options.
npm install postcss-normalize-stringconst normalizeString = require('postcss-normalize-string');For ES modules and TypeScript:
import normalizeString from 'postcss-normalize-string';const postcss = require('postcss');
const normalizeString = require('postcss-normalize-string');
// With default options (double quotes preferred)
const processor = postcss([normalizeString()]);
// With custom options
const processorSingle = postcss([
normalizeString({ preferredQuote: 'single' })
]);
// Process CSS
const input = `p:after{content:'\\'string\\' is intact'}`;
const result = processor.process(input, {from: undefined});
console.log(result.css);
// Output: p:after{content:"'string' is intact"}Creates a PostCSS plugin instance with optional configuration.
/**
* Creates a PostCSS plugin that normalizes CSS string literals
* @param opts - Configuration options for quote preference
* @returns PostCSS plugin instance
*/
function normalizeString(opts?: Options): Plugin;
normalizeString.postcss = true;
interface Options {
/** Quote preference: 'double' (default) or 'single' */
preferredQuote?: 'double' | 'single';
}
interface Plugin {
/** PostCSS plugin name identifier */
postcssPlugin: 'postcss-normalize-string';
/** Plugin processing function called once at the end */
OnceExit(css: Root): void;
}Plugin Behavior:
preferredQuote settingUsage Examples:
// Default behavior (prefers double quotes)
const plugin = normalizeString();
// Prefer single quotes
const pluginSingle = normalizeString({ preferredQuote: 'single' });
// Use with PostCSS
const postcss = require('postcss');
const result = postcss([normalizeString()]).process(cssString, {from: undefined});The plugin applies intelligent quote optimization:
preferredQuote option determines the quote type\\\n) to create single-line stringsProcessing Examples:
// Input with escaped single quotes
`p:after{content:'\\'text\\' here'}`
// Output (switches to double quotes to eliminate escaping)
`p:after{content:"'text' here"}`
// Input with escaped double quotes
`p:after{content:"\\"text\\" here"}`
// Output (switches to single quotes to eliminate escaping)
`p:after{content:'"text" here'}`
// Input with mixed escaped quotes (no change)
`p:after{content:"\\"text\\" and 'more'"}`
// Output (preserves original to avoid new escapes)
`p:after{content:"\\"text\\" and 'more'"}`
// Input with line continuations
`p:after{content:"line one\\\nline two"}`
// Output (collapses to single line)
`p:after{content:"line oneline two"}`The plugin processes these CSS node types:
// Examples of processed CSS patterns:
// Attribute selectors
`[data-content='text']` → `[data-content="text"]`
// Content property
`p:after{content:'text'}` → `p:after{content:"text"}`
// Font family names
`p{font-family:'Comic Sans'}` → `p{font-family:"Comic Sans"}`
// Import statements
`@import url('styles.css')` → `@import url("styles.css")`
// Grid template areas
`div{grid-template-areas:'header header' 'nav main'}`
→ `div{grid-template-areas:"header header" "nav main"}`interface Options {
/**
* Quote preference for strings without internal quotes
* @default 'double'
*/
preferredQuote?: 'double' | 'single';
}