I have a module that requires underscore:
import _ from "underscore"
var numbers = [1, 4, 6, 7, 9, 3, 22, 11, 54, 99, 100];
var evens = _.filter(numbers, x => x % 2 === 0);
document.getElementById('output').innerText = evens;
This works fine in my gulp task:
gulp.task('process-scripts', function(cb) {
var builder = new Builder('Scripts', 'Scripts/config.js');
builder.buildStatic('main.js', 'app/main.min.js', { minify: true });
cb();
});
And with this script tag:
<script src="/app/main.min.js"></script>
The problem is, I want to pull underscore out into it's own script. So I changed my gulp task to:
gulp.task('process-scripts', function(cb) {
var builder = new Builder('Scripts', 'Scripts/config.js');
builder.buildStatic('underscore', 'app/vendor.min.js', { minify: true });
builder.buildStatic('main.js - underscore', 'app/main.min.js', { minify: true, globalDeps: {underscore: '_'} });
cb();
});
Doing so, I get underscore pulled out of the main bundle and put into the vendor bundle. At the bottom of main.min.js, the bundle creates this code:
(function(factory) {
factory(_);
});
And I get the error: "Uncaught ReferenceError: _ is not defined"
I tried this same thing using jQuery in place of underscore, and it worked just fine. What I noticed while stepping through the code is that when the code at the bottom of main is called, $
was found on the window object, and so there was no error.
How can I get the main module to recognize underscore in the vendor script?