diff --git a/.eslintignore b/.eslintignore index fee781489..fb4f528a7 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,6 +4,7 @@ blockly_node_javascript_en.js gulpfile.js /msg/* /core/css.js +/core/utils/global.js /tests/blocks/* /tests/compile/* /tests/jsunit/* diff --git a/core/utils/global.js b/core/utils/global.js index 0f5a2ce17..0903aa437 100644 --- a/core/utils/global.js +++ b/core/utils/global.js @@ -19,7 +19,7 @@ */ /** - * @fileoverview Core utility methods for Blockly + * @fileoverview Provides a reference to the global object. * @author samelh@google.com (Sam El-Husseini) */ 'use strict'; @@ -33,6 +33,19 @@ goog.provide('Blockly.utils.global'); /** * Reference to the global object. + * + * More info on this implementation here: + * https://docs.google.com/document/d/1NAeW4Wk7I7FV0Y2tcUFvQdGMc89k2vdgSXInw8_nvCI/edit */ -Blockly.utils.global = this || self; - +Blockly.utils.global = function() { + if (typeof self === 'object') { + return self; + } + if (typeof window === 'object') { + return window; + } + if (typeof global === 'object') { + return global; + } + return this; +}(); diff --git a/gulpfile.js b/gulpfile.js index cbf032ba9..9a426e7d0 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -509,8 +509,6 @@ function packageCommonJS(namespace, dependencies) { */ gulp.task('package-blockly', function() { return gulp.src('blockly_compressed.js') - .pipe(gulp.insert.prepend(` - var self = this;`)) .pipe(packageUMD('Blockly', [])) .pipe(gulp.rename('blockly.js')) .pipe(gulp.dest(packageDistribution)); @@ -524,9 +522,8 @@ gulp.task('package-blockly', function() { gulp.task('package-blockly-node', function() { // Override textToDomDocument, providing a Node.js alternative to DOMParser. return gulp.src('blockly_compressed.js') - .pipe(gulp.insert.wrap(` - var self = global;`, - `if (typeof DOMParser !== 'function') { + .pipe(gulp.insert.append(` + if (typeof DOMParser !== 'function') { var JSDOM = require('jsdom').JSDOM; var window = (new JSDOM()).window; var document = window.document;