Closed tsarapke closed 8 years ago
Currently, I found way how to get access to files, which were concatenated. In gulp-useref/index.js I added additional argument in exports which called "callback" and then call him.
module.exports = function (opts, callback) {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-useref', 'Streaming not supported'));
return;
}
var output = useref(file.contents.toString(), opts);
callback(output[1]);
"output" have following structure:
[
resultHtml,
{
css: {
'css/combined.css': {
'assets': [ 'css/one.css', 'css/two.css' ]
}
},
js: {
'scripts/combined.js': {
'assets': [ 'scripts/one.js', 'scripts/two.js' ]
},
'scripts/async.js': {
'assets': [ 'scripts/three.js', 'scripts/four.js' ]
}
}
}
]
Example of my tasks
[
"build/src/js/**/*.js",
"!build/src/js/**/file-1.js",
"!build/src/js/**/file-2.js",
...
"!build/src/js/**/file-n.js"
]
var bundleFiles = {
js: [],
css: []
};
function getAssets(output, type) {
if(output[type] !== undefined) {
for(var key in output[type]) {
for(var i = 0; i < output[type][key].assets.length; i++) {
bundleFiles[type].push(path.basename(output[type][key].assets[i]));
}
}
}
bundleFiles[type] = uniq(bundleFiles[type]); // lodash uniq method
}
function getExcludion(type, src, src_folder) {
var excludion = bundleFiles[type];
for(var i = 0; i < excludion.length; i++) {
excludion[i] = "!" + path.join(src_folder, excludion[i]);
}
excludion.unshift(src);
return excludion;
}
gulp.task("distribution", function () {
var assets = useref.assets();
return gulp.src(path_to_html)
.pipe(assets)
.pipe(gulpif('*.js', uglify()))
.pipe(assets.restore())
.pipe(useref(null, function(output) {
getAssets(output, "js");
getAssets(output, "css");
}))
.pipe(gulp.dest(config.root.dist));
});
gulp.task("js-dist", ["distribution"], function () {
return gulp.src(getExcludion("js", path_to_all_js, path_to_js_folder))
.pipe(gulp.dest(path_setup.dest));
});
So, can you add possibility to get access to files, which participate in concatenation and resolve problem with couple of html files which contain same bundles?
Maybe https://github.com/contra/gulp-cached would work for you?
@tsarapke Hi, I have the same problem. Do you have a solution now?
@ryanwangtian Hi, I wrote about it in my second post here. It's not a good solution, or rather it is not even a solution. It is a crutch. But it works for me (it regard to list of used files).
I had a similar problem, just found this thread, but for me gulp-cached was quite enough: @jonkemp , thanks for the tip
There are other solutions as well for incremental builds:
If I use gulp-useref with couple of html files, it process each time with same bundles.
Structure:
html-file-1.html
html-file-2.html
The general idea of task: