-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Library mode output not tree-shakeable when consumed by webpack #5174
Comments
Maybe this is related: #2071 (comment) |
I am seeing this too with a library build with Happy to provide an example repo if it helps. But FYI my library config looks like this: import path from "path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import dts from "vite-dts";
const isExternal = (id: string) => !id.startsWith(".") && !path.isAbsolute(id);
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react({
babel: {
plugins: ["formatjs"],
},
}),
dts(),
],
build: {
lib: {
entry: "./src/index.ts",
formats: ["es", "cjs"],
fileName: (format) => `index.${format}.js`,
},
rollupOptions: {
external: isExternal,
output: {
// Since we publish our ./src folder, there's no point
// in bloating sourcemaps with another copy of it.
sourcemapExcludeSources: true,
},
},
sourcemap: true,
minify: false,
},
}); My libraries The generated ES code seems to be littered with |
Has anyone found a solution to this? I'm in the same pickle with tree shaking vite library into a webpack project. I have sideEffects: false set in package.json within the component library. |
For background into Webpack's behavior, A solution to this is to not bundle, via Rollup's // vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
build: {
minify: false,
target: 'esnext', // use w/e here but don't set it too loose
lib: {
fileName: '[name]',
},
rollupOptions: {
output: {
preserveModules: true,
},
},
},
}) |
The unused code is not removed in Webpack because terser does not remove it. var cat = /* @__PURE__ */ a("cat");
var dog = /* @__PURE__ */ a("dog");
var fish = /* @__PURE__ */ a("fish");
var components = /* @__PURE__ */ Object.freeze({
__proto__: null,
[Symbol.toStringTag]: "Module",
Cat: cat,
Dog: dog,
Fish: fish
}); This is the output from terser for the code above. var g=a("cat"),o=a("dog"),t=a("fish");Symbol.toStringTag; If you set Symbol.toStringTag;
Vite could set |
Has anyone found a solution to this? |
I was experiencing two issues when building with
tl;dr to enable tree-shaking
How to validate: In your Will be tree shaken:
Will Not be tree-shaken
|
I had the same problem building my library with Vite lib mode .. Either, @hoop71 solution works but you have to mark everything with Either, don't use lib mode and use rollup like I did :
|
Describe the bug
When writing a library using vite "library mode," the output is expected to be tree-shakeable regardless of where it is consumed. When consumed by vite, things work properly, but when consumed by webpack, the output is not tree-shakeable.
The repo provided has a folder for a very simple vue library built by vite, which should be tree shakeable. It also has two more folders with sample apps (one vite app, one vue cli app) that attempt to use the library. The sample repo's README should explain in more detail. Both sample apps are minimal, using the default output from
npm init vite
orvue create
. The sample library uses the recommended configuration from the vite docs for library mode.A bit of history behind tracking down this bug can be found here - the TL;DR is that the
namespaceToStringTag: true
option in the vite build process emits[Symbol.toStringTag]: "Module",
in the output, and webpack chokes on it. Removing/commenting that line makes webpack in the vue cli app happy again, and I can't see any ill effects in the vite app, though my tests are rudimentary.Reproduction
https://github.com/mgdodge/vue-compiler-treeshaking-bug
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: