How would you structure TypeScript code and exports for effective tree-shaking?
Tests ES module semantics and sideEffects for library authors. Great answers: emit ESM, set sideEffects:false, avoid stateful barrel files, and preserve import syntax. Red flag: saying ESM alone guarantees tree-shaking without mentioning side effects.
WHAT IT TESTS: ES module static analysis and package.json sideEffects for bundler dead-code elimination. ANSWER OUTLINE: Compile TypeScript to ESM via module:esnext to preserve import/export syntax. Declare sideEffects:false in package.json so webpack skips unused files. Avoid stateful re-exports and barrel files that execute on import. Ship ES module entry points and avoid CommonJS. RED FLAG: Claiming export const alone is enough, ignoring sideEffects, or using tsconfig module:CommonJS for browser libraries.
Read the original → webpack.js.org
- #typescript
- #webpack
- #tree-shaking
- #esm
- #library-authoring
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.