Skip to content

Commit

Permalink
fix(build): Fix buildAdvanceCompilationTest
Browse files Browse the repository at this point in the history
- In build_tasks.js:
  - Replace the old compile() function with a new one factored out of
    buildCompiled().
  - Update buildAdvancedCompilationTest to use the new compile()
    and other helpers created in the meantime.
  - Remove no-longer-used maybeAddClosureLibrary().

- Remove externs/{block,generator,goog}-externs.js, which are no longer
  used by any compile pipeline.

- Update core/blockly.js to fix issue with detection of compiled mode
  when using ADVANCED_OPTIMISATIONS.

- Update only other use of globalThis, in core/utils/xml.js, to
  consistently treat it as a dictionary object.

- Update instructions in tests/compile/index.html.

This commit is sort-of-a-prerequisite to google#5602; test:compile:advanced
was previously working but the generated `main_compresed.js` would
throw errors upon loading.
  • Loading branch information
cpcallen committed Nov 26, 2021
1 parent ca23506 commit 526e600
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 217 deletions.
13 changes: 9 additions & 4 deletions core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ goog.require('Blockly.Events.VarCreate');
*/
exports.VERSION = 'uncompiled';

/**
* @define {boolean} Overridden to true by the compiler.
*/
const COMPILED = false;

// Add a getter and setter pair for Blockly.alert, Blockly.confirm,
// Blockly.mainWorkspace, Blockly.prompt and Blockly.selected for backwards
// compatibility.
Expand Down Expand Up @@ -700,22 +705,22 @@ exports.zelos = zelos;
//
// This is only needed in uncompiled mode (see
// google/blockly-samples#902); in compiled mode the exports object is
// already the value of globalThis.Blockly.
// already the value of globalThis['Blockly'].
//
// Note that this code will still attempt to redefine accessors on a
// previously-imported copy of the Blockly library if both are
// imported in uncompiled mode. This will fail with TypeError as the
// accessors are nonconfigurable (which is good, as otherwise one
// accessors on one copy would call get/set functions on the other
// copy!)
if (!goog.global['COMPILED'] && typeof goog.global['Blockly'] === 'object' &&
goog.global['Blockly'] !== exports) {
if (!COMPILED && typeof globalThis['Blockly'] === 'object' &&
globalThis['Blockly'] !== exports) {
const descriptors = Object.getOwnPropertyDescriptors(exports);
const accessors = {};
for (const key in descriptors) {
if (descriptors[key].get || descriptors[key].set) {
accessors[key] = descriptors[key];
}
}
Object.defineProperties(globalThis.Blockly, accessors);
Object.defineProperties(globalThis['Blockly'], accessors);
}
2 changes: 1 addition & 1 deletion core/utils/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports.NAME_SPACE = NAME_SPACE;
* jsdom package instead.
* @type {!Document}
*/
let xmlDocument = globalThis.document;
let xmlDocument = globalThis['document'];

/**
* Get the document object to use for XML serialization.
Expand Down
28 changes: 0 additions & 28 deletions externs/block-externs.js

This file was deleted.

20 changes: 0 additions & 20 deletions externs/generator-externs.js

This file was deleted.

66 changes: 0 additions & 66 deletions externs/goog-externs.js

This file was deleted.

139 changes: 44 additions & 95 deletions scripts/gulpfiles/build_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,56 +183,6 @@ var JSCOMP_ERROR = [
'visibility'
];

/**
* Helper method for calling the Closure compiler.
* @param {*} compilerOptions
* @param {boolean=} opt_verbose Optional option for verbose logging
* @param {boolean=} opt_warnings_as_error Optional option for treating warnings
* as errors.
* @param {boolean=} opt_strict_typechecker Optional option for enabling strict
* type checking.
*/
function compile(compilerOptions, opt_verbose, opt_warnings_as_error,
opt_strict_typechecker) {
const options = {};
options.compilation_level = 'SIMPLE_OPTIMIZATIONS';
options.warning_level = opt_verbose ? 'VERBOSE' : 'DEFAULT';
options.language_in = 'ECMASCRIPT6_STRICT',
options.language_out = 'ECMASCRIPT5_STRICT';
options.rewrite_polyfills = true;
options.hide_warnings_for = 'node_modules';
if (opt_warnings_as_error || opt_strict_typechecker) {
options.jscomp_error = JSCOMP_ERROR;
if (opt_strict_typechecker) {
options.jscomp_error.push('strictCheckTypes');
}
}

const platform = ['native', 'java', 'javascript'];

return closureCompiler({...options, ...compilerOptions}, { platform });
}

/**
* Helper method for possibly adding the Closure library into a sources array.
* @param {Array<string>} srcs
*/
function maybeAddClosureLibrary(srcs) {
if (argv.closureLibrary) {
// If you require Google's Closure library, you can include it in your
// build by adding the --closure-library flag.
// You will also need to include the "google-closure-library" in your list
// of devDependencies.
console.log('Including the google-closure-library in your build.');
if (!fs.existsSync('./node_modules/google-closure-library')) {
throw Error('You must add the google-closure-library to your ' +
'devDependencies in package.json, and run `npm install`.');
}
srcs.push('./node_modules/google-closure-library/closure/goog/**/**/*.js');
}
return srcs;
}

/**
* This task updates tests/deps.js, used by blockly_uncompressed.js
* when loading Blockly in uncompiled mode.
Expand Down Expand Up @@ -493,6 +443,34 @@ function unflattenCorePaths(pathString) {
return pathString.replace(/-slash-/g, path.sep);
}

/**
* Helper method for calling the Closure compiler, establishing
* default options (that can be overridden by the caller).
* @param {*} options Caller-supplied options that will override the
* defaultOptions.
*/
function compile(options) {
const defaultOptions = {
compilation_level: 'SIMPLE_OPTIMIZATIONS',
warning_level: argv.verbose ? 'VERBOSE' : 'DEFAULT',
language_in: 'ECMASCRIPT6_STRICT',
language_out: 'ECMASCRIPT5_STRICT',
rewrite_polyfills: true,
hide_warnings_for: 'node_modules',
externs: ['./externs/svg-externs.js'],
};
if (argv.debug || argv.strict) {
defaultOptions.jscomp_error = [...JSCOMP_ERROR];
if (argv.strict) {
defaultOptions.jscomp_error.push('strictCheckTypes');
}
}
// Extra options for Closure Compiler gulp plugin.
const platform = ['native', 'java', 'javascript'];

return closureCompiler({...defaultOptions, ...options}, {platform});
}

/**
* This task compiles the core library, blocks and generators, creating
* blockly_compressed.js, blocks_compressed.js, etc.
Expand All @@ -505,35 +483,20 @@ function buildCompiled() {
// Closure Compiler options.
const packageJson = getPackageJson(); // For version number.
const options = {
compilation_level: 'SIMPLE_OPTIMIZATIONS',
warning_level: argv.verbose ? 'VERBOSE' : 'DEFAULT',
language_in: 'ECMASCRIPT6_STRICT',
language_out: 'ECMASCRIPT5_STRICT',
rewrite_polyfills: true,
hide_warnings_for: 'node_modules',
externs: ['./externs/svg-externs.js'],
define: 'Blockly.VERSION="' + packageJson.version + '"',
chunk: chunkOptions.chunk,
chunk_wrapper: chunkOptions.chunk_wrapper,
rename_prefix_namespace: NAMESPACE_OBJECT,
// Don't supply the list of source files in chunkOptions.js as an
// option to Closure Compiler; instead feed them as input via gulp.src.
};
if (argv.debug || argv.strict) {
options.jscomp_error = [...JSCOMP_ERROR];
if (argv.strict) {
options.jscomp_error.push('strictCheckTypes');
}
}
// Extra options for Closure Compiler gulp plugin.
const pluginOptions = ['native', 'java', 'javascript'];

// Fire up compilation pipline.
return gulp.src(chunkOptions.js, {base: './'})
.pipe(stripApacheLicense())
.pipe(gulp.sourcemaps.init())
.pipe(gulp.rename(flattenCorePaths))
.pipe(closureCompiler(options, pluginOptions))
.pipe(compile(options))
.pipe(gulp.rename({suffix: COMPILED_SUFFIX}))
.pipe(gulp.sourcemaps.mapSources(unflattenCorePaths))
.pipe(
Expand All @@ -547,38 +510,24 @@ function buildCompiled() {
*/
function buildAdvancedCompilationTest() {
const srcs = [
'tests/compile/main.js', 'tests/compile/test_blocks.js', 'core/**/**/*.js',
'blocks/*.js', 'generators/**/*.js'
'closure/goog/base_minimal.js',
'core/**/*.js', 'blocks/**/*.js', 'generators/**/*.js',
'tests/compile/main.js', 'tests/compile/test_blocks.js',
];
return gulp.src(maybeAddClosureLibrary(srcs), {base: './'})

// Closure Compiler options.
const options = {
dependency_mode: 'PRUNE',
compilation_level: 'ADVANCED_OPTIMIZATIONS',
entry_point: './tests/compile/main.js',
js_output_file: 'main_compressed.js',
};
return gulp.src(srcs, {base: './'})
.pipe(stripApacheLicense())
.pipe(gulp.sourcemaps.init())
// Directories in Blockly are used to group similar files together
// but are not used to limit access with @package, instead the
// method means something is internal to Blockly and not a public
// API.
// Flatten all files so they're in the same directory, but ensure that
// files with the same name don't conflict.
.pipe(gulp.rename(function(p) {
if (p.dirname.indexOf('core') === 0) {
var dirname = p.dirname.replace(
new RegExp(path.sep.replace(/\\/, '\\\\'), "g"), "-");
p.dirname = "";
p.basename = dirname + "-" + p.basename;
}
}))
.pipe(compile(
{
dependency_mode: 'PRUNE',
compilation_level: 'ADVANCED_OPTIMIZATIONS',
entry_point: './tests/compile/main.js',
js_output_file: 'main_compressed.js',
externs: ['./externs/svg-externs.js', './externs/goog-externs.js'],
},
argv.verbose, argv.strict))
.pipe(gulp.sourcemaps.mapSources(function(sourcePath, file) {
return sourcePath.replace(/-/g, '/');
}))
.pipe(gulp.rename(flattenCorePaths))
.pipe(compile(options))
.pipe(gulp.sourcemaps.mapSources(unflattenCorePaths))
.pipe(gulp.sourcemaps.write(
'.', {includeContent: false, sourceRoot: '../../'}))
.pipe(gulp.dest('./tests/compile/'));
Expand Down
5 changes: 2 additions & 3 deletions tests/compile/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
<body>
<h1>Blockly: Advanced Compilation Test</h1>

<p>To run this test manually, download
<a href="https://dl.google.com/closure-compiler/compiler-latest.zip">closure-compiler-vxxxxxxxx.jar</a>,
place it in this directory, then run `npm run test:compile:advanced` from the command line.</p>
<p>To run this test manually, run `npm run test:compile:advanced`
from the command line, then open this file in your web browser.</p>

<p>Measure the size of main_compressed.js (295kb as of October 2017), then reload
this page and see if Blockly works.</p>
Expand Down

0 comments on commit 526e600

Please sign in to comment.