-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Declare side-effecting modules so bundlers have better hints for what can be tree-shaken #21365
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
Open
NullVoxPopuli
wants to merge
6
commits into
main
Choose a base branch
from
nvp/configure-side-effects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c0b9975
Shrink hello-world bundle from 251 KB to 173 KB (-31%) via bundler hints
NullVoxPopuli 8d63c88
Add agadoo tree-shake regression check
NullVoxPopuli f124c5f
Drop dead PURE_INTERNAL_PACKAGES entries
NullVoxPopuli 2a0fac8
Specify the side-effects as per the linter
NullVoxPopuli d0e80b8
I haven't analyzed the dev modules
NullVoxPopuli 31fccff
Going back to 'false', because ember-auto-importt is all green with i
NullVoxPopuli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| /* eslint-disable no-console, n/no-process-exit */ | ||
| import { check } from 'agadoo'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); | ||
|
|
||
| // Entries that fully tree-shake today. A regression here means a new | ||
| // top-level side effect was introduced — fix it or move the entry to | ||
| // KNOWN_IMPURE_ENTRIES (and update package.json sideEffects accordingly). | ||
| const KNOWN_PURE_ENTRIES = [ | ||
| '@ember/-internals/browser-environment', | ||
| '@ember/-internals/error-handling', | ||
| '@ember/-internals/owner', | ||
| '@ember/-internals/utility-types', | ||
| '@ember/deprecated-features', | ||
| '@ember/destroyable', | ||
| '@ember/owner', | ||
| '@ember/reactive', | ||
| '@ember/template-compilation', | ||
| '@ember/test', | ||
| '@ember/version', | ||
| '@glimmer/destroyable', | ||
| '@glimmer/encoder', | ||
| '@glimmer/env', | ||
| '@glimmer/global-context', | ||
| '@glimmer/owner', | ||
| '@glimmer/util', | ||
| '@glimmer/vm', | ||
| '@glimmer/wire-format', | ||
| '@simple-dom/document', | ||
| 'backburner.js', | ||
| 'dag-map', | ||
| 'route-recognizer', | ||
| ]; | ||
|
|
||
| // Entries that have known top-level side effects today (manager | ||
| // registrations, validator state, backburner instance, etc.). These are | ||
| // tracked so that improvements get noticed: if one of these starts | ||
| // passing agadoo, promote it to KNOWN_PURE_ENTRIES. | ||
| const KNOWN_IMPURE_ENTRIES = [ | ||
| '@ember/-internals/container', | ||
| '@ember/-internals/deprecations', | ||
| '@ember/-internals/environment', | ||
| '@ember/-internals/glimmer', | ||
| '@ember/-internals/meta', | ||
| '@ember/-internals/metal', | ||
| '@ember/-internals/routing', | ||
| '@ember/-internals/runtime', | ||
| '@ember/-internals/string', | ||
| '@ember/-internals/utils', | ||
| '@ember/-internals/views', | ||
| '@ember/application', | ||
| '@ember/array', | ||
| '@ember/canary-features', | ||
| '@ember/component', | ||
| '@ember/controller', | ||
| '@ember/debug', | ||
| '@ember/engine', | ||
| '@ember/enumerable', | ||
| '@ember/helper', | ||
| '@ember/instrumentation', | ||
| '@ember/modifier', | ||
| '@ember/object', | ||
| '@ember/renderer', | ||
| '@ember/routing', | ||
| '@ember/runloop', | ||
| '@ember/service', | ||
| '@ember/template', | ||
| '@ember/template-compiler', | ||
| '@ember/template-factory', | ||
| '@ember/utils', | ||
| '@glimmer/manager', | ||
| '@glimmer/node', | ||
| '@glimmer/opcode-compiler', | ||
| '@glimmer/program', | ||
| '@glimmer/reference', | ||
| '@glimmer/runtime', | ||
| '@glimmer/tracking', | ||
| '@glimmer/validator', | ||
| 'ember-template-compiler', | ||
| 'ember-testing', | ||
| 'router_js', | ||
| 'rsvp', | ||
| ]; | ||
|
|
||
| function findAllEntries() { | ||
| const distRoot = path.join(root, 'dist/prod/packages'); | ||
| const entries = []; | ||
| function walk(dir, prefix) { | ||
| for (const name of fs.readdirSync(dir)) { | ||
| const full = path.join(dir, name); | ||
| const stat = fs.statSync(full); | ||
| if (stat.isDirectory()) { | ||
| const indexPath = path.join(full, 'index.js'); | ||
| if (fs.existsSync(indexPath)) { | ||
| entries.push(prefix ? `${prefix}/${name}` : name); | ||
| } else { | ||
| walk(full, prefix ? `${prefix}/${name}` : name); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| walk(distRoot, ''); | ||
| return entries.sort(); | ||
| } | ||
|
|
||
| const allEntries = findAllEntries(); | ||
| const tracked = new Set([...KNOWN_PURE_ENTRIES, ...KNOWN_IMPURE_ENTRIES]); | ||
| const untracked = allEntries.filter((e) => !tracked.has(e)); | ||
| const removed = [...tracked].filter((e) => !allEntries.includes(e)); | ||
|
|
||
| const purityRegressed = []; | ||
| const newlyPure = []; | ||
|
|
||
| for (const entry of KNOWN_PURE_ENTRIES) { | ||
| if (!allEntries.includes(entry)) continue; | ||
| const file = path.join(root, 'dist/prod/packages', entry, 'index.js'); | ||
| const result = await check(file); | ||
| if (result.shaken) { | ||
| console.log(` ✓ ${entry} (pure)`); | ||
| } else { | ||
| console.error(` ✗ ${entry} (expected pure, has side effects)`); | ||
| purityRegressed.push(entry); | ||
| } | ||
| } | ||
|
|
||
| for (const entry of KNOWN_IMPURE_ENTRIES) { | ||
| if (!allEntries.includes(entry)) continue; | ||
| const file = path.join(root, 'dist/prod/packages', entry, 'index.js'); | ||
| const result = await check(file); | ||
| if (result.shaken) { | ||
| console.error(` ! ${entry} (expected impure, now pure — promote it!)`); | ||
| newlyPure.push(entry); | ||
| } else { | ||
| console.log(` · ${entry} (impure, as expected)`); | ||
| } | ||
| } | ||
|
|
||
| let exitCode = 0; | ||
|
|
||
| if (purityRegressed.length > 0) { | ||
| console.error( | ||
| `\n${purityRegressed.length} entr${purityRegressed.length === 1 ? 'y' : 'ies'} regressed (was pure, now impure):` | ||
| ); | ||
| for (const entry of purityRegressed) console.error(` - ${entry}`); | ||
| console.error( | ||
| `\nA new top-level side effect was introduced. Either:\n` + | ||
| ` 1. Remove the side effect (preferred), or\n` + | ||
| ` 2. If the side effect is intentional, move the entry from\n` + | ||
| ` KNOWN_PURE_ENTRIES to KNOWN_IMPURE_ENTRIES in\n` + | ||
| ` bin/check-tree-shake.mjs.` | ||
| ); | ||
| exitCode = 1; | ||
| } | ||
|
|
||
| if (newlyPure.length > 0) { | ||
| console.error( | ||
| `\n${newlyPure.length} entr${newlyPure.length === 1 ? 'y' : 'ies'} unexpectedly tree-shake (was impure, now pure):` | ||
| ); | ||
| for (const entry of newlyPure) console.error(` - ${entry}`); | ||
| console.error( | ||
| `\nThis is good news — those packages got smaller. Promote them by\n` + | ||
| `moving them from KNOWN_IMPURE_ENTRIES to KNOWN_PURE_ENTRIES in\n` + | ||
| `bin/check-tree-shake.mjs.` | ||
| ); | ||
| exitCode = 1; | ||
| } | ||
|
|
||
| if (untracked.length > 0) { | ||
| console.error(`\n${untracked.length} entr${untracked.length === 1 ? 'y' : 'ies'} not tracked:`); | ||
| for (const entry of untracked) console.error(` - ${entry}`); | ||
| console.error( | ||
| `\nNew package(s) appeared. Run agadoo manually and add each to either\n` + | ||
| `KNOWN_PURE_ENTRIES or KNOWN_IMPURE_ENTRIES in bin/check-tree-shake.mjs.` | ||
| ); | ||
| exitCode = 1; | ||
| } | ||
|
|
||
| if (removed.length > 0) { | ||
| console.error( | ||
| `\n${removed.length} tracked entr${removed.length === 1 ? 'y' : 'ies'} no longer build:` | ||
| ); | ||
| for (const entry of removed) console.error(` - ${entry}`); | ||
| console.error(`\nRemove the stale entries from bin/check-tree-shake.mjs.`); | ||
| exitCode = 1; | ||
| } | ||
|
|
||
| if (exitCode === 0) { | ||
| console.log( | ||
| `\n${KNOWN_PURE_ENTRIES.length} pure + ${KNOWN_IMPURE_ENTRIES.length} impure entries match expectations.` | ||
| ); | ||
| } | ||
|
|
||
| process.exit(exitCode); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| diff --git a/index.js b/index.js | ||
| index 53354a5de59bb26283bb2cedb6ff5ff0ca6a1fa1..28777e32a9960785fcc31d79ade2a14b200f0cfe 100644 | ||
| --- a/index.js | ||
| +++ b/index.js | ||
| @@ -25,7 +25,7 @@ export async function check(input) { | ||
| const { code } = result.output[0]; | ||
|
|
||
| const ast = acorn.parse(code, { | ||
| - ecmaVersion: 11, | ||
| + ecmaVersion: 'latest', | ||
| sourceType: 'module' | ||
| }); | ||
|
|
||
| @@ -33,7 +33,7 @@ export async function check(input) { | ||
| return node.type !== 'ImportDeclaration'; | ||
| }); | ||
|
|
||
| - console.log(code); | ||
| +// console.log removed by patch | ||
|
|
||
| return { | ||
| shaken: nodes.length === 0 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a manual lint, of sorts