-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Support array in package.json's sideEffects property #1766
Support array in package.json's sideEffects property #1766
Conversation
@@ -125,7 +125,7 @@ class Asset { | |||
|
|||
async getPackage() { | |||
if (!this._package) { | |||
this._package = await this.getConfig(['package.json']); | |||
this._package = await this.resolver.findPackage(path.dirname(this.name)); |
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.
Why was this changed?
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.
Because findPackage()
will add a pkgdir
property to the package, which is needed later for globbing the sideEffects
array entries.
src/packagers/JSConcatPackager.js
Outdated
case 'boolean': | ||
return sideEffects; | ||
case 'string': | ||
if (process.platform === 'win32') { |
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.
You can use path.normalize(sideEffects)
instead of manually checking for windows and using replace
Oh, you're doing the opposite, nevermind
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.
Thinking again, this is not necessary at all. "sideEffects": ["foo\\bar.js"]
is not supported in package.json
anyway.
src/packagers/JSConcatPackager.js
Outdated
sideEffects = sideEffects.replace(/\\/g, '/'); | ||
} | ||
return mm.isMatch( | ||
path.relative((await asset.getPackage()).pkgdir, asset.name), |
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.
You can save some I/O and async
overhead by setting pkgdir
in cacheData
preemptively in hoist.js
like we do for sideEffects
:
parcel/src/scope-hoisting/hoist.js
Lines 34 to 38 in 7423541
asset.cacheData.imports = asset.cacheData.imports || Object.create(null); | |
asset.cacheData.exports = asset.cacheData.exports || Object.create(null); | |
asset.cacheData.wildcards = asset.cacheData.wildcards || []; | |
asset.cacheData.sideEffects = | |
asset._package && asset._package.sideEffects; |
src/packagers/JSConcatPackager.js
Outdated
@@ -23,6 +24,31 @@ const helpers = | |||
.readFileSync(path.join(__dirname, '../builtins/helpers.js'), 'utf8') | |||
.trim() + '\n'; | |||
|
|||
async function hasSideEffects(asset, sideEffects) { |
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.
nit: defaulting sideEffects
using {sideEffects} = asset.cacheData
could make the if
conditions using it cleaner (await hasSideEffects(asset)
)
d7ccd48
to
69859fb
Compare
@fathyb Thanks for the review. I have addressed all your comments. |
src/packagers/JSConcatPackager.js
Outdated
@@ -23,6 +24,25 @@ const helpers = | |||
.readFileSync(path.join(__dirname, '../builtins/helpers.js'), 'utf8') | |||
.trim() + '\n'; | |||
|
|||
function hasSideEffects(asset, {sideEffects} = asset.cacheData) { |
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.
this function could probably go in hoist.js
. That way asset.cacheData.sideEffects
could always be a boolean computed using this function. Might be slightly faster, especially since we can cache the results.
src/Asset.js
Outdated
this._package.pkgdir = path.dirname( | ||
await config.resolve(this.name, ['package.json']) | ||
); | ||
} |
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.
I actually liked your original solution using the resolver. This one will result in two lookups.
69859fb
to
870edf7
Compare
Thanks @devongovett for your review. I have addressed your comments as well now. |
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.
LGTM, thanks for working on this 👍
This pull request implements array support for the
package.json
'ssideEffects
property, like webpack does. Example:Fixes #1566.