diff --git a/src/change_import_style.ts b/src/change_import_style.ts index e8f3a42..3f12b25 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,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 * 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) => { 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/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.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(); + }); +}); 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';