Skip to content
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

feat(tree-shaking): enable browser target webpack tree-shaking #21055

Closed
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
62 changes: 62 additions & 0 deletions web3.js/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const isCalledByBundler = caller => caller.name.startsWith('@rollup');

module.exports = api => {
const calledByBundler = api.caller(isCalledByBundler);
const transformEnvVariables =
!calledByBundler &&
process.env.NODE_ENV &&
process.env.BROWSER !== undefined;

return {
presets: [
[
'@babel/preset-env',
{
modules: false,
bugfixes: true,
},
],
['@babel/preset-typescript'],
],
plugins: [
[
'@babel/plugin-proposal-class-properties',
{
loose: true,
},
],
[
'@babel/plugin-proposal-private-methods',
{
loose: true,
},
],
[
'@babel/plugin-proposal-private-property-in-object',
{
loose: true,
},
],
transformEnvVariables && [
'transform-inline-environment-variables',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This on the other hand, I think is good to keep in any case, since it enables not forcing consumers to inject themselves inlined process.env.BROWSER & process.env.NODE_ENV values in their builds (even though, process.env.NODE_ENV is injected by most bundlers automatically today)

{
include: ['NODE_ENV', 'BROWSER'],
},
],
transformEnvVariables && [
'minify-dead-code-elimination',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we consider https://github.com/solana-labs/solana/pull/21055/files#r759707513, then this becomes unnecessary as well, the consumer's bundler / minifier will remove the bit of dead code:

  if (!process.env.BROWSER) {
    agentManager = new AgentManager(useHttps);
  }

{
keepFnName: true,
keepFnArgs: true,
keepClassName: true,
},
],
transformEnvVariables && [
'babel-plugin-transform-remove-imports',
{
test: 'agent-manager',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really satisfied by this solution, forces the build to be aware that a specific import is unused in specific conditions ̛& needs to be removed.

I believe it might be best to not remove the import, and in fact ship the code containing references to http[s] Node.js module, even if it forces additional yet trivial configuration of consumers (which in most cases, is covered by frameworks build)

},
],
].filter(Boolean),
};
};
31 changes: 0 additions & 31 deletions web3.js/babel.config.json

This file was deleted.

196 changes: 196 additions & 0 deletions web3.js/package-lock.json

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

Loading