Autoprefixer Is Causing Node-sass Source Map To Point To The Wrong Files
I built a custom Node script that does all of my SCSS processing manually in JS rather than through the command line or with webpack.
The process is as follows:
- Watch .scss files for changes.
- On change, compile the SCSS to CSS with
node-sass
and generate a source map withnode-sass
at the same time. - Write the
screen.css
andscreen.css.map
file using `fs.writeFile() - For the
screen.css
file, I then re-read it withfs.readFile
to get the buffer, and run the CSS contents throughpostcss
using theautoprefixer
plugin to autoprefix all of the CSS. I then re-write this usingfs.writeFile
again and finally upload it to the server usingjsftp
. At the same time, the process for thescreen.css.map
file is the exact same, except that I don't run it throughpostcss
, I just re-read it and upload it to the server after the initial writing.
My problem
If I include the autoprefixer
plugin when using postcss
, e.g:
postcss([autoprefixer]).process(buffer, {
from: "assets/css/screen.css",
to: "assets/css/screen.css"
}).then(result => {...})
the sourcemap is completely wrong when inspecting in the browser and points to not only the wrong lines but also the wrong files.
For example, the SCSS for the page banner is in _banner.scss
, but the sourcemap tells me it is all in _calendar.scss
.
However if I keep all of the code the exact same but leave out autoprefixer
, e.g:
postcss([]).process(buffer, {
from: "assets/css/screen.css",
to: "assets/css/screen.css"
}).then(result => {...})
The sourcemap works perfectly, pointing me correctly to _banner.scss
for all the banner styles.
Has anyone got any idea how I can fix this and get my SCSS sourcemaps working whilst still using autoprefixer?
Link to full sourcecode for the SCSS processing node script here, ignore the horrible nesting, this is just an early version :) https://github.com/JJGerrish/twk-boilerplate/blob/master/template/style.js
Answer
For anyone who needs it I found a fix.
Basically, postcss
has an option to detect a previous source map and use it for it's own source map instead of generating a new one.
I found this solution on a GitHub issue that did the trick for me, basically, you have to get the node-sass
generated source map, stringify it, and then pass it to the postcss
map
option object.
https://github.com/postcss/postcss/issues/222#issuecomment-318136962
Code below incase link disappears:
postcss([autoprefixer])
.process(result.css, {
from: "assets/css/screen.css",
to: "assets/css/screen.css",
map: {
prev: result.map.toString(),
inline: false,
},
})
.then(result => {...})
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM