huozhi / bunchee

Zero config bundler for ECMAScript and TypeScript packages
https://npmjs.com/bunchee
859 stars 28 forks source link

Shared modules should inherit file extension (e.g. `.mjs`) #557

Open amannn opened 1 week ago

amannn commented 1 week ago

Another week, another bug report :).

I noticed when you have a build that generates .mjs files (since the package uses type: 'commonjs'), then shared modules of the entry points will use the .js extension. This results in consumer errors if the shared module has further imports since all modules that use ESM in this graph should use the .mjs extension.

Reproduction: ./dist/esm/core.mjs

In the linked example this works, but if utils-shared-BdVwpBM3.js would have another import (e.g. import Link from 'next/link') then this would break:

Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
amannn commented 1 week ago

Hmm, same seems to be true also for separate chunks that are created via the 'use client' split: react.mjs.

huozhi commented 4 days ago

I changed the feature to experimental, probably still need to use a separate entry to achieve it for now 😢 The spit chunks feature is quite limited for do this automatically with rollup. I'm still exploring the other ways to do it