@rollup/plugin-commonjs 19.0.0 → 19.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -160,8 +160,18 @@ export function commonjsRegister (path, loader) {
160
160
  DYNAMIC_REQUIRE_LOADERS[path] = loader;
161
161
  }
162
162
 
163
+ export function commonjsRegisterOrShort (path, to) {
164
+ const resolvedPath = commonjsResolveImpl(path, null, true);
165
+ if (resolvedPath !== null && DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
166
+ DYNAMIC_REQUIRE_CACHE[path] = DYNAMIC_REQUIRE_CACHE[resolvedPath];
167
+ } else {
168
+ DYNAMIC_REQUIRE_SHORTS[path] = to;
169
+ }
170
+ }
171
+
163
172
  const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
164
173
  const DYNAMIC_REQUIRE_CACHE = Object.create(null);
174
+ const DYNAMIC_REQUIRE_SHORTS = Object.create(null);
165
175
  const DEFAULT_PARENT_MODULE = {
166
176
  id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []
167
177
  };
@@ -266,10 +276,13 @@ export function commonjsResolveImpl (path, originalModuleDir, testCache) {
266
276
  const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
267
277
  if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
268
278
  return resolvedPath;
269
- };
279
+ }
280
+ if (DYNAMIC_REQUIRE_SHORTS[resolvedPath]) {
281
+ return resolvedPath;
282
+ }
270
283
  if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
271
284
  return resolvedPath;
272
- };
285
+ }
273
286
  }
274
287
  if (!shouldTryNodeModules) break;
275
288
  const nextDir = normalize(originalModuleDir + '/..');
@@ -288,10 +301,17 @@ export function commonjsResolve (path, originalModuleDir) {
288
301
  }
289
302
 
290
303
  export function commonjsRequire (path, originalModuleDir) {
291
- const resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
304
+ let resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
292
305
  if (resolvedPath !== null) {
293
306
  let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
294
307
  if (cachedModule) return cachedModule.exports;
308
+ let shortTo = DYNAMIC_REQUIRE_SHORTS[resolvedPath];
309
+ if (shortTo) {
310
+ cachedModule = DYNAMIC_REQUIRE_CACHE[shortTo];
311
+ if (cachedModule)
312
+ return cachedModule.exports;
313
+ resolvedPath = commonjsResolveImpl(shortTo, null, true);
314
+ }
295
315
  const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
296
316
  if (loader) {
297
317
  DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
@@ -388,15 +408,13 @@ function getPackageEntryPoint(dirPath) {
388
408
  }
389
409
 
390
410
  function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
391
- let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`;
411
+ let code = `const commonjsRegisterOrShort = require('${HELPERS_ID}?commonjsRegisterOrShort');`;
392
412
  for (const dir of dynamicRequireModuleDirPaths) {
393
413
  const entryPoint = getPackageEntryPoint(dir);
394
414
 
395
- code += `\ncommonjsRegister(${JSON.stringify(
415
+ code += `\ncommonjsRegisterOrShort(${JSON.stringify(
396
416
  getVirtualPathForDynamicRequirePath(dir, commonDir)
397
- )}, function (module, exports) {
398
- module.exports = require(${JSON.stringify(normalizePathSlashes(path.join(dir, entryPoint)))});
399
- });`;
417
+ )}, ${JSON.stringify(getVirtualPathForDynamicRequirePath(path.join(dir, entryPoint), commonDir))});`;
400
418
  }
401
419
  return code;
402
420
  }