Skip to content

Commit

Permalink
Check to see if bundle has been loaded already. Fixes #273
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Frampton authored and Robert-Frampton committed Oct 11, 2017
1 parent cfe20c6 commit bf01bee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
7 changes: 7 additions & 0 deletions karma-coverage.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ module.exports = function(config) {
included: true,
served: true
},
{
pattern: 'packages/metal-soy-bundle/build/bundle.js',
watched: false,
included: false,
served: true
},
{
pattern: 'packages/metal-web-component/node_modules/babel-polyfill/dist/polyfill.min.js',
watched: false,
Expand Down Expand Up @@ -65,6 +71,7 @@ module.exports = function(config) {
'packages/metal-incremental-dom/src/incremental-dom.js': ['browserify'],
'packages/metal-incremental-dom/lib/incremental-dom.js': ['browserify'],
'packages/metal-soy-bundle/lib/bundle.js': ['browserify'],
'packages/metal-soy-bundle/build/bundle.js': ['browserify'],
'packages/metal*/test/**/*.js': ['browserify']
},

Expand Down
7 changes: 7 additions & 0 deletions karma-sauce.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ module.exports = function(config) {
included: true,
served: true
},
{
pattern: 'packages/metal-soy-bundle/build/bundle.js',
watched: false,
included: false,
served: true
},
{
pattern: 'packages/metal-web-component/node_modules/babel-polyfill/dist/polyfill.min.js',
watched: false,
Expand Down Expand Up @@ -65,6 +71,7 @@ module.exports = function(config) {
'packages/metal-incremental-dom/src/incremental-dom.js': ['browserify'],
'packages/metal-incremental-dom/lib/incremental-dom.js': ['browserify'],
'packages/metal-soy/node_modules/metal-soy-bundle/lib/bundle.js': ['browserify'],
'packages/metal-soy-bundle/build/bundle.js': ['browserify'],
'packages/metal*/test/**/*.js': ['browserify']
},

Expand Down
8 changes: 7 additions & 1 deletion packages/metal-soy-bundle/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ gulp.task('build', function() {
return gulp.src(dependencies)
.pipe(concat('bundle.js'))
.pipe(replace('var goog = goog || {};', 'var goog = this.goog || {};'))
.pipe(header('import \'metal-incremental-dom\';\n\n(function() {\nthis.CLOSURE_NO_DEPS = true;\nthis.goog = this.goog || {};\n\n'))
.pipe(header('import \'metal-incremental-dom\';\n\n(function() {\nthis.CLOSURE_NO_DEPS = true;\nthis.goog = this.goog || {};\n\n' +
'if (this.__METAL_SOY_BUNDLE_LOADED__) {\n' +
' console.warn(\'Warning: metal-soy-bundle has already been loaded. Dedupe bundle to remove this warning.\');\n' +
' return;\n' +
'}\n' +
'this.__METAL_SOY_BUNDLE_LOADED__ = true;\n\n'
))
.pipe(footer('\n\ngoog.loadModule(function() {\n' +
' goog.module(\'incrementaldom\');\n' +
' return IncrementalDOM;\n' +
Expand Down
32 changes: 32 additions & 0 deletions packages/metal-soy-bundle/test/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
describe('metal-soy-bundle', function() {
before(function() {
sinon.stub(console, 'warn');
});

after(function() {
console.warn.restore();
});

it('should show warning instead of erroring when loading bundle twice', function(done) {
assert(window.__METAL_SOY_BUNDLE_LOADED__);
assert.equal(console.warn.callCount, 0);

loadBundle(function() {
assert.equal(console.warn.callCount, 1);
assert(console.warn.calledWith('Warning: metal-soy-bundle has already been loaded. Dedupe bundle to remove this warning.'));

done();
});
});
});

function loadBundle(done) {
var script = document.createElement('script');

script.crossOrigin = 'anonymous';
script.onload = done;
script.src = '/base/packages/metal-soy-bundle/build/bundle.js';
script.type = 'text/javascript';

document.body.appendChild(script);
}

0 comments on commit bf01bee

Please sign in to comment.