Skip to content

Fix import statement with "subpackages" in converted ESModule #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/change_import_style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check needed? It makes the code look cleaner, but since this code is generated anyways I don't think it really matters. I think you could just do: 'import {$3 as $1} from $2;

: `import { ${propertyName} as ${varName} } from ${moduleIdentifier};`);
}

const replaceCJSExportsWithECMAExports = (contents: string) => {
Expand Down
8 changes: 7 additions & 1 deletion test/pizza_service_proto_test.spec.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion test/require.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
});
2 changes: 1 addition & 1 deletion test/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
}
Expand Down
49 changes: 49 additions & 0 deletions test/rollup_test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
declare function require(module: string): any;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do:

import * as bundle from 'test_es6_bundling';

You'll also need a declaration file to get this to compile. I couldn't figure out how to export one, so I just added:

// test/test_es6_bundling.d.ts
declare module "test_es6_bundling" {
  var a: any;
  export = a;
}

And added it to the srcs of the test.


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();
});
});
3 changes: 2 additions & 1 deletion test/test_bundling.ts
Original file line number Diff line number Diff line change
@@ -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';