Repository navigation
CommonJS plugin strictRequires settings always hoists NodeJS conditional require calls from an imported UMD file
#1381
Replies: 3 comments
|
For anyone interested, the workaround I've is to replace |
|
If I understand correctly, somehow Using the following basic setup, combining with the // rollup.config.js const { defineConfig } = require("rollup");
const commonjs = require("@rollup/plugin-commonjs");
module.exports = [
defineConfig({
input: "index.js",
context: this,
output: [
{
name: "rollup-dynamic-require",
file: `output.js`,
sourcemap: "inline",
format: "umd",
},
],
plugins: [
commonjs({
ignoreDynamicRequires: true, // this is the option for dynamic imports.
}),
],
}),
];// index.js const dynamicRequire = function (module) {
return require(module);
};
(function rollupDynamicRequire() {
let required;
for (const element of [0, 1, 2]) {
if (element === 0) {
required = dynamicRequire("http");
} else if (element === 1) {
required = dynamicRequire("https");
} else if (element === 2) {
required = dynamicRequire("os");
}
console.log(required.globalAgent.protocol);
}
})();// output.js (function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["rollup-dynamic-require"] = factory());
})(this, (function () { 'use strict';
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var rollupDynamicRequire = {};
var hasRequiredRollupDynamicRequire;
function requireRollupDynamicRequire () {
if (hasRequiredRollupDynamicRequire) return rollupDynamicRequire;
hasRequiredRollupDynamicRequire = 1;
const dynamicRequire = function (module) {
return require(module);
};
(function rollupDynamicRequire() {
let required;
for (const element of [0, 1, 2]) {
if (element === 0) {
required = dynamicRequire("http");
} else if (element === 1) {
required = dynamicRequire("https");
} else if (element === 2) {
required = dynamicRequire("os");
}
console.log(required.globalAgent.protocol);
}
})();
return rollupDynamicRequire;
}
var rollupDynamicRequireExports = requireRollupDynamicRequire();
var index = /*@__PURE__*/getDefaultExportFromCjs(rollupDynamicRequireExports);
return index;
})); |
|
For this particular UMD case, you can leave the library source unchanged and tell the CommonJS plugin not to convert that specific call: plugins: [
commonjs({
strictRequires: true,
ignore: ['os'],
}),
nodeResolve(),
],
I reproduced this with Rollup 4.60.0,
The distinction is that This does not polyfill |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello everyone,
Given a entry-point
index.js:And a UMD bundle (
umd.cjs) with "dynamic" require calls to NodeJS builtins that are called only under certain conditions:A rollup config
rollup.config.js:This results in the following final UMD bundle file:
The
umd.cjsrequirerequire('os');has been replaced with areturn require$$0;which changes the logic that is happening inside the bundle. This also causes the following errors:I believe this is somehow expected. However, if I use the setting
strictRequiresfrom the plugin@rollup/plugin-commonjsin the rollup.config.js:The expectation was to NOT hoist the require('os') call in the resulting final UMD file. In other words. From:
to something like (modified manually):
Paradoxically, an imported ES6 module file with dynamic require doesn't result in a hoisted require call.
Does anybody know how to avoid the hoisting of the file and how to fix the bundling errors?
All reactions