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.

Inject into .js file

See original GitHub issue

I’m trying to inject a js dependency into a .js file, so I could lazy load that file later. In my Angular use case it looks like this:

// analytics-module.js that is later get concatinated to app.js
$ocLazyLoad.load({
    name: 'angles',
    files: ['<!-- inject:analytics:js --><!-- endinject -->']
}).then(function() {
    // Show analytics
});

Im trying to achive it with the following task:

// gulpfile.js
gulp.src('_public/js/app*.js')
.pipe(inject(gulp.src(["_public/js/vendor-analytics*.js"], { read: false }), {
    ignorePath: '/_public/',
    starttag: '<!-- inject:analytics:js -->',
    transform: function (filepath, file, i, length) {
        return filepath;
    }
}))
.pipe(gulp.dest("_public/"));

I’m getting the following output: gulp-inject 1 files into app.js., but in reality the placeholder for the file does not get changed to the filepath.

Does injection into .js files is supported? In there a way to debug it somehow?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
joakimbengcommented, Jan 26, 2015

I’m afraid that’s not possible, see #59 .

In your case maybe this can work:

gulp.src('_public/js/app*.js')
.pipe(inject(gulp.src(["_public/js/vendor-analytics*.js"], { read: false }), {
    ignorePath: '/_public/',
    starttag: '/* inject:analytics */ files: [',
    endtag: ']',
    transform: function (filepath, file, i, length) {
        return '\'' + filepath + '\'' + (i + 1 < length ? ', ' : '');
    }
}))
.pipe(gulp.dest("_public/js"));

// and:

$ocLazyLoad.load({
    name: 'angles',
    /* inject:analytics */ files: []
})

Then your files will be injected into the files array, and even comma separate the files if there’s more than one file.

0reactions
dorjoncommented, Apr 20, 2017

Hello I am also facing issue but not similar I want to inject variable to JS file. so how can I achieve this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inject external javascript file in html body after page load
I want to load an external javascript after page ...
Read more >
How to Inject JavaScript Code to Manipulate Websites ...
1. Installing the Extension to Inject the Code ... The following only applies if you use Google Chrome. Install the extension custom JavaScript...
Read more >
Inject Javascript with references to other Js files - UiPath Forum
I have a task to Inject java script into a Hmtl page and execute the java script code which returns the output. HTML...
Read more >
injectJs - PhantomJS
Injects external script code from the specified file into the page (like page.includeJs , except that the file ... Inject do.js file into...
Read more >
klei/gulp-inject: A javascript, stylesheet and ... - GitHub
gulp-inject takes a stream of source files, transforms each file to a string and injects each transformed string into placeholders in the target...
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