From a83a17663bc62c08b24b791d689580ba4ce3a223 Mon Sep 17 00:00:00 2001 From: Radium Zheng Date: Tue, 21 Jan 2020 18:17:57 +1100 Subject: [PATCH 1/4] repro --- test/pizza_service_proto_test.spec.ts | 8 +++++++- test/rollup_test.spec.js | 6 ++++++ test/test_bundling.ts | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/test/pizza_service_proto_test.spec.ts b/test/pizza_service_proto_test.spec.ts index 4584fab..cf4e0b6 100644 --- a/test/pizza_service_proto_test.spec.ts +++ b/test/pizza_service_proto_test.spec.ts @@ -1,6 +1,6 @@ import {Pizza, PizzaSize} from 'rules_typescript_proto/test/proto/common/pizza_pb'; import {OrderPizzaRequest, OrderPizzaResponse} from 'rules_typescript_proto/test/proto/pizza_service_pb'; -import {PizzaService} from 'rules_typescript_proto/test/proto/pizza_service_pb_service'; +import {PizzaService,PizzaServiceClient} from 'rules_typescript_proto/test/proto/pizza_service_pb_service'; declare function require(module: string): any; @@ -14,6 +14,12 @@ describe('DeliveryPerson', () => { expect(PizzaService).toBeDefined(); }); + it('PizzaServiceClient.orderPizza should return a UnaryResponse', () => { + const client = new PizzaServiceClient('http://localhost', {}); + const response = client.orderPizza(new OrderPizzaRequest(), (_) => {}); + expect(typeof response.cancel).toBe('function'); + }); + it('Generated code seems to work', () => { const request = new OrderPizzaRequest(); diff --git a/test/rollup_test.spec.js b/test/rollup_test.spec.js index 27fc402..1ff545b 100644 --- a/test/rollup_test.spec.js +++ b/test/rollup_test.spec.js @@ -28,6 +28,12 @@ describe('Rollup', () => { expect(typeof client.orderPizza).toBe('function'); }); + it('PizzaServiceClient.orderPizza should return a UnaryResponse', () => { + const client = new bundle.PizzaServiceClient('http://localhost', {}); + const response = client.orderPizza(new bundle.OrderPizzaRequest(), (_) => {}); // This will throw + expect(typeof response.cancel).toBe('function'); + }) + it('should follow expected naming styles', () => { expect(new bundle.alllowercase().setTest(1)).toBeTruthy(); expect(new bundle.ALLUPPERCASE().setTest(1)).toBeTruthy(); diff --git a/test/test_bundling.ts b/test/test_bundling.ts index f82dada..0188f75 100644 --- a/test/test_bundling.ts +++ b/test/test_bundling.ts @@ -1,4 +1,5 @@ -export {alllowercase, ALLUPPERCASE, lowerCamelCase, m24M_, M2_M, M2M, M2M_, m42_M, M9, m_22M, M_2M, snake_case_snake_case, Upper_snake_Case, UpperCamelCase,} from 'rules_typescript_proto/test/proto/naming_styles_pb'; +export { alllowercase, ALLUPPERCASE, lowerCamelCase, m24M_, M2_M, M2M, M2M_, m42_M, M9, m_22M, M_2M, snake_case_snake_case, Upper_snake_Case, UpperCamelCase, } from 'rules_typescript_proto/test/proto/naming_styles_pb'; export { DeliveryPerson } from 'rules_typescript_proto/test/proto/common/delivery_person_pb'; export { Pizza, PizzaSize } from 'rules_typescript_proto/test/proto/common/pizza_pb'; export { PizzaService, PizzaServiceClient } from 'rules_typescript_proto/test/proto/pizza_service_pb_service'; +export { OrderPizzaRequest } from 'rules_typescript_proto/test/proto/pizza_service_pb'; From 4f61e4ac3e3fa4fe22acaafe523e13333d5e9f7f Mon Sep 17 00:00:00 2001 From: Radium Zheng Date: Wed, 22 Jan 2020 20:32:16 +1100 Subject: [PATCH 2/4] import --- src/change_import_style.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/change_import_style.ts b/src/change_import_style.ts index e8f3a42..27754d3 100644 --- a/src/change_import_style.ts +++ b/src/change_import_style.ts @@ -19,8 +19,8 @@ function main() { const umdContents = convertToUmd(args, initialContents); fs.writeFileSync(args.output_umd_path, umdContents, 'utf8'); - const commonJsContents = convertToESM(args, initialContents); - fs.writeFileSync(args.output_es6_path, commonJsContents, 'utf8'); + const esmContents = convertToESM(args, initialContents); + fs.writeFileSync(args.output_es6_path, esmContents, 'utf8'); } function replaceRecursiveFilePaths(args: any) { @@ -88,7 +88,7 @@ function convertToESM(args: any, initialContents: string): string { }; const replaceRequiresWithSubpackageImports = (contents: string) => { - return contents.replace(/var ([\w\d_]+) = require\((['"][\w\d@/_-]+['"])\)\.([\w\d_]+);/g, 'import * as $1 from $2;') + return contents.replace(/var ([\w\d_]+) = require\((['"][\w\d@/_-]+['"])\)\.([\w\d_]+);/g, 'import {$3 as $1} from $2;') } const replaceCJSExportsWithECMAExports = (contents: string) => { From 1f79c6bf9621a7deafd1af071e6b8184af06c1eb Mon Sep 17 00:00:00 2001 From: Radium Zheng Date: Wed, 22 Jan 2020 21:08:02 +1100 Subject: [PATCH 3/4] fix --- src/change_import_style.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/change_import_style.ts b/src/change_import_style.ts index 27754d3..3f12b25 100644 --- a/src/change_import_style.ts +++ b/src/change_import_style.ts @@ -88,7 +88,13 @@ function convertToESM(args: any, initialContents: string): string { }; const replaceRequiresWithSubpackageImports = (contents: string) => { - return contents.replace(/var ([\w\d_]+) = require\((['"][\w\d@/_-]+['"])\)\.([\w\d_]+);/g, 'import {$3 as $1} from $2;') + // Example: + // Changes: var foo = require("@improbable-eng/grpc-web").bar; + // To: import {bar as foo} from "@improbable-eng/grpc-web"; + return contents.replace(/var ([\w\d_]+) = require\((['"][\w\d@/_-]+['"])\)\.([\w\d_]+);/g, + (_, varName, moduleIdentifier, propertyName) => + propertyName == varName ? `import { ${varName} } from ${moduleIdentifier}` + : `import { ${propertyName} as ${varName} } from ${moduleIdentifier};`); } const replaceCJSExportsWithECMAExports = (contents: string) => { From fa21e6a057a96ea18c2f61d02d90d4bf82db354a Mon Sep 17 00:00:00 2001 From: Radium Zheng Date: Wed, 22 Jan 2020 21:42:28 +1100 Subject: [PATCH 4/4] wip --- test/BUILD.bazel | 38 ++++++++++++++++++++++++++----- test/require.config.js | 3 ++- test/rollup.config.js | 2 +- test/rollup_test.spec.js | 6 ----- test/rollup_test.spec.ts | 49 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 test/rollup_test.spec.ts diff --git a/test/BUILD.bazel b/test/BUILD.bazel index 2b905b4..e2b3a1e 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -106,13 +106,13 @@ rollup_bundle( entry_points = { ":test_bundling.ts": "index", }, + format = "cjs", output_dir = True, deps = [ ":test_bundling_lib", "@npm//rollup-plugin-commonjs", "@npm//rollup-plugin-node-resolve", ], - format = "cjs", ) ts_library( @@ -121,20 +121,46 @@ ts_library( tsconfig = ":test_bundling_tsconfig.json", deps = [ "//test/proto:naming_styles_ts_proto", - "//test/proto/common:pizza_ts_proto", "//test/proto:pizza_service_ts_proto", "//test/proto/common:delivery_person_ts_proto", - "@npm//google-protobuf", + "//test/proto/common:pizza_ts_proto", "@npm//@improbable-eng/grpc-web", + "@npm//google-protobuf", ], ) jasmine_node_test( name = "rollup_test", srcs = [ - ":rollup_test.spec.js" + ":rollup_test.spec.js", ], data = [ - ":test_es6_bundling" - ] + ":test_es6_bundling", + ], +) + +ts_library( + name = "rollup_test_spec", + testonly = 1, + srcs = ["rollup_test.spec.ts"], + tsconfig = ":tsconfig.json", + + deps=["@npm//@types/jasmine",] +) + + +karma_web_test_suite( + name = "rollup_browser_test_suite", + srcs = [ + "require.config.js", + ], + browsers = [ + "@io_bazel_rules_webtesting//browsers:chromium-local", + ], + tags = ["native"], + deps = [ + ":rollup_test_spec", + ":test_es6_bundling", + + ], ) diff --git a/test/require.config.js b/test/require.config.js index 0c60723..de3649c 100644 --- a/test/require.config.js +++ b/test/require.config.js @@ -2,6 +2,7 @@ require.config({ paths: { '@improbable-eng/grpc-web': 'base/npm/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.umd', - 'browser-headers': 'base/npm/node_modules/browser-headers/dist/browser-headers.umd' + 'browser-headers': 'base/npm/node_modules/browser-headers/dist/browser-headers.umd', + 'test_es6_bundling': 'base/rules_typescript_proto/test/test_es6_bundling/index', } }); diff --git a/test/rollup.config.js b/test/rollup.config.js index 89a556a..b6f2af8 100644 --- a/test/rollup.config.js +++ b/test/rollup.config.js @@ -7,7 +7,7 @@ module.exports = { commonjs({ // Temporary fix until https://github.com/improbable-eng/grpc-web/issues/369 is resolved. namedExports: { - './node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js': [ + '@improbable-eng/grpc-web': [ 'grpc', ], } diff --git a/test/rollup_test.spec.js b/test/rollup_test.spec.js index 1ff545b..27fc402 100644 --- a/test/rollup_test.spec.js +++ b/test/rollup_test.spec.js @@ -28,12 +28,6 @@ describe('Rollup', () => { expect(typeof client.orderPizza).toBe('function'); }); - it('PizzaServiceClient.orderPizza should return a UnaryResponse', () => { - const client = new bundle.PizzaServiceClient('http://localhost', {}); - const response = client.orderPizza(new bundle.OrderPizzaRequest(), (_) => {}); // This will throw - expect(typeof response.cancel).toBe('function'); - }) - it('should follow expected naming styles', () => { expect(new bundle.alllowercase().setTest(1)).toBeTruthy(); expect(new bundle.ALLUPPERCASE().setTest(1)).toBeTruthy(); diff --git a/test/rollup_test.spec.ts b/test/rollup_test.spec.ts new file mode 100644 index 0000000..9844bed --- /dev/null +++ b/test/rollup_test.spec.ts @@ -0,0 +1,49 @@ +declare function require(module: string): any; + +const bundle = require('rules_typescript_proto/test/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); + }); + + it('should define DeliveryPerson', () => { + expect(bundle.DeliveryPerson).toBeDefined(); + expect(new bundle.DeliveryPerson()).toBeTruthy(); + }); + + it('should define PizzaService', () => { + expect(bundle.PizzaService).toBeDefined(); + expect(bundle.PizzaService.serviceName).toBe('test.bazel.proto.PizzaService'); + expect(bundle.PizzaService.OrderPizza.methodName).toBe('OrderPizza'); + }); + + it('should define PizzaServiceClient', () => { + expect(bundle.PizzaServiceClient).toBeDefined(); + const client = new bundle.PizzaServiceClient('http://localhost', {}); + expect(typeof client.orderPizza).toBe('function'); + }); + + it('should follow expected naming styles', () => { + expect(new bundle.alllowercase().setTest(1)).toBeTruthy(); + expect(new bundle.ALLUPPERCASE().setTest(1)).toBeTruthy(); + expect(new bundle.lowerCamelCase().setTest(1)).toBeTruthy(); + expect(new bundle.UpperCamelCase().setTest(1)).toBeTruthy(); + expect(new bundle.snake_case_snake_case().setTest(1)).toBeTruthy(); + expect(new bundle.Upper_snake_Case().setTest(1)).toBeTruthy(); + expect(new bundle.M2M().setTest(1)).toBeTruthy(); + expect(new bundle.M_2M().setTest(1)).toBeTruthy(); + expect(new bundle.M2_M().setTest(1)).toBeTruthy(); + expect(new bundle.M2M_().setTest(1)).toBeTruthy(); + expect(new bundle.m_22M().setTest(1)).toBeTruthy(); + expect(new bundle.m42_M().setTest(1)).toBeTruthy(); + expect(new bundle.m24M_().setTest(1)).toBeTruthy(); + expect(new bundle.M9().setTest(1)).toBeTruthy(); + }); +});