rollup-plugin-node-externals icon indicating copy to clipboard operation
rollup-plugin-node-externals copied to clipboard

7.1.3 --> 8.0.0 regression: possibly pnpm related?

Open zmullett opened this issue 11 months ago • 3 comments

I build using vite and pnpm. In 7.1.3 everything was WAI with no additional options specified.

With 8.0.0 my builds are broken and the source map includes entries such as:

../lib/some_usual_and_expected_file.ts
../node_modules/.pnpm/@[email protected]/node_modules/@lit/reactive-element/css-tag.js
../node_modules/.pnpm/[email protected]/node_modules/lodash-es/_root.js

Has something regressed in terms of pnpm support?

zmullett avatar Mar 10 '25 14:03 zmullett

Has something regressed in terms of pnpm support?

Nothing that I am aware of.

Is your build still ok with 7.1.3?

Septh avatar Mar 10 '25 23:03 Septh

Yes, the build still works at 7.1.3.

zmullett avatar Mar 12 '25 06:03 zmullett

Was afraid you would say that :)

Could you please provide a repro so I can investigate?

Septh avatar Mar 14 '25 03:03 Septh

Finally got back to this 😅

Steps to reproduce:

  1. Clone https://github.com/zmullett/rollup-plugin-node-externals-v8-repro.
  2. Ensure you have pnpm installed.
  3. Run pnpm i - observe that rollup-plugin-node-externals@7 is used.
  4. Run pnpm build - first observe that the generated file dist/rollup-plugin-node-externals-v8-repro.mjs has only an import statement - this is the expected behavior.
  5. Run pnpm i rollup-plugin-node-externals@8 to upgrade.
  6. Run pnpm build again - now observe that the generated file dist/rollup-plugin-node-externals-v8-repro.mjs includes the module that should only have been imported as earlier - this is the bug.

zmullett avatar Jun 08 '25 17:06 zmullett

Thank you for the repro.

From what I see, this is not an issue with pnpm but with Vite.

If you move the plugin from build.rollupOptions.plugins to the top-level plugins array in vite.config.js:

export default defineConfig({
  build: {
    lib: {
      entry: resolve('index.js'),
      formats: ['es'],
    },
    rollupOptions: {
      plugins: [
        // nodeExternals() // <-- move from here...
      ],
    },
  },
  plugins: [
    nodeExternals()        // <-- to here
  ]
})

then the build works as expected.

I am not sure why but clearly, Vite treats Rollup plugins a little differently.

7.1.3 did work because the resolveId hook had a order: 'pre' property; however, it caused other problems and was removed in 8.0.0 (see #33).

Right now, I don't know what I can do about this. I think the simple workaround is to either use the top-level plugins array as shown above, or stick to 7.1.3 if it works for you. 🤷‍♂️ I'm going to stress that point in the docs.

Septh avatar Jun 17 '25 10:06 Septh

Confirmed, that works. Thanks for debugging and finding that difference between the plugins lists, ugh! I'll leave the ticket open in case you want to track that docs update with it.

zmullett avatar Jun 17 '25 11:06 zmullett

FWIW, I found this in Vite documentation:

If a Rollup plugin only makes sense for the build phase, then it can be specified under build.rollupOptions.plugins instead. It will work the same as a Vite plugin with enforce: 'post' and apply: 'build'.

8.0.1 is out with an updated README, now closing this issue. Thanks again!

Septh avatar Jun 17 '25 18:06 Septh