From cf90154b2336ade199dc1ae9d8b30bb9cd2cd315 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Wed, 22 Jan 2020 11:10:35 -0800 Subject: [PATCH] Support for closure library option in uncompressed local build (#3633) * Support for closure library option in uncompressed local build * Refactor into maybeAddClosureLibrary method --- gulpfile.js | 66 ++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index a77c68f50..ef9fb4148 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -161,16 +161,13 @@ function compile(compilerOptions, opt_verbose, opt_warnings_as_error) { } /** - * This task builds Blockly's core files. - * blockly_compressed.js + * Helper method for possibly adding the closure library into a sources array. + * @param {Array.} srcs */ -gulp.task('build-compressed', function (cb) { - const defines = 'Blockly.VERSION="' + packageJson.version + '"'; - const srcs = ['core/**/**/*.js']; +function maybeAddClosureLibrary(srcs) { if (argv.closureLibrary) { // If you require the google closure library, you can include it in your - // build by running: - // gulp build-compressed --closure-library + // 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.'); @@ -178,9 +175,18 @@ gulp.task('build-compressed', function (cb) { 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'); + srcs.push('./node_modules/google-closure-library/closure/goog/**/**/*.js'); } - return gulp.src(srcs, {base: './'}) + return srcs; +} + +/** + * This task builds Blockly's core files. + * blockly_compressed.js + */ +gulp.task('build-compressed', function (cb) { + const defines = 'Blockly.VERSION="' + packageJson.version + '"'; + return gulp.src(maybeAddClosureLibrary(['core/**/**/*.js']), {base: './'}) // 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 @@ -229,7 +235,7 @@ goog.provide('Blockly.FieldTextInput'); goog.provide('Blockly.FieldVariable'); goog.provide('Blockly.Mutator'); goog.provide('Blockly.Warning');`; - return gulp.src('blocks/*.js', {base: './'}) + return gulp.src(maybeAddClosureLibrary(['blocks/*.js']), {base: './'}) // Add Blockly.Blocks to be compatible with the compiler. .pipe(gulp.replace(`goog.provide('Blockly.Constants.Colour');`, `${provides}goog.provide('Blockly.Constants.Colour');`)) @@ -338,6 +344,9 @@ gulp.task('build-generators', gulp.parallel( * blockly_uncompressed.js */ gulp.task('build-uncompressed', function() { + const closurePath = argv.closureLibrary ? + 'node_modules/google-closure-library/closure/goog' : + 'closure/goog'; const header = `// Do not edit this file; automatically generated by gulp. 'use strict'; @@ -373,42 +382,37 @@ if (this.IS_NODE_JS) { module.exports = Blockly; } else { document.write(''); + '/${closurePath}/base.js">'); document.write(''); } `; let deps = []; -return gulp.src('core/**/**/*.js') +return gulp.src(maybeAddClosureLibrary(['core/**/**/*.js'])) .pipe(through2.obj((file, _enc, cb) => { - deps.push(closureDeps.parser.parseFile(file.path).dependencies[0]); + const result = closureDeps.parser.parseFile(file.path); + for (const dep of result.dependencies) { + deps.push(dep); + } cb(null); })) .on('end', () => { - const graph = new closureDeps.depGraph.Graph(deps); - let addDependency = []; - graph.depsByPath.forEach(dep => { - addDependency.push('goog.addDependency(' + [ - '"' + path.relative('./closure/goog', dep.path) + '"', - '[' + dep.closureSymbols - .map(s => `'${s}'`).join(', ') + ']', - '[' + dep.imports - .map(i => i.symOrPath) - .filter(i => i !== 'goog') - .sort() - .map(i => `'${i}'`).join(', ') + ']', - ].join(', ') + ');'); - }); - const requires = ` -goog.addDependency("base.js", [], []); + // Update the path to closure for any files that we don't know the full path + // of (parsed from a goog.addDependency call). + for (const dep of deps) { + dep.setClosurePath(closurePath); + } + + const addDependency = closureDeps.depFile.getDepFileText(closurePath, deps); + + const requires = `goog.addDependency("base.js", [], []); // Load Blockly. goog.require('Blockly.requires') `; fs.writeFileSync('blockly_uncompressed.js', header + - addDependency.sort((a, b) => - a.localeCompare(b, undefined, {sensitivity: 'base'})).join('\n') + + addDependency + requires + footer); });