Skip to content

Commit

Permalink
Use modern JS tooling instead of Closure's deprecated Python scripts. (
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed Jun 1, 2021
1 parent 3069f82 commit fd896ba
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 26 deletions.
7 changes: 2 additions & 5 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,8 @@ statements like:

var message = proto.my.package.MyMessage();

If unfamiliar with Closure or its compiler, consider reviewing Closure documentation
https://developers.google.com/closure/library/docs/tutorial
https://developers.google.com/closure/library/docs/closurebuilder
https://developers.google.com/closure/library/docs/depswriter
At a high level, closurebuilder.py can walk dependencies, and compile your code, and all dependencies for Protobuf into a single .js file. Using depsbuilder.py to generate a dependency file can also be considered for non-production dev environments.
If unfamiliar with Closure or its compiler, consider reviewing
[Closure documentation](https://developers.google.com/closure/library).

CommonJS imports
----------------
Expand Down
2 changes: 0 additions & 2 deletions js/commonjs/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* the google-protobuf.js file that we build at distribution time.
*/

// Include a dummy provide statement so that closurebuilder.py does not skip over this
// file.
goog.provide('jspb.Export');

goog.require('goog.object');
Expand Down
2 changes: 0 additions & 2 deletions js/commonjs/export_asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* closure_asserts_commonjs.js that is only used at testing time.
*/

// Include a dummy provide statement so that closurebuilder.py does not skip over this
// file.
goog.provide('jspb.ExportAsserts');

goog.require('goog.testing.asserts');
Expand Down
2 changes: 0 additions & 2 deletions js/commonjs/export_testdeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* export_asserts.js.
*/

// Include a dummy provide statement so that closurebuilder.py does not skip over this
// file.
goog.provide('jspb.ExportTestDeps');

goog.require('goog.crypt.base64');
Expand Down
16 changes: 14 additions & 2 deletions js/compatibility_tests/v3.0.0/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,20 @@ for file in *_test.js binary/*_test.js; do
done
cp commonjs/{jasmine.json,import_test.js} commonjs_out/
mkdir -p commonjs_out/test_node_modules
../../node_modules/google-closure-library/closure/bin/calcdeps.py -i commonjs/export_asserts.js -p . -p ../../node_modules/google-closure-library/closure -o compiled --compiler_jar ../../node_modules/google-closure-compiler/compiler.jar > commonjs_out/test_node_modules/closure_asserts_commonjs.js
../../node_modules/google-closure-library/closure/bin/calcdeps.py -i commonjs/export_testdeps.js -p ../.. -p ../../node_modules/google-closure-library/closure -o compiled --compiler_jar ../../node_modules/google-closure-compiler/compiler.jar > commonjs_out/test_node_modules/testdeps_commonjs.js
../../node_modules/.bin/google-closure-compiler \
--entry_point=commonjs/export_asserts.js \
--js=commonjs/export_asserts.js \
--js=../../node_modules/google-closure-library/closure/goog/**.js \
--js=../../node_modules/google-closure-library/third_party/closure/goog/**.js \
> commonjs_out/test_node_modules/closure_asserts_commonjs.js
../../node_modules/.bin/google-closure-compiler \
--entry_point=commonjs/export_testdeps.js \
--js=commonjs/export_testdeps.js \
--js=../../binary/*.js \
--js=!../../binary/*_test.js \
--js=../../node_modules/google-closure-library/closure/goog/**.js \
--js=../../node_modules/google-closure-library/third_party/closure/goog/**.js \
> commonjs_out/test_node_modules/testdeps_commonjs.js
cp ../../google-protobuf.js commonjs_out/test_node_modules
cp -r ../../commonjs_out/node_modules commonjs_out

Expand Down
36 changes: 23 additions & 13 deletions js/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,31 @@ gulp.task('genproto_group3_commonjs_strict', function (cb) {
});


function getClosureBuilderCommand(exportsFile, outputFile) {
return './node_modules/google-closure-library/closure/bin/build/closurebuilder.py ' +
'--root node_modules ' +
'-o compiled ' +
'--compiler_jar node_modules/google-closure-compiler-java/compiler.jar ' +
'-i ' + exportsFile + ' ' +
'map.js message.js binary/arith.js binary/constants.js binary/decoder.js ' +
'binary/encoder.js binary/reader.js binary/utils.js binary/writer.js ' +
exportsFile + ' > ' + outputFile;
function getClosureCompilerCommand(exportsFile, outputFile) {
const closureLib = 'node_modules/google-closure-library';
return [
'node_modules/.bin/google-closure-compiler',
`--js=${closureLib}/closure/goog/**.js`,
`--js=${closureLib}/third_party/closure/goog/**.js`,
'--js=map.js',
'--js=message.js',
'--js=binary/arith.js',
'--js=binary/constants.js',
'--js=binary/decoder.js',
'--js=binary/encoder.js',
'--js=binary/reader.js',
'--js=binary/utils.js',
'--js=binary/writer.js',
`--js=${exportsFile}`,
`--entry_point=${exportsFile}`,
`> ${outputFile}`
].join(' ');
}

gulp.task('dist', gulp.series(['genproto_wellknowntypes'], function(cb) {
// TODO(haberman): minify this more aggressively.
// Will require proper externs/exports.
exec(getClosureBuilderCommand('commonjs/export.js', 'google-protobuf.js'),
exec(getClosureCompilerCommand('commonjs/export.js', 'google-protobuf.js'),
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
Expand All @@ -164,7 +174,7 @@ gulp.task('dist', gulp.series(['genproto_wellknowntypes'], function(cb) {

gulp.task('commonjs_asserts', function (cb) {
exec('mkdir -p commonjs_out/test_node_modules && ' +
getClosureBuilderCommand(
getClosureCompilerCommand(
'commonjs/export_asserts.js',
'commonjs_out/test_node_modules/closure_asserts_commonjs.js'),
function (err, stdout, stderr) {
Expand All @@ -176,7 +186,7 @@ gulp.task('commonjs_asserts', function (cb) {

gulp.task('commonjs_testdeps', function (cb) {
exec('mkdir -p commonjs_out/test_node_modules && ' +
getClosureBuilderCommand(
getClosureCompilerCommand(
'commonjs/export_testdeps.js',
'commonjs_out/test_node_modules/testdeps_commonjs.js'),
function (err, stdout, stderr) {
Expand Down Expand Up @@ -229,7 +239,7 @@ gulp.task(
],
function(cb) {
exec(
'./node_modules/google-closure-library/closure/bin/build/depswriter.py binary/arith.js binary/constants.js binary/decoder.js binary/encoder.js binary/reader.js binary/utils.js binary/writer.js debug.js map.js message.js node_loader.js test_bootstrap.js > deps.js',
'./node_modules/.bin/closure-make-deps --closure-path=. --file=node_modules/google-closure-library/closure/goog/deps.js binary/arith.js binary/constants.js binary/decoder.js binary/encoder.js binary/reader.js binary/utils.js binary/writer.js debug.js map.js message.js node_loader.js test_bootstrap.js > deps.js',
function(err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
Expand Down
1 change: 1 addition & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"devDependencies": {
"glob": "~7.1.4",
"google-closure-compiler": "~20190819.0.0",
"google-closure-deps": "^20210406.0.0",
"google-closure-library": "~20190819.0.0",
"gulp": "~4.0.2",
"jasmine": "~3.4.0"
Expand Down

0 comments on commit fd896ba

Please sign in to comment.