0

I want to inject javascript library inside head tag and my customized and my own code at body using GULP.

For Example:

<html>
    <head>
      <!-- injectJS -->
         <script src="jquery.js">
         <script src="d3.js">
      <!-- injected -->
    </head>

    <body>
      <!-- injectJS -->
         <script src="index.js">
      <!-- injected -->
    </body>
</html> 
3
  • I think you are misunderstanding what GULP is. GULP is just a task-runner. It doesn't directly add html to your pages. You can configure GULP with some plugins to combine javascript files to a head.js and body.js. And then you can use those 2 files instead of adding every dependency file to the DOM. Commented May 6, 2018 at 9:09
  • 1
    Have you looked at gulp-inject for example? Commented May 7, 2018 at 0:29
  • @Mark Thanks, now i went through github.com/klei/gulp-inject Commented May 7, 2018 at 12:07

1 Answer 1

2

I've been using gulp-template to do exactly this. Your index.html would then have to be adjusted. e.g.:

<% scripts.forEach( function ( file ) { %>
    <script type="text/javascript" src="<%= file %>" inline></script>
<% }); %>

In this example your gulpfile task would look something like:

gulp.task('index', () =>
    gulp.src('src/index.html')
        .pipe(template({scripts: ['script.js', 'another-one.js']}))
        .pipe(gulp.dest('dist'))
    );

Another way to achieve this would be using gulp-inject. I prefer gulp-template though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.