Skip to content

Commit a3bd81b

Browse files
Dan Mulleralexeagle
authored andcommitted
fix(labs): make grpc service files tree shakable
1 parent a7e045b commit a3bd81b

7 files changed

Lines changed: 58 additions & 14 deletions

File tree

examples/protocol_buffers/app.e2e-spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ describe('protocol_buffers', () => {
2020
div.getText().then(t => expect(t).toEqual(`Car from server: Porsche`));
2121
done();
2222
});
23+
24+
it('should have a grpc service client', (done) => {
25+
const div = element(by.css('div.ts2'));
26+
div.getText().then(t => expect(t).toEqual(`CarServiceClient is defined: yes!`));
27+
done();
28+
});
2329
});

examples/protocol_buffers/app.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {CarServiceClient} from 'examples_protocol_buffers/car_grpc_web_pb';
12
import {Car} from 'examples_protocol_buffers/car_pb';
23

34
const car = new Car();
@@ -7,3 +8,8 @@ const el: HTMLDivElement = document.createElement('div');
78
el.innerText = `Car from server: ${car.getMake()}`;
89
el.className = 'ts1';
910
document.body.appendChild(el);
11+
12+
const el2: HTMLDivElement = document.createElement('div');
13+
el2.innerText = `CarServiceClient is defined: ${CarServiceClient ? 'yes!' : 'no :('}`;
14+
el2.className = 'ts2';
15+
document.body.appendChild(el2);

examples/protocol_buffers/car.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ message Car {
1212
Tire rear_tires = 5;
1313
int64 mileage = 6;
1414
}
15+
16+
message GetCarRequest {
17+
}
18+
message GetCarResponse {
19+
}
20+
21+
service CarService {
22+
rpc GetCar(GetCarRequest) returns (GetCarResponse) {
23+
}
24+
}

examples/protocol_buffers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@types/long": "^4.0.0",
1111
"@types/node": "11.11.1",
1212
"google-protobuf": "3.11.4",
13-
"grpc-web": "1.0.7",
13+
"grpc-web": "1.1.0",
1414
"http-server": "^0.11.1",
1515
"karma": "~4.1.0",
1616
"karma-chrome-launcher": "2.2.0",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"core-util-is": "^1.0.2",
4242
"date-fns": "1.30.1",
4343
"google-protobuf": "^3.6.1",
44-
"grpc-web": "^1.0.7",
44+
"grpc-web": "1.1.0",
4545
"hello": "file:./tools/npm_packages/hello",
4646
"history-server": "^1.3.1",
4747
"http-server": "^0.11.1",

packages/labs/grpc_web/change_import_style.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,15 @@ function convertToUmd(args, initialContents) {
8181
// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
8282
function convertToESM(args, initialContents) {
8383
const replaceGoogExtendWithExports = (contents) => {
84-
return contents.replace(/goog\.object\.extend\(exports, ([\w\.]+)\);/g, (_, packageName) => {
84+
const symbols = [];
85+
let exportVariable;
86+
let packageName;
87+
88+
contents = contents.replace(/goog\.object\.extend\(exports, ([\w\.]+)\);/g, (_, p) => {
89+
packageName = p;
90+
exportVariable = contents.includes('const grpc = {}') ? 'exportVariable' : packageName;
91+
8592
const exportSymbols = /goog\.exportSymbol\('([\w\.]+)',.*\);/g;
86-
const symbols = [];
8793

8894
let match;
8995
while ((match = exportSymbols.exec(initialContents))) {
@@ -95,15 +101,25 @@ function convertToESM(args, initialContents) {
95101
}
96102
}
97103

98-
return `export const { ${symbols.join(', ')} } = ${packageName}`;
104+
return `export const { ${symbols.join(', ')} } = ${exportVariable}`;
99105
});
106+
107+
return symbols.reduce(
108+
(contents, symbol) => {return contents.replace(
109+
new RegExp(`${packageName}\\.${symbol}`, 'g'), `${exportVariable}.${symbol}`)},
110+
contents)
100111
};
101112

102113
const replaceCMDefaultExportWithExports = (contents) => {
103-
return contents.replace(/module.exports = ([\w\.]+)\;/g, (_, packageName) => {
104-
const exportSymbols = new RegExp(`${packageName.replace('.', '\\.')}\.([\\w\\.]+) =`, 'g');
114+
const symbols = [];
115+
let exportVariable;
116+
let packageName;
105117

106-
const symbols = [];
118+
contents = contents.replace(/module.exports = ([\w\.]+)\;/g, (_, p) => {
119+
packageName = p;
120+
exportVariable = contents.includes('const grpc = {}') ? 'exportVariable' : packageName;
121+
122+
const exportSymbols = new RegExp(`${packageName.replace('.', '\\.')}\.([\\w\\.]+) =`, 'g');
107123

108124
let match;
109125
while ((match = exportSymbols.exec(initialContents))) {
@@ -115,8 +131,13 @@ function convertToESM(args, initialContents) {
115131
}
116132
}
117133

118-
return `export const { ${symbols.join(', ')} } = ${packageName};`;
134+
return `export const { ${symbols.join(', ')} } = ${exportVariable};`;
119135
});
136+
137+
return symbols.reduce(
138+
(contents, symbol) => {return contents.replace(
139+
new RegExp(`${packageName}\\.${symbol}`, 'g'), `${exportVariable}.${symbol}`)},
140+
contents)
120141
};
121142

122143
const replaceRequiresWithImports = (contents) => {
@@ -151,7 +172,8 @@ function convertToESM(args, initialContents) {
151172
replaceCMDefaultExportWithExports,
152173
replaceCJSExportsWithECMAExports,
153174
];
154-
return transformations.reduce((currentContents, transform) => {
175+
176+
return `const exportVariable = {}\n` + transformations.reduce((currentContents, transform) => {
155177
return transform(currentContents);
156178
}, initialContents);
157179
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,10 +4557,10 @@ growly@^1.3.0:
45574557
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
45584558
integrity sha1-8QdIy+dq+WS3yWyTxrzCivEgwIE=
45594559

4560-
grpc-web@^1.0.7:
4561-
version "1.0.7"
4562-
resolved "https://registry.yarnpkg.com/grpc-web/-/grpc-web-1.0.7.tgz#9e4fbcf63d3734515332ab59e42baa7d0d290015"
4563-
integrity sha512-Fkbz1nyvvt6GC6ODcxh9Fen6LLB3OTCgGHzHwM2Eni44SUhzqPz1UQgFp9sfBEfInOhx3yBdwo9ZLjZAmJ+TtA==
4560+
grpc-web@1.1.0:
4561+
version "1.1.0"
4562+
resolved "https://registry.yarnpkg.com/grpc-web/-/grpc-web-1.1.0.tgz#18f73583a0a8c0b6c44a5cba62e66376d0ad019e"
4563+
integrity sha512-oPoS4/E/EO0TA2ZOSf3AxV2AbWDeabwfbAo+8oXNenOw87RmKz4hME8Sy4KDu2dUnqK8cuGfzdQlJPAEQEygNQ==
45644564

45654565
handlebars@^4.1.2:
45664566
version "4.5.3"

0 commit comments

Comments
 (0)