Skip to content
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

When packaging a library with Vite@v6, the built code loses the named exports #18574

Closed
7 tasks done
condorheroblog opened this issue Nov 5, 2024 · 2 comments · Fixed by #18577
Closed
7 tasks done

Comments

@condorheroblog
Copy link

Describe the bug

I use Vite@v6 to package [email protected] to output IIFE format code. The code built in Vite@v5 contains named exports, but these named exports are lost with Vite@v6, leaving only a default export.

Here are the links to reproduce in v5 and v6:

  1. https://stackblitz.com/edit/stackblitz-starters-qsvdae?file=index.js
  2. https://stackblitz.com/edit/stackblitz-starters-3lubdz?file=index.js

image

The output above is just a different version of Vite. Using the same code, it is as follows

import { createRequire } from 'node:module';
import { build } from 'vite';

const require = createRequire(import.meta.url);

export async function buildPackage() {
  // Call the build function with build configurations
  const result = await build({
    // Do not use a configuration file
    configFile: false,
    build: {
      // Do not output files
      write: false,
      lib: {
        // Entry file is the specified package path
        entry: require.resolve('path-to-regexp'),
        // Convert package name to camel case
        name: 'pathToRegexp',
        // Output format is iife
        formats: ['iife'],
        // Output file name is the same as the package name
        fileName: 'path-to-regexp',
      },
      rollupOptions: {
        output: {
          // Export method is named export
          exports: 'named',
          // Allow extension of existing global variables
          extend: true,
        },
      },
      // Do not minify
      minify: false,
    },
  });

  // Check if result is an array, if so, take the first element, otherwise use result directly
  const _result = Array.isArray(result) ? result[0] : result;

  // If the _result does not have an output property, return
  if (!('output' in _result)) {
    return;
  }

  return _result.output[0].code;
}

console.log(await buildPackage());

console.log(`Hello Node.js v${process.versions.node}!`);

Reproduction

https://stackblitz.com/edit/stackblitz-starters-qsvdae?file=index.js

Steps to reproduce

Please npm run build

System Info

-

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

It seems rollup/plugins#1639 is affecting this. By setting commonjsOptions.strictRequires: 'auto', the other exports are declared in the output.

It seems the change is expected but the document only mentions about ESM output.
rollup/plugins@8f02987#diff-8d9594ca9d2280f57b2af5725720f6b0f76eabf4b137fe80d3da13d02c6f78fd

@sapphi-red
Copy link
Member

sapphi-red commented Nov 5, 2024

The README doesn't say about IIFE, but it seems output.exports: 'default' works as well.

When bundling to CommonJS, i.e output.format === 'cjs', make sure that you do not set output.exports to 'named'. The default value of 'auto' will usually work, but you can also set it explicitly to 'default'. That makes sure that Rollup assigns the default export that was generated for your CommonJS entry point to module.exports, and semantics do not change.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants