Skip to content

Commit

Permalink
feat(tools): minify proto JS files (#1435)
Browse files Browse the repository at this point in the history
* feat: add minifyJs option for minifyTools
  • Loading branch information
sofisl committed Mar 30, 2023
1 parent 3cfb046 commit 014fa16
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
28 changes: 28 additions & 0 deletions test/fixtures/echo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Lots of comments we can delete in uglify
async function main() {
// Showcases auto-pagination functionality.

// Let's say we have an API call that returns results grouped into pages.
// It accepts 4 parameters (just like gRPC stub calls do):
return 'SUCCESS';
}

main().catch(console.error);
// More comments

15 changes: 15 additions & 0 deletions test/unit/minify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,26 @@ describe('minify tool', () => {
const objectBefore = require(echoProtoJson);
await minify.main(testDir);
const statAfter = await fsp.stat(echoProtoJson);
delete require.cache[require(echoProtoJson)];
const objectAfter = require(echoProtoJson);
const contentAfter = (await fsp.readFile(echoProtoJson)).toString();
const parsedObjectAfter = JSON.parse(contentAfter);
assert(statBefore.size > statAfter.size);
assert.deepEqual(objectBefore, objectAfter);
assert.deepEqual(objectBefore, parsedObjectAfter);
});

it('minifies the proto js file', async () => {
const echoProtoJsFixture = path.join(fixturesDir, 'echo.js');
const echoProtoJs = path.join(testDir, 'echo.js');
await fsp.copyFile(echoProtoJsFixture, echoProtoJs);
const statBefore = await fsp.stat(echoProtoJs);
const resultBefore = require(echoProtoJs);
await minify.main(testDir);
const statAfter = await fsp.stat(echoProtoJs);
delete require.cache[require(echoProtoJs)];
const resultAfter = require(echoProtoJs);
assert(statBefore.size > statAfter.size);
assert.deepEqual(resultBefore, resultAfter);
});
});
29 changes: 21 additions & 8 deletions tools/minify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ import {promises as fsp} from 'fs';
import * as path from 'path';
import * as uglify from 'uglify-js';

async function minifyFile(filename: string) {
async function minifyFile(filename: string, isJs: boolean) {
const content = (await fsp.readFile(filename)).toString();
const output = uglify.minify(content, {
expression: true,
compress: false, // we need to keep it valid JSON
output: {quote_keys: true},
});
let options;
if (isJs) {
options = {
expression: false,
};
} else {
options = {
expression: true,
compress: false, // we need to keep it valid JSON
output: {quote_keys: true},
};
}
const output = uglify.minify(content, options as uglify.MinifyOptions);
if (output.error) {
throw output.error;
}
Expand All @@ -39,9 +47,14 @@ export async function main(directory?: string) {
const jsonFiles = files.filter(file => file.match(/\.json$/));
for (const jsonFile of jsonFiles) {
console.log(`Minifying ${jsonFile}...`);
await minifyFile(path.join(buildDir, jsonFile));
await minifyFile(path.join(buildDir, jsonFile), false);
}
const jsFiles = files.filter(file => file.match(/\.js$/));
for (const jsFile of jsFiles) {
console.log(`Minifying ${jsFile}...`);
await minifyFile(path.join(buildDir, jsFile), true);
}
console.log('Minified all proto JSON files successfully.');
console.log('Minified all proto JS and JSON files successfully.');
}

function usage() {
Expand Down

0 comments on commit 014fa16

Please sign in to comment.