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

about tree-shaking #4488

Closed
wdssmq opened this issue May 5, 2022 · 5 comments · Fixed by #4511
Closed

about tree-shaking #4488

wdssmq opened this issue May 5, 2022 · 5 comments · Fixed by #4511

Comments

@wdssmq
Copy link

wdssmq commented May 5, 2022

// base.js
const str1 = "1111";

const str2 = "2222".trim();

const str3 = `3333`;

const str4 = `4444`.trim();

const $ = window.$ || unsafeWindow.$;

export { str1, str2, str3, str4, $ };
// main.js
import { str1, str2, str3, str4, $ } from "./base";

console.log(str1);

bundle:

(function () {
	'use strict';

	// base.js
	const str1 = "1111";

`4444`	.trim();

	window.$ || unsafeWindow.$;

	// main.js

	console.log(str1);

})();

如何不输出「4444 .trim();」和 window.$ || unsafeWindow.$;两行?

How to not output 「4444 .trim();」and window.$ || unsafeWindow.$;?

@dnalborczyk
Copy link
Contributor

dnalborczyk commented May 6, 2022

I'll let others chime in, but this const str4 = `4444`.trim(); appears to be a bug, or a feature request.

the second const $ = window.$ || unsafeWindow.$; looks correct as window.$ and the global unsafeWindow.$ could have potential side effects.

that said, I think I still see a bug, unless it's by design:

I believe this alone should not be tree-shaken: const $ = window.$, as a $ getter could have side effects.

https://rollupjs.org/repl/?version=2.72.0&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmNvbnN0JTIwJTI0JTIwJTNEJTIwd2luZG93LiUyNCUyMiUyQyUyMmlzRW50cnklMjIlM0F0cnVlJTdEJTVEJTJDJTIyb3B0aW9ucyUyMiUzQSU3QiUyMmZvcm1hdCUyMiUzQSUyMmVzJTIyJTJDJTIybmFtZSUyMiUzQSUyMm15QnVuZGxlJTIyJTJDJTIyYW1kJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlMkMlMjJnbG9iYWxzJTIyJTNBJTdCJTdEJTdEJTJDJTIyZXhhbXBsZSUyMiUzQW51bGwlN0Q=

@wdssmq
Copy link
Author

wdssmq commented May 6, 2022

@dnalborczyk
Thanks for the reply.

Some of the older monkey scripts use jQuery, which I will gradually replace with native js, or comment out.

The other issue does look like a bug, and may need further confirmation.

@lukastaegert
Copy link
Member

As for the window question, Rollup does not know the $ global and more importantly does not know the unsafeWindow global. Accessing a non-existing global will throw an error, so Rollup needs to keep the code. Setting treeshake.unknownGlobalSideEffects to false will probably fix this, but maybe you also need treeshake.propertyReadSideEffects, see https://rollupjs.org/guide/en/#treeshake.

As for retaining .trim(), this will be fixed by #4511

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

Successfully merging a pull request may close this issue.

3 participants