Repository navigation
Description
Summary
Lightning CSS can generate source maps, and chain them through an input map (e.g. one from Sass), but the gem doesn't expose this. It always prints without a source map, its options don't include one, and LightningCSS::Result has no map. Lightning CSS's Node binding exposes this as sourceMap, inputSourceMap and projectRoot, with the map on the result.
This matters for asset pipelines that compile Sass and then transform or minify the CSS with Lightning CSS. Without it, a source map either has to be dropped at the Lightning CSS step, or it points at CSS that no longer matches the output.
Current behaviour
lightningcss 1.33.0.2 (crate 1.0.0-alpha.72), Ruby 4.0.5, arm64-darwin
LightningCSS.transform(".a { color: red }", filename: "main.css").respond_to?(:map)
#=> false
LightningCSS.transform(".a{}", source_map: true)
#=> LightningCSS::OptionError: Unknown option: source_map
LightningCSS.transform(".a{}", input_source_map: "{}")
#=> LightningCSS::OptionError: Unknown option: input_source_map
LightningCSS.transform(".a{}", project_root: "/")
#=> LightningCSS::OptionError: Unknown option: project_rootNode binding
The same input through the npm package (1.33.0, Node 26.7.0):
const css = ".a {\n user-select: none;\n}\n\n.b {\n color: red;\n}\n";
transform({ filename: "main.css", code: Buffer.from(css), sourceMap: true, minify: true }).map.toString()
// {"version":3,"sourceRoot":null,"mappings":"AAAA,oBAIA","sources":["main.css"],
// "sourcesContent":[".a {\n user-select: none;\n}\n\n.b {\n color: red;\n}\n"],"names":[]}Chained through an input map, as if main.css had been compiled from app.scss:
const inputSourceMap = JSON.stringify({
version: 3, file: "main.css", sources: ["app.scss"], names: [],
sourcesContent: ["$c: red;\n.a { user-select: none; }\n.b { color: $c; }\n"],
mappings: "AACA;EAAI;;;AACJ;EAAI"
});
transform({ filename: "main.css", code: Buffer.from(css), sourceMap: true, inputSourceMap, minify: true }).map.toString()
// {"version":3,"sourceRoot":null,"mappings":"ACCA,oBACA","sources":["main.css","app.scss"],
// "sourcesContent":[..., "$c: red;\n.a { user-select: none; }\n.b { color: $c; }\n"],"names":[]}The chained mappings point into app.scss (source index 1), at the lines the two rules came from.
Cause
In rust/src/lib.rs (on main, ac391b0):
transform_sourceprints withsource_map: None, project_root: None(lines 189–190).bundle_source(line 259) andtransform_attribute(line 303) print with..PrinterOptions::default(), so they don't create a map either.TransformOptions(rust/src/options.rs) andOptions::KNOWN(lib/lightningcss/options.rb) have no source map options, andTransformResult/LightningCSS::Resulthave no map.
Suggested API
result = LightningCSS.transform(css,
filename: "main.css",
source_map: true, # generate a map (default false)
input_source_map: sass_map_json, # optional, a JSON String to chain through
project_root: "/app" # optional, what `sources` are relative to
)
result.map #=> JSON String, or nil when `source_map` isn't truebundle(path, source_map: true)would add every file the bundle reads tosources, with its contents, so the map points into the imported files too.Transformerwould pass these through like the other options.- An
input_source_mapthat isn't valid JSON could raiseOptionError. In my testing the npm package silently ignores one it can't parse, which is easy to miss. transform_style_attributeprobably doesn't need maps.
Implementation notes
PrinterOptions already has the source_map and project_root fields the gem sets to None. Roughly:
- When
source_mapis true, create a source map (Lightning CSS uses theparcel_sourcemapcrate) rooted atproject_root, addfilenameas a source withcodeas its contents, and pass it asPrinterOptions.source_map. - After printing, if
input_source_mapwas given, parse it and extend the generated map with it, so the result maps back to the original sources. - Serialize the map to JSON and return it as
mapin the result JSON, then expose it asResult#map.
I haven't checked which parcel_sourcemap version lightningcss 1.0.0-alpha.72 depends on. A direct dependency would need to match it.
Activity
marcoroth commentedon Nov 8, 2026
Thanks @malomalo, this should be resolved via #27