gulp-useref
gulp-useref copied to clipboard
Does not work with gulp-autoprefixer and gulp-sourcemaps
OS X El [email protected] [email protected] [email protected] (For versions info, see this package.json)
TL;DR
I am considering managing all my assets through HTML build blocks. gulp-useref is ideal, taking assets in HTML as a stream that can be piped to other plugins rather than use gulp.src() to define assets path, especially for multiple HTML files.
Here is the major issue I struggled with. See '@ THE ISSUE' in code below:
├── app
│ ├── index.html
│ ├── scripts
│ │ └── main.js
│ └── styles
│ ├── main.scss
│ └── src
├── gulpfile.babel.js
└── package.json
gulp.task('html', () => {
return gulp.src('app/index.html')
.pipe($.useref({},
// after assets in HTML is searched out,
// transform them before concat
lazypipe()
.pipe($.sourcemaps.init)
.pipe(function() {
return $.if(/\.scss$/, $.sass({
precision: 10
}).on('error', $.sass.logError));
})
//# @THE ISSUE
//# it seems gulp-autoprefixer changed the value of "sources" entry in main.css.map,
//# which in turn cannot be identified by Chrome.
.pipe(function() {
return $.if(/\.css$/, $.autoprefixer());
})
))
.pipe($.if(/\.css$/, $.sourcemaps.write('.')))
.pipe($.size({
title: 'html',
showFiles: true
}))
.pipe(gulp.dest('dist'))
});
I've tested some cases:
- With
gulp-autoprefixeras above,main.css.mapcannot be identified by Chrome. The value of"source"entry inmain.css.maponly includedmain.css. The rest of values seemed to be stripped off bygulp-autoprefixer. - Without
gulp-autoprefixer,gulp-sourcemapsworks as expected and Chrome found the sourcemap successfully. The value of"source"entry inmain.css.mapincluded all Sass partials in main.scss. - Only using
gulp-useref'snoconcatoption withgulp-autoprefixerso there was no need to uselazypipe. Sourcemap breaked still.lazypipemight not be the culprit. - Using gulp.src() to define the same
main.scsspath and added 'index.html' path in'html'task without usinggulp-useref, and keep thegulp-sassandgulp-autoprefixercode unchanged.gulp-sourcemapsworks as expected. The value of"source"entry included all Sass partials inmain.scss. - Having tested
JSassets, bothgulp-uglifyandgulp-sourcemapsworked withgulp-useref. The result was consistent with the one without usinggulp-useref. - Without
gulp-useref,gulp-autoprefixerandgulp-sourcemapsworks correctly. See the'styles'task.
Conclusion: gulp-useref seems cannot co-exist with gulp-autoprefixer and gulp-sourcemaps.
Does the stream that gulp-useref emits differ from the one that gulp.src() does?
Details
Here is a reduced test case with a test HTML file, a gulpfile.babel.js and a package.json included. Feel free to clone and try.
The Purpose
To manage all assets, generally including Scss, JS, CSS(from bower), through HTML build blocks. The workflow would be simple and effective, especially for the scenario we have multiple HTML files including different assets. So we do not need to change assets path manually in gulpfile.js for another HTML file including assets different from previous ones.
To manage 'Scss' files directly from HTML, this might be the ideal workflow:
-
put the relative 'Scss' path in HTML, like this
<!-- build:css styles/main.css --> <link rel="stylesheet" href="styles/main.scss"> <!-- endbuild --> -
allow
gulp-userefto analyze 'Scss' assets to be transformed, and use -
gulp-sassto compile 'Scss' files -
gulp-autoprefixerto process the vendor prefixes ofCSSfiles -
gulp-sourcemapsto generate sourmaps.
However the sourcemaps issue is probably at Step 2, 4 or 5.
For now, I use a workaround that yeoman does:
- leaving the
Scssfiles to be handled by'styles'task and use CSS path in HTML. - handle
JSassets in HTML build blocks bygulp-useref