wiledal / gulp-include

Enables functionality similar to that of snockets / sprockets or other file insertion compilation tools.
158 stars 68 forks source link

Require Behaviour #73

Closed dhedey closed 5 years ago

dhedey commented 8 years ago

My team was recently using gulp-include and were building multiple entry points in parallel (using one invocation of gulp-include in the gulp file).

Some of our code should only be included once, hence we required (instead of including it). However, this prevented it from being added in other entry points.

What are peoples' thoughts on this? In the way we're using it, "require" would ideally mean "require once in the recursive building of each entry point file", not "require once across all entry points", but I can appreciate this behaviour may be expected in some (possibly a minority of?) instances.

So would there be support behind a third command? Possibly a require_per_entry option?

My other thoughts on this are that possibly "require" is a little confusing - is is synonymous with require in ruby I guess (but ruby's include is rather different), but on the other hand, PHP has both "require" and "include", which both behave similarly to gulp-includes "include". I wonder what people think of the alternative names which are perhaps a little clearer? (still supporting require for backwards compatibility):

I'd be happy to put in a pull request for something akin to include_once_per_entry_point, or something similar, assuming it would be appreciated?

cshold commented 7 years ago

I ran into this recently as well. In our case, we have different JS files for different areas of the site.

main.js
  - require jquery
  - require helpers
  - require modules/*

secondary.js
  - require jquery
  - require helpers
  - require modules/thing

The second file to run will not include jquery, helpers, or modules/thing since they were already included before. Here's the task code:

gulp.src(['src/scripts/*.js'])
    .pipe(include())
    .pipe(gulp.dest(config.dist.assets));

Our setup allows us to switch to using include instead of require, but seeing as they are separate entry points I was hoping it wouldn't be necessary.

Edit: Using gulp-foreach got it working as expected in my case.

wiledal commented 5 years ago

This is now supported in the plugin with the separateInputs-option. This will treat each entry-file as a separate require-stream.