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
Fix tree-shaking wildcards with sideEffects: false #1682
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ const helpers = | |
class JSConcatPackager extends Packager { | ||
async start() { | ||
this.addedAssets = new Set(); | ||
this.assets = new Map(); | ||
this.exposedModules = new Set(); | ||
this.externalModules = new Set(); | ||
this.size = 0; | ||
|
@@ -43,6 +44,8 @@ class JSConcatPackager extends Packager { | |
this.needsPrelude = true; | ||
} | ||
|
||
this.assets.set(asset.id, asset); | ||
|
||
for (let mod of asset.depAssets.values()) { | ||
if ( | ||
!this.bundle.assets.has(mod) && | ||
|
@@ -88,19 +91,34 @@ class JSConcatPackager extends Packager { | |
for (let identifier in asset.cacheData.imports) { | ||
let [source, name] = asset.cacheData.imports[identifier]; | ||
let dep = asset.depAssets.get(asset.dependencies.get(source)); | ||
|
||
if (name === '*') { | ||
this.markUsedExports(dep); | ||
} | ||
|
||
this.markUsed(dep, name); | ||
} | ||
|
||
for (let source of asset.cacheData.wildcards) { | ||
let dep = asset.depAssets.get(asset.dependencies.get(source)); | ||
for (let exportIdentifier in dep.cacheData.exports) { | ||
this.markUsed(dep, exportIdentifier); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct? Do we really want to mark every export in the wildcard as used? Shouldn't it only be the one matching There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, it's a piece of code I added early but forgot to remove |
||
} | ||
} | ||
} | ||
|
||
markUsed(mod, id) { | ||
let exp = mod.cacheData.exports[id]; | ||
markUsed(mod, name) { | ||
let {id} = this.findExportModule(mod.id, name); | ||
mod = this.assets.get(id) || mod; | ||
|
||
let exp = mod.cacheData.exports[name]; | ||
if (Array.isArray(exp)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should always be false now right, since you already followed the exports through in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not in a code-splitted packager, doesn't really make sense to fallback on |
||
let depMod = mod.depAssets.get(mod.dependencies.get(exp[0])); | ||
return this.markUsed(depMod, exp[1]); | ||
} | ||
|
||
this.markUsedExports(mod); | ||
mod.usedExports.add(id); | ||
mod.usedExports.add(name); | ||
} | ||
|
||
getExportIdentifier(asset) { | ||
|
@@ -521,6 +539,54 @@ class JSConcatPackager extends Packager { | |
|
||
await super.write(output); | ||
} | ||
|
||
resolveModule(id, name) { | ||
let module = this.assets.get(+id); | ||
return module.depAssets.get(module.dependencies.get(name)); | ||
} | ||
|
||
findExportModule(id, name, replacements) { | ||
let asset = this.assets.get(+id); | ||
let exp = | ||
asset && | ||
Object.prototype.hasOwnProperty.call(asset.cacheData.exports, name) | ||
? asset.cacheData.exports[name] | ||
: null; | ||
|
||
// If this is a re-export, find the original module. | ||
if (Array.isArray(exp)) { | ||
let mod = this.resolveModule(id, exp[0]); | ||
return this.findExportModule(mod.id, exp[1], replacements); | ||
} | ||
|
||
// If this module exports wildcards, resolve the original module. | ||
// Default exports are excluded from wildcard exports. | ||
let wildcards = asset && asset.cacheData.wildcards; | ||
if (wildcards && name !== 'default' && name !== '*') { | ||
for (let source of wildcards) { | ||
let mod = this.resolveModule(id, source); | ||
let m = this.findExportModule(mod.id, name, replacements); | ||
if (m.identifier) { | ||
return m; | ||
} | ||
} | ||
} | ||
|
||
// If this is a wildcard import, resolve to the exports object. | ||
if (asset && name === '*') { | ||
exp = `$${id}$exports`; | ||
} | ||
|
||
if (replacements && replacements.has(exp)) { | ||
exp = replacements.get(exp); | ||
} | ||
|
||
return { | ||
identifier: exp, | ||
name, | ||
id | ||
}; | ||
} | ||
} | ||
|
||
module.exports = JSConcatPackager; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {foo} from 'bar' | ||
|
||
output = foo |
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
should this be in an
else
? Otherwise we pass*
as the name tomarkUsed
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.js
:b.js
: <==b.js
should bedep
here, if we removemarkUsed
it'll get trimmed because it doesn't have any exports/import in thecacheData
(just 2 elements inwildcards
)Using
*
seemed like a nice fix to me for this, as it'll keepusedImports.size > 0
and never conflict with existing import. I'm just getting familiar with this part though, so not really sure