Skip to content

Commit

Permalink
Add /*@__PURE__*/ annotation for better tree-shaking support (#470)
Browse files Browse the repository at this point in the history
Hi,

We are using `protoc-gen-es` to generate TypeScript files for our
protobuf messages. In our project, there are unused messages that get
compiled into JavaScript files but are not imported anywhere. Since
we're using Vite, which relies on Rollup for bundling, Rollup does not
remove makeMessageType function calls from the output bundle, as seen in
the example below:

```js
f.makeMessageType("blacklist.SearchUnblacklistsByPhonesResponse", () => [{
  no: 1,
  name: "unblacklisted_phones",
  kind: "scalar",
  T: 9,
  repeated: !0
}]);
```

Rollup assumes that `f.makeMessageType` may have side effects (such as
mutating the global state), leading to unnecessary code being included
in the final bundle. As a result, our bundle size has doubled, becoming
significantly larger.

To address this issue, I've added the `/*@__PURE__*/` annotation before
`makeMessageType` function calls. This informs Rollup that these
function calls do not have any side effects, as documented in the
[Rollup configuration
options](https://rollupjs.org/configuration-options/#treeshake-annotations).
Adding this annotation will allow Rollup to better optimize the output
bundle by eliminating unused code.

If you have any suggestions for a better solution or improvements,
please leave a comment.

Thank you!
  • Loading branch information
pmzi authored Mar 13, 2024
1 parent 38ca963 commit 4f94199
Show file tree
Hide file tree
Showing 62 changed files with 587 additions and 587 deletions.
12 changes: 6 additions & 6 deletions packages/protobuf-test/src/gen/js/extra/comments_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions packages/protobuf-test/src/gen/js/extra/enum_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/protobuf-test/src/gen/js/extra/example_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions packages/protobuf-test/src/gen/js/extra/extensions-proto2_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/protobuf-test/src/gen/js/extra/jstype-proto2_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/protobuf-test/src/gen/js/extra/jstype_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/protobuf-test/src/gen/js/extra/msg-maps_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/protobuf-test/src/gen/js/extra/msg-message_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/protobuf-test/src/gen/js/extra/msg-oneof_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/protobuf-test/src/gen/js/extra/msg-scalar_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4f94199

Please sign in to comment.