eight04 / rollup-plugin-cjs-es

Convert CommonJS module into ES module.
MIT License
21 stars 0 forks source link

Is there a reason to consider this over @rollup/plugin-commonjs? #23

Open gajus opened 1 year ago

gajus commented 1 year ago

Stumbled upon this plugin by a coincidence and wondering if there is a reason for its existence.

eight04 commented 1 year ago

There are two major differences:

  1. This plugin throws require error at build time instead of run time so I can catch errors early.
  2. This plugin tends to generate cleaner output therefore rollup can remove dead code more efficiently.

https://github.com/rollup/rollup/issues/2201 https://github.com/rollup/rollup-plugin-commonjs/issues/311

gajus commented 1 year ago

Is the API compatible? Just thinking if there is an easy way to swap this in my setup. I am using Vite, which configures Rollup behind the scenes.

gajus commented 1 year ago

Gave this a quick try, and it failed with error:

Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(commonjs--resolver) resolveId "/Users/gajus/Developer/contra/gaia/node_modules/.pnpm/vite-plugin-ssr@0.4.73_vite@4.1.1/node_modules/vite-plugin-ssr/dist/esm/client/router/entry.js" undefined
(rollup-plugin-cjs-es) transform "/Users/gajus/Developer/contra/gaia/node_modules/.pnpm/vite-plugin-ssr@0.4.73_vite@4.1.1/node_modules/vite-plugin-ssr/dist/esm/client/router/entry.js"
(commonjs--resolver) resolveId "./useClientRouter" "/Users/gajus/Developer/contra/gaia/node_modules/.pnpm/vite-plugin-ssr@0.4.73_vite@4.1.1/node_modules/vite-plugin-ssr/dist/esm/client/router/entry.js"
(rollup-plugin-cjs-es) transform "/Users/gajus/Developer/contra/gaia/node_modules/.pnpm/vite-plugin-ssr@0.4.73_vite@4.1.1/node_modules/vite-plugin-ssr/dist/esm/client/router/useClientRouter.js"
...
(commonjs--resolver) resolveId "@react-aria/overlays" "/Users/gajus/Developer/contra/gaia/apps/contra-web-app/src/components/DialogSelect/DialogSelect.tsx"
    at EventEmitter.handleEmptyEventLoop (file:///Users/gajus/Developer/contra/gaia/node_modules/.pnpm/rollup@3.12.1/node_modules/rollup/dist/es/shared/rollup.js:24142:20)
    at Object.onceWrapper (node:events:627:28)
    at EventEmitter.emit (node:events:513:28)
    at process.<anonymous> (file:///Users/gajus/Developer/contra/gaia/node_modules/.pnpm/rollup@3.12.1/node_modules/rollup/dist/es/shared/rollup.js:24136:55)
    at process.emit (node:events:525:35)
 ELIFECYCLE  Command failed with exit code 1.
gajus commented 1 year ago

I've gotten past that error. However, now getting a new error.

rror: react/jsx-dev-runtime is not loaded.
Error: '../../node_modules/.pnpm/react-relay@14.1.0_react@18.2.0/node_modules/react-relay/index.js' doesn't export names expected by 'src/pages/projects.page.client.tsx'
Error: '../../node_modules/.pnpm/react-relay@14.1.0_react@18.2.0/node_modules/react-relay/index.js' doesn't export names expected by 'src/pages/opportunities.page.client.tsx'
Error: '../../node_modules/.pnpm/react-relay@14.1.0_react@18.2.0/node_modules/react-relay/index.js' doesn't export names expected by 'src/pages/settings.page.client.tsx'
Error: '../../node_modules/.pnpm/react-relay@14.1.0_react@18.2.0/node_modules/react-relay/index.js' doesn't export names expected by 'src/pages/:username/opportunities.page.client.tsx'
Error: '../../node_modules/.pnpm/react-relay@14.1.0_react@18.2.0/node_modules/react-relay/index.js' doesn't export names expected by 'src/pages/:username/projects.page.client.tsx'
Error: '../../node_modules/.pnpm/react-relay@14.1.0_react@18.2.0/node_modules/react-relay/index.js' doesn't export names expected by 'src/pages/:username/recommendations.page.client.tsx'
[..]

and eventually failing with:

"default" is not exported by "../../node_modules/.pnpm/localforage@1.10.0/node_modules/localforage/dist/localforage.js", imported by "src/services/async-storage.ts".
file: /Users/gajus/Developer/contra/gaia/apps/contra-web-app/src/services/async-storage.ts:1:7
1: import localForage from 'localforage';
          ^
2:
3: const CWA_STORAGE_DB_NAME = 'cwa';
error during build:
RollupError: "default" is not exported by "../../node_modules/.pnpm/localforage@1.10.0/node_modules/localforage/dist/localforage.js", imported by "sr

Need to spend more time familiarizing with the config.

eight04 commented 1 year ago

Try turning on the nested option. Here is an example repo: https://github.com/eight04/node-test/tree/cjs-es-localforage

eight04 commented 1 year ago
Error: '../../node_modules/.pnpm/react-relay@14.1.0_react@18.2.0/node_modules/react-relay/index.js' doesn't export names expected by 'src/pages/projects.page.client.tsx'

This is because that cjs-es is unable to transform react-relay/index.js to named exports, and projects.page.client.tsx tries to import a named member from react-relay/index.js.

https://unpkg.com/react-relay@14.1.0/index.js The entry point contains a single module.exports statement, which will be transformed into something like:

import * as _ from './lib/index.js';
export {_ as default};

Maybe we can make cjs-es convert this kind of syntax into

export * from './lib/index.js';
eight04 commented 1 year ago

Is the API compatible? Just thinking if there is an easy way to swap this in my setup. I am using Vite, which configures Rollup behind the scenes.

Nope. The workflow is also different. With cjs-es, you may have to run rollup twice so it can analyze the whole module graph then decide how to transform the module.

I have no experience with vite so I can't tell if it will work or not.