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

As a Builder, I want package tree-shaking, so I can minimize my build size, compile time, test time, and page load time. #1148

Open
2 tasks
MantisClone opened this issue Sep 12, 2023 · 4 comments · May be fixed by #1205

Comments

@MantisClone
Copy link
Member

MantisClone commented Sep 12, 2023

Problem

  • The top-level tsconfig.json specifies "module": "commonjs". This prevents tree-shaking, or the elimination of dead code.

According to the webpack docs:

In order to take advantage of tree shaking, you must...

  • Use ES2015 module syntax (i.e. import and export).
  • Ensure no compilers transform your ES2015 module syntax into CommonJS modules (this is the default behavior of the popular Babel preset @babel/preset-env - see the documentation for more details).
  • Add a "sideEffects" property to your project's package.json file.
  • Use the production mode configuration option to enable various optimizations including minification and tree shaking.
  • Make sure you set a correct value for devtool as some of them can't be used in production mode.

Solution

  • Set "module": "node16" and "moduleResolution": "node16" in the tsconfig.json

    For compatibility with Node.js so users who require CommonJS can get it, but consumers of our bundled packages will get ES2020 and can do tree-shaking.

    "module": "node16"

    Available from 4.7+, the node16 and nodenext modes integrate with Node’s native ECMAScript Module support. The emitted JavaScript uses either CommonJS or ES2020 output depending on the file extension and the value of the type setting in the nearest package.json

    "moduleResolution": "node16"

    Node.js v12 and later supports both ECMAScript imports and CommonJS require, which resolve using different algorithms. These moduleResolution values, when combined with the corresponding module values, picks the right algorithm for each resolution based on whether Node.js will see an import or require in the output JavaScript code.

  • Append .js to all relative imports filenames

  • Set "type": "module" in all package.json files

Definiton of Done

Non Issues


Migrated from Asana: https://app.asana.com/0/1203912381456855/1205320821557402

@MantisClone
Copy link
Member Author

MantisClone commented Oct 13, 2023

@skiv71 discovered that when we configure "module": "es2015" we can still use our packages in a node js application with require(). So maybe we don't need to use "module": "node16".

@MantisClone
Copy link
Member Author

We've discussed this task a lot this past week, despite it not being part of the sprint. I've decided to move it into this sprint so that we can implement it and move on.

@MantisClone MantisClone linked a pull request Oct 20, 2023 that will close this issue
@MantisClone MantisClone linked a pull request Oct 20, 2023 that will close this issue
@MantisClone MantisClone assigned MantisClone and unassigned skiv71 Oct 20, 2023
@MantisClone
Copy link
Member Author

While working on this, I disabled Webpack. I think that we'll remove Webpack as part of this task so I've added #1203 as a subtask in the description.

@MantisClone
Copy link
Member Author

The effort required to fix this is larger than anticipated and upon closer inspection, we realize that tree-shaking will still be blocked by #1202 even after switching to ES Modules.

Thus, we still want to do this task, but I'm reducing the priority to Medium.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🎫 Backlog
Development

Successfully merging a pull request may close this issue.

2 participants