Fix Blockly.utils.global when wrapped in a module. (#3035)

* Fix Blockly.utils.global when wrapped in a module.
This commit is contained in:
Sam El-Husseini
2019-09-17 14:07:03 -07:00
committed by GitHub
parent 9004346f7d
commit 99c624db34
3 changed files with 19 additions and 8 deletions

View File

@@ -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/*

View File

@@ -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;
}();

View File

@@ -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;