Support for closure library option in uncompressed local build (#3633)

* Support for closure library option in uncompressed local build

* Refactor into maybeAddClosureLibrary method
This commit is contained in:
Sam El-Husseini
2020-01-22 11:10:35 -08:00
committed by GitHub
parent 58b690728d
commit cf90154b23

View File

@@ -161,16 +161,13 @@ function compile(compilerOptions, opt_verbose, opt_warnings_as_error) {
} }
/** /**
* This task builds Blockly's core files. * Helper method for possibly adding the closure library into a sources array.
* blockly_compressed.js * @param {Array.<string>} srcs
*/ */
gulp.task('build-compressed', function (cb) { function maybeAddClosureLibrary(srcs) {
const defines = 'Blockly.VERSION="' + packageJson.version + '"';
const srcs = ['core/**/**/*.js'];
if (argv.closureLibrary) { if (argv.closureLibrary) {
// If you require the google closure library, you can include it in your // If you require the google closure library, you can include it in your
// build by running: // build by adding the --closure-library flag.
// gulp build-compressed --closure-library
// You will also need to include the "google-closure-library" in your list // You will also need to include the "google-closure-library" in your list
// of devDependencies. // of devDependencies.
console.log('Including the google-closure-library in your build.'); 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 ' + throw Error('You must add the google-closure-library to your ' +
'devDependencies in package.json, and run `npm install`.'); '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 // Directories in Blockly are used to group similar files together
// but are not used to limit access with @package, instead the // but are not used to limit access with @package, instead the
// method means something is internal to Blockly and not a public // 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.FieldVariable');
goog.provide('Blockly.Mutator'); goog.provide('Blockly.Mutator');
goog.provide('Blockly.Warning');`; 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. // Add Blockly.Blocks to be compatible with the compiler.
.pipe(gulp.replace(`goog.provide('Blockly.Constants.Colour');`, .pipe(gulp.replace(`goog.provide('Blockly.Constants.Colour');`,
`${provides}goog.provide('Blockly.Constants.Colour');`)) `${provides}goog.provide('Blockly.Constants.Colour');`))
@@ -338,6 +344,9 @@ gulp.task('build-generators', gulp.parallel(
* blockly_uncompressed.js * blockly_uncompressed.js
*/ */
gulp.task('build-uncompressed', function() { 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. const header = `// Do not edit this file; automatically generated by gulp.
'use strict'; 'use strict';
@@ -373,42 +382,37 @@ if (this.IS_NODE_JS) {
module.exports = Blockly; module.exports = Blockly;
} else { } else {
document.write('<script src="' + this.BLOCKLY_DIR + document.write('<script src="' + this.BLOCKLY_DIR +
'/closure/goog/base.js"></script>'); '/${closurePath}/base.js"></script>');
document.write('<script>this.BLOCKLY_BOOT(this);</script>'); document.write('<script>this.BLOCKLY_BOOT(this);</script>');
} }
`; `;
let deps = []; let deps = [];
return gulp.src('core/**/**/*.js') return gulp.src(maybeAddClosureLibrary(['core/**/**/*.js']))
.pipe(through2.obj((file, _enc, cb) => { .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); cb(null);
})) }))
.on('end', () => { .on('end', () => {
const graph = new closureDeps.depGraph.Graph(deps); // Update the path to closure for any files that we don't know the full path
let addDependency = []; // of (parsed from a goog.addDependency call).
graph.depsByPath.forEach(dep => { for (const dep of deps) {
addDependency.push('goog.addDependency(' + [ dep.setClosurePath(closurePath);
'"' + path.relative('./closure/goog', dep.path) + '"', }
'[' + dep.closureSymbols
.map(s => `'${s}'`).join(', ') + ']', const addDependency = closureDeps.depFile.getDepFileText(closurePath, deps);
'[' + dep.imports
.map(i => i.symOrPath) const requires = `goog.addDependency("base.js", [], []);
.filter(i => i !== 'goog')
.sort()
.map(i => `'${i}'`).join(', ') + ']',
].join(', ') + ');');
});
const requires = `
goog.addDependency("base.js", [], []);
// Load Blockly. // Load Blockly.
goog.require('Blockly.requires') goog.require('Blockly.requires')
`; `;
fs.writeFileSync('blockly_uncompressed.js', fs.writeFileSync('blockly_uncompressed.js',
header + header +
addDependency.sort((a, b) => addDependency +
a.localeCompare(b, undefined, {sensitivity: 'base'})).join('\n') +
requires + requires +
footer); footer);
}); });