Currently, I am in the process of packaging a library for npm that uses type: "module"
. To accomplish this, I have configured vite's library mode with the following settings:
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,
});
My goal is to avoid bundling everything into a single file. However, simply using preserveModules
does not provide a solution.
From what I understand, since my dependencies
are clearly outlined in the package.json
, there should be no need to bundle them. Yet, by default vite bundles all files, resulting in an .esm.js
file without any import
statements.
While this may seem insignificant, it does raise concerns about efficiency. After all, consumers will still need to install all dependencies separately - duplicating the code seems unnecessary.
Is there a way to update my configuration so that external dependencies are imported instead of bundled?
Here are some of the strategies I have experimented with:
- Utilizing the
external
option in the rollout configuration as both a function (
) and with specified values (["@uppy/core"])external: (id: string) => { return /^node_modules/.test(id)}