save inject svg file when changing html document












12















I have a some front-end project with workflow in gulp.



Plugins:




  • gulp-inject

  • browsersync

  • gulp-watch

  • gulp-useref


gulpfile.js:



var 
gulp = require('gulp'),
...

var path = {
app : { // src
html : 'app/*.html',
js : 'app/js/*.js',
svg : 'app/**/*.svg',
},
dist : { // dist
html : 'dist/',
js : 'dist/js/',
},
watch : { // watcher
html : 'app/**/*.html',
svg : 'app/**/*.svg',
js : 'app/js/**/*.js',
}
};

// server
var config = {
server : {
'baseDir' : './dist'
},
host : 'localhost',
port : 9000,
tunel : true,
};

// HTML
gulp.task('html', ['svg'], function(){
gulp.src(path.app.html)
.pipe(rigger())
.pipe(useref())
.pipe(gulpif('*.js', uglify()))
.pipe(gulpif('*.css', minifyCss()))
.pipe(inject(svgContent, { transform: fileContents }))
.pipe(gulp.dest(path.dist.html))
.pipe(reload({stream : true}));
});

// JS
gulp.task('js', function(){
gulp.src(path.app.js)
.pipe(gulp.dest(path.dist.js))
.pipe(reload({stream : true}));
});

// SVG
function fileContents (filePath, file) {
return file.contents.toString();}

var svgContent = gulp.src(path.app.svg)
.pipe(svgmin())
.pipe(svgstore({ inlineSvg: true }))
.pipe(reload({stream : true}));

gulp.task('svg', function () {
var svgs = svgContent;
});



// watcher
gulp.task('watch', function () {
watch([path.watch.html], function(event, cb){
gulp.start('html');
});
watch([path.watch.html], function(event, cb){
gulp.start('svg');
});
watch([path.watch.js], function(event, cb){
gulp.start('js');
});
});

// start server
gulp.task('webserver', function(){
browserSync(config);
});

// Cleaning
gulp.task('clean', function(cb){
clean(path.clean, cb);
});

// Default task
gulp.task('default', [
'html',
'svg',
'js',
'webserver',
'watch'
]);


html:



...
<div style="height: 0; width: 0; position: absolute; visibility: hidden">
<!-- inject:svg --><!-- endinject -->
</div>
...

<!-- build:js js/vendor.js -->
<script src="libs/jquery/jquery-2.1.3.min.js"></script>
...
<!-- endbuild -->


Everything works well, before the changes in the html file. Any change causes the compiled inline svg file to disappear inside the html file (inside the inject).





I try it code:



gulpfile.js:



// html
gulp.task('html', function(){
gulp.src(path.app.html)
.pipe(useref())
.pipe(gulpif('*.js', uglify()))
.pipe(gulpif('*.css', minifyCss()))
.pipe(gulp.dest(path.dist.html))
.pipe(reload({stream : true}));
});

// SVG
gulp.task('svg', function () {

var svgs = gulp.src(path.app.svg)
.pipe(svgstore({ inlineSvg: true }));

function fileContents (filePath, file) {
return file.contents.toString();
}

return gulp.src(path.app.html)
.pipe(inject(svgs, { transform: fileContents }))
.pipe(gulp.dest(path.dist.other));
});


Everything works, the web server is restarted & cleared with changes to any html file and save svg sprite, BUT the task useref() stopped working.



<!-- build:js js/vendor.js -->  
<script src="libs/jquery/jquery-3.2.1.min.js"></script>
<script src="libs/animate/wow.min.js"></script>
....
<!-- endbuild -->


Build vendor.js file, but does not write to dist/index.html. And after generation should be:



<script src="js/vendor.js"></script>




Question: Is it possible to configure gulpfile.js so that you can make changes to the html file without restarting the entire assembly in the console and without losing the generated svg sprite inside the html file?










share|improve this question





























    12















    I have a some front-end project with workflow in gulp.



    Plugins:




    • gulp-inject

    • browsersync

    • gulp-watch

    • gulp-useref


    gulpfile.js:



    var 
    gulp = require('gulp'),
    ...

    var path = {
    app : { // src
    html : 'app/*.html',
    js : 'app/js/*.js',
    svg : 'app/**/*.svg',
    },
    dist : { // dist
    html : 'dist/',
    js : 'dist/js/',
    },
    watch : { // watcher
    html : 'app/**/*.html',
    svg : 'app/**/*.svg',
    js : 'app/js/**/*.js',
    }
    };

    // server
    var config = {
    server : {
    'baseDir' : './dist'
    },
    host : 'localhost',
    port : 9000,
    tunel : true,
    };

    // HTML
    gulp.task('html', ['svg'], function(){
    gulp.src(path.app.html)
    .pipe(rigger())
    .pipe(useref())
    .pipe(gulpif('*.js', uglify()))
    .pipe(gulpif('*.css', minifyCss()))
    .pipe(inject(svgContent, { transform: fileContents }))
    .pipe(gulp.dest(path.dist.html))
    .pipe(reload({stream : true}));
    });

    // JS
    gulp.task('js', function(){
    gulp.src(path.app.js)
    .pipe(gulp.dest(path.dist.js))
    .pipe(reload({stream : true}));
    });

    // SVG
    function fileContents (filePath, file) {
    return file.contents.toString();}

    var svgContent = gulp.src(path.app.svg)
    .pipe(svgmin())
    .pipe(svgstore({ inlineSvg: true }))
    .pipe(reload({stream : true}));

    gulp.task('svg', function () {
    var svgs = svgContent;
    });



    // watcher
    gulp.task('watch', function () {
    watch([path.watch.html], function(event, cb){
    gulp.start('html');
    });
    watch([path.watch.html], function(event, cb){
    gulp.start('svg');
    });
    watch([path.watch.js], function(event, cb){
    gulp.start('js');
    });
    });

    // start server
    gulp.task('webserver', function(){
    browserSync(config);
    });

    // Cleaning
    gulp.task('clean', function(cb){
    clean(path.clean, cb);
    });

    // Default task
    gulp.task('default', [
    'html',
    'svg',
    'js',
    'webserver',
    'watch'
    ]);


    html:



    ...
    <div style="height: 0; width: 0; position: absolute; visibility: hidden">
    <!-- inject:svg --><!-- endinject -->
    </div>
    ...

    <!-- build:js js/vendor.js -->
    <script src="libs/jquery/jquery-2.1.3.min.js"></script>
    ...
    <!-- endbuild -->


    Everything works well, before the changes in the html file. Any change causes the compiled inline svg file to disappear inside the html file (inside the inject).





    I try it code:



    gulpfile.js:



    // html
    gulp.task('html', function(){
    gulp.src(path.app.html)
    .pipe(useref())
    .pipe(gulpif('*.js', uglify()))
    .pipe(gulpif('*.css', minifyCss()))
    .pipe(gulp.dest(path.dist.html))
    .pipe(reload({stream : true}));
    });

    // SVG
    gulp.task('svg', function () {

    var svgs = gulp.src(path.app.svg)
    .pipe(svgstore({ inlineSvg: true }));

    function fileContents (filePath, file) {
    return file.contents.toString();
    }

    return gulp.src(path.app.html)
    .pipe(inject(svgs, { transform: fileContents }))
    .pipe(gulp.dest(path.dist.other));
    });


    Everything works, the web server is restarted & cleared with changes to any html file and save svg sprite, BUT the task useref() stopped working.



    <!-- build:js js/vendor.js -->  
    <script src="libs/jquery/jquery-3.2.1.min.js"></script>
    <script src="libs/animate/wow.min.js"></script>
    ....
    <!-- endbuild -->


    Build vendor.js file, but does not write to dist/index.html. And after generation should be:



    <script src="js/vendor.js"></script>




    Question: Is it possible to configure gulpfile.js so that you can make changes to the html file without restarting the entire assembly in the console and without losing the generated svg sprite inside the html file?










    share|improve this question



























      12












      12








      12


      1






      I have a some front-end project with workflow in gulp.



      Plugins:




      • gulp-inject

      • browsersync

      • gulp-watch

      • gulp-useref


      gulpfile.js:



      var 
      gulp = require('gulp'),
      ...

      var path = {
      app : { // src
      html : 'app/*.html',
      js : 'app/js/*.js',
      svg : 'app/**/*.svg',
      },
      dist : { // dist
      html : 'dist/',
      js : 'dist/js/',
      },
      watch : { // watcher
      html : 'app/**/*.html',
      svg : 'app/**/*.svg',
      js : 'app/js/**/*.js',
      }
      };

      // server
      var config = {
      server : {
      'baseDir' : './dist'
      },
      host : 'localhost',
      port : 9000,
      tunel : true,
      };

      // HTML
      gulp.task('html', ['svg'], function(){
      gulp.src(path.app.html)
      .pipe(rigger())
      .pipe(useref())
      .pipe(gulpif('*.js', uglify()))
      .pipe(gulpif('*.css', minifyCss()))
      .pipe(inject(svgContent, { transform: fileContents }))
      .pipe(gulp.dest(path.dist.html))
      .pipe(reload({stream : true}));
      });

      // JS
      gulp.task('js', function(){
      gulp.src(path.app.js)
      .pipe(gulp.dest(path.dist.js))
      .pipe(reload({stream : true}));
      });

      // SVG
      function fileContents (filePath, file) {
      return file.contents.toString();}

      var svgContent = gulp.src(path.app.svg)
      .pipe(svgmin())
      .pipe(svgstore({ inlineSvg: true }))
      .pipe(reload({stream : true}));

      gulp.task('svg', function () {
      var svgs = svgContent;
      });



      // watcher
      gulp.task('watch', function () {
      watch([path.watch.html], function(event, cb){
      gulp.start('html');
      });
      watch([path.watch.html], function(event, cb){
      gulp.start('svg');
      });
      watch([path.watch.js], function(event, cb){
      gulp.start('js');
      });
      });

      // start server
      gulp.task('webserver', function(){
      browserSync(config);
      });

      // Cleaning
      gulp.task('clean', function(cb){
      clean(path.clean, cb);
      });

      // Default task
      gulp.task('default', [
      'html',
      'svg',
      'js',
      'webserver',
      'watch'
      ]);


      html:



      ...
      <div style="height: 0; width: 0; position: absolute; visibility: hidden">
      <!-- inject:svg --><!-- endinject -->
      </div>
      ...

      <!-- build:js js/vendor.js -->
      <script src="libs/jquery/jquery-2.1.3.min.js"></script>
      ...
      <!-- endbuild -->


      Everything works well, before the changes in the html file. Any change causes the compiled inline svg file to disappear inside the html file (inside the inject).





      I try it code:



      gulpfile.js:



      // html
      gulp.task('html', function(){
      gulp.src(path.app.html)
      .pipe(useref())
      .pipe(gulpif('*.js', uglify()))
      .pipe(gulpif('*.css', minifyCss()))
      .pipe(gulp.dest(path.dist.html))
      .pipe(reload({stream : true}));
      });

      // SVG
      gulp.task('svg', function () {

      var svgs = gulp.src(path.app.svg)
      .pipe(svgstore({ inlineSvg: true }));

      function fileContents (filePath, file) {
      return file.contents.toString();
      }

      return gulp.src(path.app.html)
      .pipe(inject(svgs, { transform: fileContents }))
      .pipe(gulp.dest(path.dist.other));
      });


      Everything works, the web server is restarted & cleared with changes to any html file and save svg sprite, BUT the task useref() stopped working.



      <!-- build:js js/vendor.js -->  
      <script src="libs/jquery/jquery-3.2.1.min.js"></script>
      <script src="libs/animate/wow.min.js"></script>
      ....
      <!-- endbuild -->


      Build vendor.js file, but does not write to dist/index.html. And after generation should be:



      <script src="js/vendor.js"></script>




      Question: Is it possible to configure gulpfile.js so that you can make changes to the html file without restarting the entire assembly in the console and without losing the generated svg sprite inside the html file?










      share|improve this question
















      I have a some front-end project with workflow in gulp.



      Plugins:




      • gulp-inject

      • browsersync

      • gulp-watch

      • gulp-useref


      gulpfile.js:



      var 
      gulp = require('gulp'),
      ...

      var path = {
      app : { // src
      html : 'app/*.html',
      js : 'app/js/*.js',
      svg : 'app/**/*.svg',
      },
      dist : { // dist
      html : 'dist/',
      js : 'dist/js/',
      },
      watch : { // watcher
      html : 'app/**/*.html',
      svg : 'app/**/*.svg',
      js : 'app/js/**/*.js',
      }
      };

      // server
      var config = {
      server : {
      'baseDir' : './dist'
      },
      host : 'localhost',
      port : 9000,
      tunel : true,
      };

      // HTML
      gulp.task('html', ['svg'], function(){
      gulp.src(path.app.html)
      .pipe(rigger())
      .pipe(useref())
      .pipe(gulpif('*.js', uglify()))
      .pipe(gulpif('*.css', minifyCss()))
      .pipe(inject(svgContent, { transform: fileContents }))
      .pipe(gulp.dest(path.dist.html))
      .pipe(reload({stream : true}));
      });

      // JS
      gulp.task('js', function(){
      gulp.src(path.app.js)
      .pipe(gulp.dest(path.dist.js))
      .pipe(reload({stream : true}));
      });

      // SVG
      function fileContents (filePath, file) {
      return file.contents.toString();}

      var svgContent = gulp.src(path.app.svg)
      .pipe(svgmin())
      .pipe(svgstore({ inlineSvg: true }))
      .pipe(reload({stream : true}));

      gulp.task('svg', function () {
      var svgs = svgContent;
      });



      // watcher
      gulp.task('watch', function () {
      watch([path.watch.html], function(event, cb){
      gulp.start('html');
      });
      watch([path.watch.html], function(event, cb){
      gulp.start('svg');
      });
      watch([path.watch.js], function(event, cb){
      gulp.start('js');
      });
      });

      // start server
      gulp.task('webserver', function(){
      browserSync(config);
      });

      // Cleaning
      gulp.task('clean', function(cb){
      clean(path.clean, cb);
      });

      // Default task
      gulp.task('default', [
      'html',
      'svg',
      'js',
      'webserver',
      'watch'
      ]);


      html:



      ...
      <div style="height: 0; width: 0; position: absolute; visibility: hidden">
      <!-- inject:svg --><!-- endinject -->
      </div>
      ...

      <!-- build:js js/vendor.js -->
      <script src="libs/jquery/jquery-2.1.3.min.js"></script>
      ...
      <!-- endbuild -->


      Everything works well, before the changes in the html file. Any change causes the compiled inline svg file to disappear inside the html file (inside the inject).





      I try it code:



      gulpfile.js:



      // html
      gulp.task('html', function(){
      gulp.src(path.app.html)
      .pipe(useref())
      .pipe(gulpif('*.js', uglify()))
      .pipe(gulpif('*.css', minifyCss()))
      .pipe(gulp.dest(path.dist.html))
      .pipe(reload({stream : true}));
      });

      // SVG
      gulp.task('svg', function () {

      var svgs = gulp.src(path.app.svg)
      .pipe(svgstore({ inlineSvg: true }));

      function fileContents (filePath, file) {
      return file.contents.toString();
      }

      return gulp.src(path.app.html)
      .pipe(inject(svgs, { transform: fileContents }))
      .pipe(gulp.dest(path.dist.other));
      });


      Everything works, the web server is restarted & cleared with changes to any html file and save svg sprite, BUT the task useref() stopped working.



      <!-- build:js js/vendor.js -->  
      <script src="libs/jquery/jquery-3.2.1.min.js"></script>
      <script src="libs/animate/wow.min.js"></script>
      ....
      <!-- endbuild -->


      Build vendor.js file, but does not write to dist/index.html. And after generation should be:



      <script src="js/vendor.js"></script>




      Question: Is it possible to configure gulpfile.js so that you can make changes to the html file without restarting the entire assembly in the console and without losing the generated svg sprite inside the html file?







      javascript html gulp svg-sprite






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 at 13:53







      HamSter

















      asked Jan 2 at 7:43









      HamSterHamSter

      4031926




      4031926
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002844%2fsave-inject-svg-file-when-changing-html-document%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002844%2fsave-inject-svg-file-when-changing-html-document%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Callistus III

          Ostreoida

          Plistias Cous