I am building an UMD library that is meant to be used only in browsers. However, it has a dependency on another UMD library/module/bundle .js file which contains require statements for nodejs modules. The statements aren't run always, but only if that sub-library detects it's running in NodeJS. For context, the required nodejs modules are os
, http
and https
but they are all required dynamically.
My rollup.config.js
is already using the @rollup/plugin-node-resolve
, @rollup/plugin-commonjs
and @rollup/plugin-typescript
. But when building it triggers some warnings.
(!) Missing shims for Node.js built-ins
Creating a browser bundle that depends on "os", "http" and "https". You might need to include https://github.com/FredKSchott/rollup-plugin-polyfill-node
(!) Missing global variable names
https://rollupjs.org/guide/en/#outputglobals
Use "output.globals" to specify browser global variable names corresponding to external modules:
os (guessing "require$$0")
http (guessing "require$$1")
https (guessing "require$$2")
The resulting generated bundle file:
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('os'), require('http'), require('https')) :
typeof define === 'function' && define.amd ? define(['exports', 'os', 'http', 'https'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.ns_ = global.ns_ || {}, global.ns_.Bundle = {}), global.require$$0, global.require$$1, global.require$$2));
})(this, (function (exports, require$$0, require$$1, require$$2) { 'use strict';
// ...
// Pseudo-code
if(node) {
const os = require('os');
//...
}
The bundler has replaced my require('os')
statements with require$$0
so it isn't calling the require statements "conditionally" as I would have expected. They have been pre-called (hoisted). Any way for Rollup to not do hoisting of that?
from Bundling with rollup a library that imports a legacy UMD library that uses conditional node require statements
No comments:
Post a Comment