question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't remove injections when stream is empty

See original GitHub issue

Hi, I’m using gulp-inject to inject some files ./app/scripts/**/*.js into my index.html. When gulp finds files, the plugin works as expected and injects all files. But when I remove all files from the filesystem the files aren’t removed in the index.html. I experimented a little bit and found out that when at least one file is in the stream the plugin correctly removes all others. When I remove the last file, the index.html remains untouched resulting in 404’s.

I reduced the problem to this simple task to reproduce it:

gulp.task('inject', function () {
  var jsFiles = gulp.src(['./app/scripts/**/*.js']);

  return gulp.src('./app/index.html')
    .pipe($.inject(jsFiles, {relative: true}))
    .pipe(gulp.dest('./app'));
});

Am I doing something wrong or is this the intended behavior?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kurdincommented, Feb 16, 2015

in case if anybody need workaround, here is how I remove scripts with gulp-replace

var replace = require('gulp-replace');
...
gulp.task('remove-dev-js', function() {
  return gulp.src('./html/**/*.html')
    .pipe(replace(/<!-- (.*?):js -->([\S\s]*?)<!-- endinject -->/gmi, '<!-- $1:js -->\n<!-- endinject -->'))
    .pipe(gulp.dest('./html'));
});
0reactions
derekgreercommented, Aug 28, 2015

The desire to remove sections probably arises for many where style sheets or scripts need to be injected in a particular order for local/debug builds and as combined/minifed for release builds. For example:

<!-- bower:js -->
<!-- endinject -->

<!-- otherThirdParty:js -->
<!-- endinject -->

<!-- myStuff:js -->
<script src="~/dist/lib/how-it-be-in-prod-9cebad55e4.js"></script>
<!-- endinject -->

For such cases, another workaround to consider is to use a single injection tag for all script types and combine multiple streams into one stream which is passed to inject. This will also allow you to conditionally combine streams based upon environment variables, etc. The best library I’ve found for doing this is streamqueue.

Here’s an example:

gulp.task("inject-dependencies", function() {
  var layoutStream = gulp.src("./index.html", { base: "./" });

  var firstStream = gulp.src("something/**/*", { read: false });
  var secondStream = gulp.src("something/**/*", { read: false });
  var thirdStream = gulp.src("something/**/*", { read: false });

  var stream = isRelease ? firstStream : queue({ objectMode: true }, secondStream, thirdStream);

  return layoutStream
    .pipe($.inject(stream, {
      name: "scripts",
      relative: false,
      addRootSlash: false
    }))
    .pipe(gulp.dest("./"));
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you remove "null" values from a Java Stream [duplicate]
Here's a few ways I can think of: stream.filter(x -> x != null). stream.filter(Objects::nonNull).
Read more >
How To: Lipo Injections at Home - Invigor Medical
In order to get the best results from the lipotropic injection, it is recommended that you administer the shot on an empty stomach...
Read more >
Common Questions About Giving Yourself Shots - PeaceHealth
If you need to give yourself injections, or shots, at home, ... If you're bleeding after you remove the needle, apply direct pressure...
Read more >
Empty Nose Syndrome: What It Is, Causes & Treatment
Empty nose syndrome (ENS) is a rare and controversial condition that's most common in people who've had nasal surgery.
Read more >
SQL Injection in Java and How to Easily Prevent it | DigitalOcean
SQL Injection is one of the top 10 web application vulnerabilities. ... They can alter, delete data in your database and take your ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found