Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Rollup Dependencies that Use Node Globals #163

Closed
CMCDragonkai opened this issue Jun 3, 2018 · 10 comments
Closed

Rollup Dependencies that Use Node Globals #163

CMCDragonkai opened this issue Jun 3, 2018 · 10 comments

Comments

@CMCDragonkai
Copy link

CMCDragonkai commented Jun 3, 2018

I have third party dependencies that use node globals like process. They don't explicitly require the module using var process = require('process'); so Rollup cannot find them.

I have a process shim in my devDependencies. And for the dependencies that do in fact use var process = require('process'); it works and rewrites them to getting the process shim. However it renames the object to browser for some reason.

How do I do either:

  1. Make the shim process be named process instead of being named browser OR
  2. How do I explicitly inject a global called process to be resolved to the process shim (in this case currently named browser in the UMD module)?
@TrySound
Copy link
Member

TrySound commented Jun 3, 2018

Are you sure you didn't forget about this?

import replace from 'rollup-plugin-replace';

replace({
  'process.env.NODE_ENV': JSON.stringify('production')
})

@CMCDragonkai
Copy link
Author

@TrySound That's for strings? I have things using process.* where * is some function.

@CMCDragonkai
Copy link
Author

To further elaborate. This plugin has an option: preferBuiltins: true/false. If I leave it true, it will leave the globals as unresolved dependencies and missing global variable names.

Because I have devDependencies pointing to packages with the appropriate names: buffer, event, path and process. I have the preferBuiltins: false. This means Rollup generally finds areas where there are explicit import or require on these global modules. However Rollup doesn't find cases when the globals are just used implicitly without and explicit import nor require.

So I'm trying to find a way to be able to use preferBuiltins: false and still resolve those global identifiers. Or I use preferBuiltins: true and and try to manually resolve all these global dependencies. But that still doesn't resolve the actual names like process that is used implicitly.

@TrySound
Copy link
Member

TrySound commented Jun 3, 2018

@CMCDragonkai
Copy link
Author

I have tried that. What ends up happening is that the implicit uses of process end up using the shim provided by that plugin, whereas the explicit uses of process use the actual process shim that I have in my devDependencies. While this may not be a problem, it ends up being one for Buffer because there is now 2 implementations of Buffer in the project.

@mmomtchev
Copy link

mmomtchev commented Jan 6, 2019

I have the same problem when building for node.js (I am just trying to build a single .min.js file so that I don't have to manage node_modules in production) - I have preferBuiltins: true and I still get a Buffer shim in my output, so I end up with 2 implementations of Buffer depending how you use it.
process is fine tho.

@mmomtchev
Copy link

This ended being a case-sensitivity issue - I have a Buffer shim called buffer in my node_modules (because someone required it). So when bundling for node.js this shim gets included even with preferBuiltins: true. Adding "Buffer" to externals doesn't change anything, but adding "buffer" solves the issue.

@dima-takoy-zz
Copy link

dima-takoy-zz commented May 5, 2019

This ended being a case-sensitivity issue - I have a Buffer shim called buffer in my node_modules (because someone required it). So when bundling for node.js this shim gets included even with preferBuiltins: true. Adding "Buffer" to externals doesn't change anything, but adding "buffer" solves the issue.

its OK, because npm package name can be only in lowercase.

Can it be closed for now?

@bterlson
Copy link
Contributor

bterlson commented Jul 3, 2019

Fwiw you can use rollup-plugin-inject to generate explicit imports for implicit dependencies on buffer and process.

@bterlson
Copy link
Contributor

Closing this issue as I believe the original question has been answered.

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

No branches or pull requests

5 participants