How to keep root level `export` when building with Vite in format `esm`

Viewed 337

it's a pretty simple requirement,

I hope the building result can keep the export statement in main.js,

so that I can load the script in a <script type="module"> tag and get the exported variable.

but now, the building result keeps import statement, but the export export default is lost.

any idea ?

// entry point 'main.js'
import { mount } from "./lifecycle";
export {mount};
export default mount();
// vite-config.js
export default defineConfig({
  plugins: [svelte()],
  build:{
    rollupOptions: {
      treeshake: false,
      output: {
        format: 'es'
      }
    }
  },
})
1 Answers

What you are probably looking for is preserveEntrySignatures. Set it to something other than false to keep the exports.

export default defineConfig({
  // ...
  build:{
    rollupOptions: {
      preserveEntrySignatures: 'strict',
      // ...
    }

By default Vite will set this to false unless it is operating in library or SSR mode.

Related