Skip to content

Commit 6dd53c0

Browse files
committed
Only add dynamic loader code when dynamic feature is enabled
1 parent c88be13 commit 6dd53c0

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

packages/commonjs/src/helpers.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ export const HELPERS_ID = '\0commonjsHelpers.js';
1717
export const HELPERS = `
1818
export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
1919
20-
export function commonjsRegister (path, loader) {
21-
DYNAMIC_REQUIRE_LOADERS[path] = loader;
22-
}
23-
2420
export function unwrapExports (x) {
2521
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
2622
}
@@ -32,6 +28,18 @@ export function createCommonjsModule(fn, module) {
3228
export function getCjsExportFromNamespace (n) {
3329
return n && n['default'] || n;
3430
}
31+
`;
32+
33+
export const HELPER_NON_DYNAMIC = `
34+
export function commonjsRequire () {
35+
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
36+
}
37+
`;
38+
39+
export const HELPERS_DYNAMIC = `
40+
export function commonjsRegister (path, loader) {
41+
DYNAMIC_REQUIRE_LOADERS[path] = loader;
42+
}
3543
3644
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
3745
const DYNAMIC_REQUIRE_CACHE = Object.create(null);
@@ -63,6 +71,7 @@ function normalize (path) {
6371
path = '.';
6472
return path;
6573
}
74+
6675
function join () {
6776
if (arguments.length === 0)
6877
return '.';
@@ -81,6 +90,7 @@ function join () {
8190
8291
return joined;
8392
}
93+
8494
function isPossibleNodeModulesPath (modulePath) {
8595
let c0 = modulePath[0];
8696
if (c0 === '/' || c0 === '\\\\') return false;
@@ -91,6 +101,7 @@ function isPossibleNodeModulesPath (modulePath) {
91101
return false;
92102
return true;
93103
}
104+
94105
export function commonjsRequire (path, originalModuleDir) {
95106
const shouldTryNodeModules = isPossibleNodeModulesPath(path);
96107
path = normalize(path);

packages/commonjs/src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
getIdFromProxyId,
1717
HELPERS,
1818
HELPERS_ID,
19+
HELPER_NON_DYNAMIC,
20+
HELPERS_DYNAMIC,
1921
PROXY_SUFFIX
2022
} from './helpers';
2123

@@ -146,7 +148,17 @@ export default function commonjs(options = {}) {
146148
resolveId,
147149

148150
load(id) {
149-
if (id === HELPERS_ID) return HELPERS;
151+
if (id === HELPERS_ID) {
152+
let code = HELPERS;
153+
154+
// Do not bloat everyone's code with the module manager code
155+
if (isDynamicRequireModulesEnabled)
156+
code += HELPERS_DYNAMIC;
157+
else
158+
code += HELPER_NON_DYNAMIC;
159+
160+
return code;
161+
}
150162

151163
// generate proxy modules
152164
if (id.endsWith(EXTERNAL_SUFFIX)) {

0 commit comments

Comments
 (0)