1

I'm using some standard gulp processes to minify my html, css and javascript. The problem I have at the moment is that i'm concatenating and uglifying my JS into one clean scripts.js file. The distribution folder still points to multiple JS files, for example:

  <script type="text/javascript" src="mock-data-live.js"></script>
  <script type="text/javascript" src="vibrant.min.js></script>
  <script type="text/javascript" src="siema.min.js"></script>
  <script type="text/javascript" src="script.min.js"></script>

Becomes

  <script type="text/javascript" src="script.min.js"></script>

Is there a way I can change my original paths in HTML when minifying so they all point to the one concatenated file?

Thank you

6
  • As the HTML file itself is mostly static and doesn't require rebuilding on change, why don't you statically point the script source to the final script's path <script type="text/javascript" src="script.min.js"></script>? Commented Feb 22, 2018 at 10:32
  • Heya, good point and I did think about this - part of my gulp build at the moment is minifying the HTML as well as the JS, so it gets outputted as part of the process each time I run the default gulp script. Commented Feb 22, 2018 at 10:43
  • I see. using a gulp plugin like in my example below should do the trick. And just a short remark type="text/javascript" can be removed as this would be the default for all script tags. Commented Feb 22, 2018 at 11:16
  • So did you find a solution? If so, could you share it? Commented Mar 10, 2018 at 21:46
  • Whoopsie, missed that - yours was the best answer. Thanks for your help. Commented Mar 13, 2018 at 11:21

2 Answers 2

1

Install gulp-html-replace

You will need an additional Gulp package for this: gulp-html-replace.

Install it via:

npm install --save-dev gulp-html-replace

Change HTML file contents

Inside your HMTL file (assuming its called "index.html"), you would use:

<html>
<head>

<!-- build:js -->
<script type="text/javascript" src="mock-data-live.js"></script>
<script type="text/javascript" src="vibrant.min.js></script>
<script type="text/javascript" src="siema.min.js"></script>
<script type="text/javascript" src="script.min.js"></script>
<!-- endbuild -->

Edit gulpfile.js

Add the build rule to your gulpfile.js:

var gulp = require('gulp');
var htmlreplace = require('gulp-html-replace');

gulp.task('default', function() {
  gulp.src('index.html')
    .pipe(htmlreplace({
        'css': 'styles.min.css',
        'js': 'script.min.js'
    }))
    .pipe(gulp.dest('build/'));
});

Run gulp

As soon as you run gulp, the resulting HTML should look like:

<html>
<head>

<script type="text/javascript" src="script.min.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Amazing, fits very well into my current process :) thanks
Very good! If this should be the solution to your problem, please don't forget to select an answer as solution. Thanks!
1

The usual way is to use something like gulp-processhtml. There are others that do the same thing.

<!-- build:js js/app.min.js -->
<script src="src/js/main.js"></script>
<!-- /build -->

During development, you are using the src/js version. On a build step, you run through a gulp-processhtml step that changes that link to

<script src="js/app.min.js"></script>

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.