wiledal / gulp-include

Enables functionality similar to that of snockets / sprockets or other file insertion compilation tools.
158 stars 68 forks source link

[RangeError: Invalid string length] #43

Open tinyfly opened 9 years ago

tinyfly commented 9 years ago

I'm trying to run gulp-include on a plugins.js that has require statements for all my third-party JS used in my project. I get to the restangular.js line before I get the above error. It isn't specific to restangular because if I put anything after the first 14 requires it'll give me the invalid string error. If I only do the first 14 files it compiles fine.

Here is my gulp task:

gulp.task('js:vendor:app', function() {  
  return gulp.src(paths.src.plugins.app)    
    .pipe(include({
      extensions: 'js'
    }))
      .on('error', console.log)    
    .pipe(gulp.dest(paths.dest.scripts));    
});

Here is my plugin.js file:

// JQuery
//= require ../../../../vendor/bower/**/jquery.js
//= require ../../../../vendor/bower/**/jquery.placeholder.js
//= require ../../../../vendor/bower/**/jquery.easing.js
//= require ../../../../vendor/bower/**/jquery.serializejson.js
//= require ../../../../vendor/bower/**/lodash.js

// AngularJS
//= require ../../../../vendor/bower/**/angular.js
//= require ../../../../vendor/bower/**/angular-ui-router.js
//= require ../../../../vendor/bower/**/angular-scroll.js
//= require ../../../../vendor/bower/**/angular-animate.js
//= require ../../../../vendor/bower/**/angular-sanitize.js
//= require ../../../../vendor/bower/**/angular-aria.js
//= require ../../../../vendor/bower/**/angular-cookies.js
//= require ../../../../vendor/bower/**/angular-resource.js
//= require ../../../../vendor/bower/**/angular-easyfb.js
//= require ../../../../vendor/bower/**/restangular.js

// Angular-Strap
//= require ../../../../vendor/bower/**/angular-strap.js
//= require ../../../../vendor/bower/**/angular-strap.tpl.js

// UI Bootstrap
//= require ../../../../vendor/bower/**/ui-bootstrap-tpls.js

// Animation Framework Dependencies
// require vendor/bower/**/TweenMax.js
// require vendor/bower/**/jquery.gsap.js

// Fast Click Polyfill
//= require ../../../../vendor/bower/**/fastclick.js

// Moment JS
//= require ../../../../vendor/bower/**/moment.js
//= require ../../../../vendor/bower/**/angular-moment.js
//= require ../../../../vendor/bower/**/twix.js

// Mapbox JS
//= require ../../../../vendor/bower/**/mapbox.uncompressed.js
//= require ../../../../vendor/bower/arc/index.js

// Bootstrap JS
//= require ../../../../vendor/bower/**/affix.js
//= require ../../../../vendor/bower/**/alert.js
// require vendor/bower/**/button.js
//= require ../../../../vendor/bower/**/carousel.js
//= require ../../../../vendor/bower/**/collapse.js
//= require ../../../../vendor/bower/**/dropdown.js
//= require ../../../../vendor/bower/**/tab.js
//= require ../../../../vendor/bower/**/transition.js
//= require ../../../../vendor/bower/**/scrollspy.js
//= require ../../../../vendor/bower/**/modal.js
//= require ../../../../vendor/bower/**/tooltip.js
//= require ../../../../vendor/bower/**/popover.js

// deprecated in favor of angular-strap select
//= require ../../../../vendor/bower/**/bootstrap-multiselect.js

//= require ../../../../vendor/bower/**/bootstrap-tour.js

// Twitter Typeahead JS
//= require ../../../../vendor/bower/**/typeahead.bundle.js

// ZeroClipboard
//= require ../../../../vendor/bower/**/ZeroClipboard.js

// jqplot and plugins
//= require ../../../../vendor/bower/**/jquery.jqplot.js
//= require ../../../../vendor/bower/**/jqplot.canvasAxisTickRenderer.js
//= require ../../../../vendor/bower/**/jqplot.cursor.js
//= require ../../../../vendor/bower/**/jqplot.dateAxisRenderer.js
//= require ../../../../vendor/bower/**/jqplot.highlighter.js

// Analytics JS
//= require ../../../../vendor/bower/**/analytics.js
wiledal commented 9 years ago

Oh my, that is one big vendor file. Looking at the amount and nature of the files you are including, I'd say it's actually running out of memory while processing.

This could be an issue with the V8 RegExp as it fails when it reaches a heap of 64mb (https://code.google.com/p/v8/issues/detail?id=3878). As the file grows with the includes, the regex has to work harder. I'll have to look into avoiding performing regex multiple times in an update.

You could try using globs to include more than one file in a single directive.

//= require ../../../../vendor/bower/**/jquery.jqplot.js
//= require ../../../../vendor/bower/**/jqplot.*.js

Since the file grows in larger chunks, it might allow more things to be included before breaking the memory limit. However, it might not do anything in this case since the error happens early.

You could also try using gulp-include version 1.1.1, which handled includes slightly differently, and might be less heavy on huge files.

sapzxc commented 8 years ago

I have similar issue. Js code randomly cuts. I have tried v 1.1.1, but no luck. I using code like this:

return gulp.src('myfile.js')
        .pipe(plugins.rename({suffix:'.dev'}))
        .pipe(plugins.include()) // parse include patterns
        .pipe(gulp.dest('dist'));
sapzxc commented 8 years ago

I found some resolve for my issue. There was file trims in base of 16kb (0 - 16kb - 32kb etc). I debugged 'gulp-include' module and did not found problems there. All files has normal length. But file still trims in random way. The module gulp-sync shutdown script without syncing all streams I suggest. There code with wait function that resolve my issue, but not clear:

return gulp.src('myfile.js')
    .pipe(plugins.rename({suffix:'.dev'}))
    .pipe(plugins.include()) // parse include patterns
    .pipe(gulp.dest('dist'))

    // wait until all file will be written to file system
    .pipe(function(){
        return require('gulp-include/node_modules/event-stream').map(function(file, callback){
            setTimeout(function(){
                callback(null, file);
            },1000);
        });
    }())