Skip to content

Support source maps (source_map, input_source_map, project_root) #26

Closed

Description

@malomalo

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_root

Node 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_source prints with source_map: None, project_root: None (lines 189–190).
  • bundle_source (line 259) and transform_attribute (line 303) print with ..PrinterOptions::default(), so they don't create a map either.
  • TransformOptions (rust/src/options.rs) and Options::KNOWN (lib/lightningcss/options.rb) have no source map options, and TransformResult / LightningCSS::Result have 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 true
  • bundle(path, source_map: true) would add every file the bundle reads to sources, with its contents, so the map points into the imported files too.
  • Transformer would pass these through like the other options.
  • An input_source_map that isn't valid JSON could raise OptionError. In my testing the npm package silently ignores one it can't parse, which is easy to miss.
  • transform_style_attribute probably doesn't need maps.

Implementation notes

PrinterOptions already has the source_map and project_root fields the gem sets to None. Roughly:

  1. When source_map is true, create a source map (Lightning CSS uses the parcel_sourcemap crate) rooted at project_root, add filename as a source with code as its contents, and pass it as PrinterOptions.source_map.
  2. After printing, if input_source_map was given, parse it and extend the generated map with it, so the result maps back to the original sources.
  3. Serialize the map to JSON and return it as map in the result JSON, then expose it as Result#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

  1. marcoroth commented on Nov 8, 2026

    @marcoroth
    Owner

    Thanks @malomalo, this should be resolved via #27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions