Skip to content

Instantly share code, notes, and snippets.

@jasenmichael
Created January 5, 2024 18:42
Show Gist options
  • Save jasenmichael/58dd3a4f8e7ec1d003e88907bba392d7 to your computer and use it in GitHub Desktop.
Save jasenmichael/58dd3a4f8e7ec1d003e88907bba392d7 to your computer and use it in GitHub Desktop.
Folder as entry in library mode for multiple independent js modules
./src/
  src/cli.ts
  src/index.ts
./vite.config.ts

vite.config.ts

import { resolve } from "node:path"
import { defineConfig } from "vite"
import { glob } from "glob"

export default defineConfig({
  build: {
    lib: {
      entry: glob.sync(resolve(__dirname, 'src/*.ts')),
      formats: ["es", "cjs"],
      fileName: (format, name) => `${name}.${format === 'es' ? 'mjs' : 'cjs'}`,
    },
  }
});

will produce:

./dist/
  cli.cjs
  cli.mjs
  index.cjs
  index.mjs
./src/
  cli.ts
  index.ts
./vite.config.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment