parcel-bundler/parcel · error · Error
CSSNanoOptimizer: Only string contents are currently support
Error message
CSSNanoOptimizer: Only string contents are currently supported
What it means
Thrown by `CSSNanoOptimizer.optimize` when the incoming bundle contents are not a `string`. cssnano/postcss operate on CSS text, so non-string contents (e.g. a `Buffer` or shared byte buffer) cannot be processed. The optimizer refuses rather than silently coercing.
Source
Thrown at packages/optimizers/cssnano/src/CSSNanoOptimizer.js:41
if (configFile) {
return configFile.contents;
}
},
async optimize({
bundle,
contents: prevContents,
getSourceMapReference,
map: prevMap,
config,
options,
}) {
if (!bundle.env.shouldOptimize) {
return {contents: prevContents, map: prevMap};
}
if (typeof prevContents !== 'string') {
throw new Error(
'CSSNanoOptimizer: Only string contents are currently supported',
);
}
const result = await postcss([
cssnano((config ?? {}: CSSNanoOptions)),
]).process(prevContents, {
// Suppress postcss's warning about a missing `from` property. In this
// case, the input map contains all of the sources.
from: undefined,
map: {
annotation: false,
inline: false,
prev: prevMap ? await prevMap.stringify({}) : null,
},
});
let map;View on GitHub (pinned to 59484858a1)
Solutions
- Ensure the CSS pipeline keeps contents as a string through to the optimizer.
- If you control the upstream stage, return string contents (decode buffers to utf8).
- Reorder optimizers so byte-producing optimizers run after cssnano.
- Confirm the bundle type is `css` and not mistakenly tagged as a binary type.
Example fix
// before — upstream returns Buffer
return {contents: Buffer.from(css), map};
// after — keep string
return {contents: css, map}; Defensive patterns
Strategy: type-guard
Validate before calling
function ensureStringContents(prev) {
if (typeof prev !== 'string') throw new Error('cssnano expects string CSS');
return prev;
} Type guard
function isStringContents(c) { return typeof c === 'string'; } Prevention
- Keep CSS pipeline contents as strings end-to-end.
- Place byte-producing optimizers after cssnano.
- Decode buffers to utf8 before returning from upstream optimizers.
When it happens
Trigger: A CSS bundle whose prior pipeline stage produced non-string contents; an optimizer chain where an earlier optimizer returned a Buffer/Uint8Array; misconfigured bundle type feeding binary into the cssnano optimizer.
Common situations: Custom optimizer ordering where a raw/byte optimizer runs before cssnano; assets marked as binary reaching a CSS bundle; version change in a transformer that switches the contents type.
Related errors
- Invalid distPath for target "${targetName}"
- Unsupported custom SVGO plugin.
- Library targets are not supported in serve mode.
- Expected package.json file in ${rootDir}
- Unexpected output file type ${ext} in target "${targetName}"
AI-assisted analysis of parcel-bundler/parcel@59484858a1 (2026-08-13).
Data as JSON: /api/errors/d3fe545951fa365d.
Report an issue: GitHub.