Description
Edit: Scenario in a nutshell is: "We're generating output into a dist
directory to be deployed on a web server (for development or production). We want source maps to just work."
Hey all,
Thanks for your awesome work on libsass. I'm working on broccoli-sass (which in turn is used by ember-cli), and I'm trying to get the source map support right.
Unfortunately, the paths that are emitted in source maps are somewhat suboptimal for us. I made a minimal example to illustrate this at https://github.com/joliss/node-sass-source-map-example, which I'll walk you through here:
Running standard-output.js to compile styles/app.scss
into dist/assets/app.css
, we get the following source map:
$ node standard-output.js
{
"version": 3,
"file": "app.css",
"sources": [
"../../app.scss", <---------------------- these paths are the problem
"../../../vendor/bootstrap.scss"
],
"sourcesContent": [
"@import \"bootstrap\";\n\nbody {\n background-color: green;\n}\n",
"body::before {\n content: \"Hello world\";\n}\n"
],
"mappings": "ACAA,AAAI;EACF,AAAS;;ADCX;EACE,AAAkB",
"names": []
}
Serving ./dist on http://0.0.0.0:3000/
When we serve dist
using a web server, rather than using file:///
URLs in the browser, paths like "../../app.scss"
that point outside the dist
directory obviously cannot resolve.
As a result, the folder structure in Chrome devtools ends up being all messed up - we expect styles/app.scss
and vendor/bootstrap.scss
, but get the following:
(If you want to try this yourself, note that to you need to enable CSS source maps in the devtools settings.)
As to what we should be emitting instead, I'm not entirely clear, but how about the following source map output (implemented in better-output.js):
$ node better-output.js
{
"version": 3,
"file": "app.css",
"sources": [
"styles/app.scss", <----------------------------- better
"vendor/bootstrap.scss"
],
"sourcesContent": [
"@import \"bootstrap\";\n\nbody {\n background-color: green;\n}\n",
"body::before {\n content: \"Hello world\";\n}\n"
],
"mappings": "ACAA,AAAI;EACF,AAAS;;ADCX;EACE,AAAkB",
"names": [],
"sourceRoot": "file:///home/ubuntu/src/node-sass-source-map-example" <------- added
}
Serving ./dist on http://0.0.0.0:3000/
We're also adding a "sourceRoot"
key pointing at our repo root, so that Chrome devtools will sort the .scss
files separately from the .css
files. This is what it looks like now:
Chrome doesn't seem to be smart enough quite yet to pick up on the file:///
sourceRoot URL and automatically suggest it as a workspace, so that we can edit and save the .scss
source files right there in devtools, but I don't see a reason why this couldn't be added in the future.
At the moment, libsass makes the assumption that "sources"
paths should be relative to the source map file. The example output I'm generating here with better-output.js
rather makes the paths relative to the project root ("sourceRoot"
), and tries to display them in a separate file hierarchy in devtools. I had a hangout with @stefanpenner (from ember-cli) today, and we had some agreement that this seems like a generally reasonable way to do it.
The better-output.js
code uses some hacks to make this happen. Obviously if we want to support this kind of source map output, libsass would need to add proper support for it.
What are your thoughts?