Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit 014fa16

Browse files
authored
feat(tools): minify proto JS files (#1435)
* feat: add minifyJs option for minifyTools
1 parent 3cfb046 commit 014fa16

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

test/fixtures/echo.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Lots of comments we can delete in uglify
18+
async function main() {
19+
// Showcases auto-pagination functionality.
20+
21+
// Let's say we have an API call that returns results grouped into pages.
22+
// It accepts 4 parameters (just like gRPC stub calls do):
23+
return 'SUCCESS';
24+
}
25+
26+
main().catch(console.error);
27+
// More comments
28+

test/unit/minify.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,26 @@ describe('minify tool', () => {
4141
const objectBefore = require(echoProtoJson);
4242
await minify.main(testDir);
4343
const statAfter = await fsp.stat(echoProtoJson);
44+
delete require.cache[require(echoProtoJson)];
4445
const objectAfter = require(echoProtoJson);
4546
const contentAfter = (await fsp.readFile(echoProtoJson)).toString();
4647
const parsedObjectAfter = JSON.parse(contentAfter);
4748
assert(statBefore.size > statAfter.size);
4849
assert.deepEqual(objectBefore, objectAfter);
4950
assert.deepEqual(objectBefore, parsedObjectAfter);
5051
});
52+
53+
it('minifies the proto js file', async () => {
54+
const echoProtoJsFixture = path.join(fixturesDir, 'echo.js');
55+
const echoProtoJs = path.join(testDir, 'echo.js');
56+
await fsp.copyFile(echoProtoJsFixture, echoProtoJs);
57+
const statBefore = await fsp.stat(echoProtoJs);
58+
const resultBefore = require(echoProtoJs);
59+
await minify.main(testDir);
60+
const statAfter = await fsp.stat(echoProtoJs);
61+
delete require.cache[require(echoProtoJs)];
62+
const resultAfter = require(echoProtoJs);
63+
assert(statBefore.size > statAfter.size);
64+
assert.deepEqual(resultBefore, resultAfter);
65+
});
5166
});

tools/minify.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@ import {promises as fsp} from 'fs';
2020
import * as path from 'path';
2121
import * as uglify from 'uglify-js';
2222

23-
async function minifyFile(filename: string) {
23+
async function minifyFile(filename: string, isJs: boolean) {
2424
const content = (await fsp.readFile(filename)).toString();
25-
const output = uglify.minify(content, {
26-
expression: true,
27-
compress: false, // we need to keep it valid JSON
28-
output: {quote_keys: true},
29-
});
25+
let options;
26+
if (isJs) {
27+
options = {
28+
expression: false,
29+
};
30+
} else {
31+
options = {
32+
expression: true,
33+
compress: false, // we need to keep it valid JSON
34+
output: {quote_keys: true},
35+
};
36+
}
37+
const output = uglify.minify(content, options as uglify.MinifyOptions);
3038
if (output.error) {
3139
throw output.error;
3240
}
@@ -39,9 +47,14 @@ export async function main(directory?: string) {
3947
const jsonFiles = files.filter(file => file.match(/\.json$/));
4048
for (const jsonFile of jsonFiles) {
4149
console.log(`Minifying ${jsonFile}...`);
42-
await minifyFile(path.join(buildDir, jsonFile));
50+
await minifyFile(path.join(buildDir, jsonFile), false);
51+
}
52+
const jsFiles = files.filter(file => file.match(/\.js$/));
53+
for (const jsFile of jsFiles) {
54+
console.log(`Minifying ${jsFile}...`);
55+
await minifyFile(path.join(buildDir, jsFile), true);
4356
}
44-
console.log('Minified all proto JSON files successfully.');
57+
console.log('Minified all proto JS and JSON files successfully.');
4558
}
4659

4760
function usage() {

0 commit comments

Comments
 (0)