CSS to JavaScript Bundle Converter
Convert CSS into a self-injecting JavaScript snippet that embeds styles via a DOM style element. Configurable prefix, suffix, minification, and newline options.
About
Distributing a front-end application as a single JavaScript file requires embedding CSS directly into the JS bundle. This converter wraps raw CSS text inside a JavaScript snippet that programmatically creates a <style> element and injects it into the document <head> at runtime. The approach mirrors tools like gulp-css2js and grunt-css2js. Incorrect escaping of quote characters, backslashes, or newline sequences in the CSS source will produce a JavaScript syntax error that silently breaks your entire stylesheet. This tool handles those edge cases: it escapes \, ", and line terminators before wrapping.
An optional CSS minification pass strips comments, collapses whitespace, and removes redundant semicolons before conversion, reducing the final bundle size. The prefix and suffix are fully configurable - you can output a DOM-injecting IIFE, assign CSS text to a variable, or wrap it in a CommonJS module.exports. Note: this tool performs textual wrapping only. It does not parse or validate CSS syntax. Malformed CSS will be faithfully embedded as-is.
Formulas
The conversion algorithm applies a sequential transformation pipeline to the raw CSS source string S:
S1 = minify(S) if minification enabled
S2 = trimTrailingNewline(S1)
S3 = trimSpacesBeforeNewline(S2)
S4 = escapeBackslashes(S3) → \ → \\
S5 = escapeQuotes(S4) → " → \"
S6 = splitOnNewline(S5) if enabled: join lines with \n" + "
output = prefix + S6 + suffix
Where prefix defaults to an IIFE that creates a <style> element: (function(c){var d=document,s=d.createElement("style");s.textContent=c;d.head.appendChild(s);})(" and suffix closes with ");. The minify function applies regex passes: strip /* ... */ comments, collapse consecutive whitespace to single space, remove spaces adjacent to { } : ; , selectors, and remove trailing semicolons before closing braces.
Reference Data
| Option | Default | Effect |
|---|---|---|
| prefix | DOM injection IIFE open | String prepended before escaped CSS text |
| suffix | DOM injection IIFE close | String appended after escaped CSS text |
| splitOnNewline | TRUE | Each CSS line becomes a separate JS string concatenation line |
| trimSpacesBeforeNewline | TRUE | Strips trailing spaces on each line before escaping |
| trimTrailingNewline | TRUE | Removes final newline characters from CSS input |
| minifyCSS | FALSE | Strips comments, collapses whitespace, removes redundant semicolons |
| Escape: \ | Always | Backslashes doubled to \\ |
| Escape: " | Always | Double quotes escaped to \" |
| Escape: " | When single-quote prefix | Single quotes escaped to \" |
| Escape: newline | Always | Literal newlines replaced with \n in strings |
| Escape: carriage return | Always | Literal \r removed or escaped |
| Output: DOM injection | Default mode | Generates IIFE that creates <style> element |
| Output: Variable assignment | Preset available | Assigns CSS string to var cssText |
| Output: ES module export | Preset available | Uses export default syntax |
| Output: CommonJS export | Preset available | Uses module.exports syntax |
| File input | Supported | Upload .css files via drag-and-drop or file picker |
| File output | Supported | Download result as .js file |