Bundle library with externalized dependencies and Vite?

87 views Asked by At

I'm trying to publish a library to npm that uses type: "module". I'm using vite's library mode with the following config:

export default defineConfig({
  css: {
    postcss: {
      plugins: [tailwindcss, autoprefixer, tailwindcss] as any,
    },
  },
  build: {
    lib: {
      entry: resolve(__dirname, "src/index.ts"),
      name: "lib_name",
      fileName: "lib_name",
    },
    rollupOptions: {
      output: [
        {
          dir: "dist/browser",
          name: "global_name",
          format: "iife",
          plugins: [],
        },
        {
          dir: "dist/esm",
          format: "esm",
          entryFileNames: "index.js",
          sourcemap: true,
        },
      ],
      external: ["@uppy/core"],
    },
  },
  plugins: [dts({ rollupTypes: true })] as any,
});

Ideally, I don't even want to bundle into one big file (but that's a different topic - preserveModules is not the solution here).

My understanding is that since my dependencies are clearly stated in package.json I do not need to bundle them. However, by default vite bundles everything and the resulting .esm.js file contains no import statements.

While this is not a big deal, it seems inefficient. Consumers need to install all dependencies anyway - why have the code twice?

How can I change my config so that external dependencies are imported rather than bundled?

Things I've tried

  • Use external rollout option as a function external: (id: string) => { return /^node_modules/.test(id)} as well as with values `["@uppy/core"]
0

There are 0 answers