Skip to content

Commit

Permalink
refactor(labs): updated grpc_web imports to reflect removed workspace…
Browse files Browse the repository at this point in the history
… symlink
  • Loading branch information
gregmagolan committed Apr 17, 2020
1 parent af0efec commit 80faa7b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
33 changes: 25 additions & 8 deletions packages/labs/src/grpc_web/change_import_style.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
const minimist = require('minimist');
const fs = require('fs');
const path = require('path');

function main() {
const args = minimist(process.argv.slice(2));
Expand Down Expand Up @@ -119,20 +120,36 @@ function convertToESM(args, initialContents) {
const replaceRequiresWithImports = (contents) => {
return contents
.replace(
/var ([\w\d_]+) = require\((['"][\.\\]*[\w\d@/_-]+['"])\)/g, 'import * as $1 from $2')
/var ([\w\d_]+) = require\(['"]([\.\\]*[\w\d@/_-]+)['"]\)/g,
(_, variable, importPath) => {
if (importPath.startsWith(args.workspace_name)) {
importPath = `./${path.relative(args.input_base_path, importPath)}`;
}
return `import * as ${variable} from '${importPath}';`
})
.replace(
/([\.\w\d_]+) = require\((['"][\.\w\d@/_-]+['"])\)/g, (_, variable, importPath) => {
/([\.\w\d_]+) = require\(['"]([\.\w\d@/_-]+)['"]\)/g, (_, variable, importPath) => {
if (importPath.startsWith(args.workspace_name)) {
importPath = `./${path.relative(args.input_base_path, importPath)}`;
}

const normalizedVariable = variable.replace(/\./g, '_');
return `import * as ${normalizedVariable} from ${importPath};\n${variable} = {...${
return `import * as ${normalizedVariable} from '${importPath}';\n${variable} = {...${
normalizedVariable}}`;
});
};

const replaceRequiresWithSubpackageImports = (contents) => {
return contents.replace(
/var ([\w\d_]+) = require\((['"][\w\d@/_-]+['"])\)\.([\w\d_]+);/g,
'import * as $1 from $2;');
};
const replaceRequiresWithSubpackageImports =
(contents) => {
return contents.replace(
/var ([\w\d_]+) = require\(['"]([\w\d@/_-]+)['"]\)\.([\w\d_]+);/g,
(_, variable, importPath) => {
if (importPath.startsWith(args.workspace_name)) {
importPath = `./${path.relative(args.input_base_path, importPath)}`;
}
return `import * as ${variable} from '${importPath}';`
});
}

const replaceCJSExportsWithECMAExports = (contents) => {
return contents.replace(/exports\.([\w\d_]+) = .*;/g, 'export { $1 };');
Expand Down
21 changes: 14 additions & 7 deletions packages/labs/test/grpc_web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ karma_web_test_suite(
rollup_bundle(
name = "test_es6_bundling",
config_file = "rollup.config.js",
entry_points = {
":test_bundling.ts": "index",
},
entry_point = ":test_bundling.ts",
format = "cjs",
output_dir = True,
deps = [
":test_bundling_lib",
"@npm//rollup-plugin-commonjs",
Expand All @@ -105,12 +102,22 @@ ts_library(
],
)

ts_library(
name = "rollup_test_lib",
testonly = 1,
srcs = ["rollup_test.spec.ts"],
deps = [
"@npm//@types/jasmine",
"@npm//@types/node",
],
)

jasmine_node_test(
name = "rollup_test",
srcs = [
":rollup_test.spec.js",
],
data = [
":test_es6_bundling",
],
deps = [
":rollup_test_lib",
],
)
4 changes: 2 additions & 2 deletions packages/labs/test/grpc_web/commonjs_test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import deliveryPersonPb = require('build_bazel_rules_nodejs/packages/labs/test/grpc_web/proto/common/delivery_person_pb');
import {PizzaServiceClient} from 'build_bazel_rules_nodejs/packages/labs/test/grpc_web/proto/pizza_service_grpc_web_pb';
import deliveryPersonPb = require('./proto/common/delivery_person_pb');
import {PizzaServiceClient} from './proto/pizza_service_grpc_web_pb';

describe('CommonJs', () => {
it('Loads imports using require()', () => {
Expand Down
9 changes: 4 additions & 5 deletions packages/labs/test/grpc_web/pizza_service_proto_test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'google-protobuf';

import {Pizza, PizzaSize} from 'build_bazel_rules_nodejs/packages/labs/test/grpc_web/proto/common/pizza_pb';
import {PizzaServiceClient} from 'build_bazel_rules_nodejs/packages/labs/test/grpc_web/proto/pizza_service_grpc_web_pb';
import {OrderPizzaRequest, OrderPizzaResponse} from 'build_bazel_rules_nodejs/packages/labs/test/grpc_web/proto/pizza_service_pb';
import {Pizza, PizzaSize} from './proto/common/pizza_pb';
import {PizzaServiceClient} from './proto/pizza_service_grpc_web_pb';
import {OrderPizzaRequest, OrderPizzaResponse} from './proto/pizza_service_pb';

declare function require(module: string): any;

Expand All @@ -27,8 +27,7 @@ describe('DeliveryPerson', () => {
});

it('delivery_person_ts_proto is included since it is a transitive dependency', () => {
const PROTOS = require(
'build_bazel_rules_nodejs/packages/labs/test/grpc_web/proto/common/delivery_person_pb');
const PROTOS = require('./proto/common/delivery_person_pb');
const DeliveryPerson = PROTOS.DeliveryPerson;
const pizza = new Pizza();
pizza.setSize(PizzaSize.PIZZA_SIZE_LARGE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const grpcWeb = require('grpc-web');
grpcWeb.MethodType = {
UNARY: 'unary'
UNARY: 'unary',
};
const bundle = require('build_bazel_rules_nodejs/packages/labs/test/grpc_web/test_es6_bundling/');
const bundle = require('./test_es6_bundling');

describe('Rollup', () => {
it('should define Pizza with protobuf API', () => {
expect(bundle.Pizza).toBeDefined();

const pizza = new bundle.Pizza();
pizza.setSize(bundle.PizzaSize.PIZZA_SIZE_LARGE);

expect(pizza.getSize()).toBe(bundle.PizzaSize.PIZZA_SIZE_LARGE);
expect(Array.isArray(pizza.getToppingIdsList())).toBe(true);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/labs/test/grpc_web/test_bundling.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {DeliveryPerson} from 'build_bazel_rules_nodejs/packages/labs/test/grpc_web/proto/common/delivery_person_pb';
export {Pizza, PizzaSize} from 'build_bazel_rules_nodejs/packages/labs/test/grpc_web/proto/common/pizza_pb';
export {DeliveryPerson} from './proto/common/delivery_person_pb';
export {Pizza, PizzaSize} from './proto/common/pizza_pb';

0 comments on commit 80faa7b

Please sign in to comment.