From c69a8cd9cc2a6dc3d4434ded1252f25791ffa0cb Mon Sep 17 00:00:00 2001 From: Robert-Frampton Date: Mon, 9 Oct 2017 16:44:55 -0700 Subject: [PATCH] Check to see if bundle has been loaded already. Fixes #273 --- karma-coverage.conf.js | 7 ++++++ karma-sauce.conf.js | 7 ++++++ packages/metal-soy-bundle/gulpfile.js | 8 +++++- packages/metal-soy-bundle/test/bundle.js | 32 ++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 packages/metal-soy-bundle/test/bundle.js diff --git a/karma-coverage.conf.js b/karma-coverage.conf.js index 327cc067..3f37b92d 100644 --- a/karma-coverage.conf.js +++ b/karma-coverage.conf.js @@ -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, @@ -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'] }, diff --git a/karma-sauce.conf.js b/karma-sauce.conf.js index 6615fb81..4ef18283 100644 --- a/karma-sauce.conf.js +++ b/karma-sauce.conf.js @@ -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, @@ -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'] }, diff --git a/packages/metal-soy-bundle/gulpfile.js b/packages/metal-soy-bundle/gulpfile.js index c48d87c3..dd7cd071 100644 --- a/packages/metal-soy-bundle/gulpfile.js +++ b/packages/metal-soy-bundle/gulpfile.js @@ -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' + diff --git a/packages/metal-soy-bundle/test/bundle.js b/packages/metal-soy-bundle/test/bundle.js new file mode 100644 index 00000000..24eabd80 --- /dev/null +++ b/packages/metal-soy-bundle/test/bundle.js @@ -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); +}