-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
[Vite version 3.0.0-alpha.9]: Treeshaking does not work with libs. #8464
Comments
The solution I found to this, prior to Vite Alpha-11 was to provide all files as entry point within build -> rollupOptions -> input as well as correcting import paths manually. At Alpha-11 I had to implement build -> rollupOptions -> output -> manualChunks with the following to resolve this issue.
import { extname, resolve } from "path";
export class ManualChuncker {
// Project
private static readonly SupportedFiles = [
"ts",
"vue",
"scss",
"scss?inline",
"js",
"json",
];
private static readonly SourceRoot = resolve(
__dirname,
"..",
"library",
"api"
);
private static readonly NodeModulesRoot = resolve(
__dirname,
"..",
"node_modules"
);
// Generated
private static readonly AssetDir = "_assets/";
private static readonly NodeModuleDir = "_external/";
private static readonly PluginDir = "_plugins/";
private static readonly VueDir = "_vue/";
public static resolve(path: string) {
return new ManualChuncker(path).resolve();
}
constructor(public readonly path: string) {}
public resolve(): string | void {
return this.moduleName;
}
private get moduleName() {
const CleanPath = this.path.substring(
0,
this.path.length - extname(this.path).length
);
if (this.isNodeModule) {
return `${ManualChuncker.NodeModuleDir}${CleanPath.substring(
ManualChuncker.NodeModulesRoot.length + 1
)}`;
}
if (this.isVue) {
return `${ManualChuncker.VueDir}${CleanPath.substring(
ManualChuncker.SourceRoot.length + 1
)}`;
}
if (this.isAsset) {
return `${ManualChuncker.AssetDir}${CleanPath.substring(
ManualChuncker.SourceRoot.length + 1
)}`;
}
if (this.isPlugin) {
return `${ManualChuncker.PluginDir}${this.path}`;
}
return CleanPath.substring(ManualChuncker.SourceRoot.length + 1);
}
private get isNodeModule() {
return this.path.startsWith(ManualChuncker.NodeModulesRoot);
}
private get isAsset() {
return (
this.fileType === "scss" ||
this.fileType === "json" ||
this.fileType === "scss?inline"
);
}
private get isVue() {
return this.path.endsWith(".vue");
}
private get isPlugin() {
return !ManualChuncker.SupportedFiles.map((ext) =>
this.path.endsWith(`.${ext}`)
).includes(true);
}
private get fileType():
| typeof ManualChuncker["SupportedFiles"][number]
| null {
const ext = extname(this.path).substring(1);
if (ManualChuncker.SupportedFiles.includes(ext)) {
return ext as typeof ManualChuncker["SupportedFiles"][number];
}
return null;
}
} P.S. |
I'm not sure if it's related to this problem. When I run vite build for the first time, the packaged files are obviously not as big as those of the second vite build. I can't reproduce this issue in a newly created project, below are screenshots of two runs
I suspect it has something to do with the cache. When I set optimizeDeps.force to true, the files output by each build are different but the size is very close |
^ I have the same problem |
@ckvv @consegrado, would you share a repro as minimal as possible so we can build a bench for these issues. Thanks! |
@patak-dev |
@ckvv would you try with: |
Have you tried #8965? Please open a new issue with a reproduction if you find a bug when using it |
@patak-dev |
Thanks for testing @ckvv, we'll review the tree-shaking issues and try to flip the default in v4 to optimize deps again 👍🏼 |
tree shaking does not work in 3.0.0-beta.8 and 3.0.0-beta.10 |
Have you tried 3.0.0 (non-beta/alpha)? Wasn't previously working for me with the alpha, but works perfectly fine with the major release. |
I tried 3.0.0-beta.8 and 3.0.0-beta.10 and 3.0.0 and 3.0.2,tree shaking does not work |
@patak-dev I recently hit a similar issue, where I had a third party library which could have been completely tree shaken out but was not. And enabling dep optimiser manually did fix the issue. It was a bit weird for me, as I expected better tree shaking without dep optimisation, as I believe dep optimisation is done without knowledge of what is actually imported from the lib. |
I'm on vite 3.1.0 and tree shaking not working at all
will includes all the lodash functions |
@kresli please share your configuration, I had to manually enable dependency optimisation to get tree shaking in lib mode {
optimizeDeps: {
disabled: false
},
build: {
commonjsOptions: {
include: [],
},
rollupOptions: {
treeshake: 'smallest',
}
}
} |
@kresli tree-shaking doesn't work on |
Describe the bug
[Vite version 3.0.0-alpha.9]: Cannot treeshaking libs.
[Vite version 2.9.9]: Its ok.
Reproduction
https://stackblitz.com/edit/vitejs-vite-bqjtx1?file=src%2FApp.tsx,vite.config.ts,stats.html
System Info
Used Package Manager
pnpm
Logs
Validations
The text was updated successfully, but these errors were encountered: