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

tree-shaking does not work #28

Closed
eleven-net-cn opened this issue Nov 22, 2022 · 14 comments
Closed

tree-shaking does not work #28

eleven-net-cn opened this issue Nov 22, 2022 · 14 comments

Comments

@eleven-net-cn
Copy link

The same as: WarrenJones/vite-plugin-require-transform#29

@eleven-net-cn
Copy link
Author

Even if set false, e.g.:

if (false) {
  require('axios');
  console.log('tree-shaking');
}

require('axios'); still added at the top, and console.log('tree-shaking'); has been removed.

@wangzongming
Copy link
Owner

Even if set false, e.g.:

if (false) {
  require('axios');
  console.log('tree-shaking');
}

require('axios'); still added at the top, and console.log('tree-shaking'); has been removed.

Yes, because the address of the require is imported using import at the top of the code during the compilation phase of the code.

require doesn't actually work when the code is running.

At present, I don't have a good idea to solve this problem. Let me think about it.

wangzongming pushed a commit that referenced this issue Nov 25, 2022
@wangzongming
Copy link
Owner

Update to 1.1.10,edit config.

vitePluginRequire({
translateType: "importMetaUrl"
}),

@eleven-net-cn
Copy link
Author

eleven-net-cn commented Nov 25, 2022

The error still exists with v1.1.10, and use specified params (translateType: "importMetaUrl").

@wangzongming

@wangzongming
Copy link
Owner

I have passed the test here, please provide the repeat case

@eleven-net-cn
Copy link
Author

eleven-net-cn commented Nov 28, 2022

I have passed the test here, please provide the repeat case

Here it is: https://github.com/eleven-net-cn/vite-require-plugin-error

The demo is build library mode (vite)

Before, Tree-shaking does not work fine, but, when I tested it just now, It was worked fine (Tree-Shaking) when set false, and does not work fine (require syntax) when set true, require is converted to new URL().

Please run npm run build to view result (dist).

@wangzongming
Copy link
Owner

I have passed the test here, please provide the repeat case

Here it is: https://github.com/eleven-net-cn/vite-require-plugin-error

The demo is build library mode (vite)

Before, Tree-shaking does not work fine, but, when I tested it just now, It was worked fine (Tree-Shaking) when set false, and does not work fine (require syntax) when set true, require is converted to new URL().

Please run npm run build to view result (dist).

When 'false', the code is not deleted, but it is not referenced at the top. Components can only do so much.

@eleven-net-cn
Copy link
Author

The current solution does not solve the problem, the behavior is not as expected.

Suggest reopen this issues until better resolved.

@wangzongming
Copy link
Owner

The current solution does not solve the problem, the behavior is not as expected.

Suggest reopen this issues until better resolved.

Running code plug-ins cannot parse. The plug-in will only run during the code compilation phase. If you have a better solution, you can suggest a pr to me

@eleven-net-cn
Copy link
Author

Until this problem is actually fixed, keep it open to help other users know more information.

I will read the source code carefully to understand more detail.

@wangzongming
Copy link
Owner

Until this problem is actually fixed, keep it open to help other users know more information.

I will read the source code carefully to understand more detail.

This is not a plug-in to solve the problem, there is no need to open.
As I said above, plug-ins are only run during the compile code phase.
If you don't cut your code while it's running.

@eleven-net-cn
Copy link
Author

eleven-net-cn commented Nov 29, 2022

I see what you mean, this plugin cannot override or resolve it.

It is recommended to specify this case in README, even if this plugin cannot resolve.

This can lead to larger bundle sizes, or debugging code to be bundle into production.

Because most users often don't know these details.

@wangzongming
Copy link
Owner

I see what you mean, this plugin cannot override or resolve it.

It is recommended to specify this case in README, even if this plugin cannot resolve.

This can lead to larger bundle sizes, or debugging code to be bundle into production.

Because most users often done't know these details.

Well, I'll change the document later.

@eleven-net-cn
Copy link
Author

Awesome thanks

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

No branches or pull requests

2 participants