From 1c16a2b25f764fa38142ad8c6b75ccce61f4a446 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Thu, 24 Feb 2022 13:39:29 +0100 Subject: [PATCH] fix(commonjs): handle external dependencies when using the cache (#1038) --- packages/commonjs/README.md | 2 +- .../commonjs/src/resolve-require-sources.js | 4 +- packages/commonjs/test/snapshots/test.js.md | 19 +++++++++ packages/commonjs/test/snapshots/test.js.snap | Bin 1298 -> 1442 bytes packages/commonjs/test/test.js | 40 ++++++++++++++++++ 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/packages/commonjs/README.md b/packages/commonjs/README.md index c126e4ac2..44a7652db 100644 --- a/packages/commonjs/README.md +++ b/packages/commonjs/README.md @@ -13,7 +13,7 @@ ## Requirements -This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v2.38.3+. +This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v12.0.0+) and Rollup v2.68.0+. If you are using [`@rollup/plugin-node-resolve`](https://github.com/rollup/plugins/tree/master/packages/node-resolve), it should be v13.0.6+. ## Install diff --git a/packages/commonjs/src/resolve-require-sources.js b/packages/commonjs/src/resolve-require-sources.js index 11647e010..f96ace50e 100644 --- a/packages/commonjs/src/resolve-require-sources.js +++ b/packages/commonjs/src/resolve-require-sources.js @@ -100,7 +100,7 @@ export function getRequireResolver(extensions, detectCyclesAndConditional) { resolvedSources, meta: { commonjs: parentMeta } }) { - // We explicitly track ES modules to handle ciruclar imports + // We explicitly track ES modules to handle circular imports if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false; if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false; const parentRequires = parentMeta && parentMeta.requires; @@ -135,7 +135,7 @@ export function getRequireResolver(extensions, detectCyclesAndConditional) { await Promise.all( Object.keys(resolvedSources) .map((source) => resolvedSources[source]) - .filter(({ id }) => !parentRequireSet.has(id)) + .filter(({ id, external }) => !(external || parentRequireSet.has(id))) .map(async (resolved) => { if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) { return ( diff --git a/packages/commonjs/test/snapshots/test.js.md b/packages/commonjs/test/snapshots/test.js.md index 684f9bc65..63fc433c8 100644 --- a/packages/commonjs/test/snapshots/test.js.md +++ b/packages/commonjs/test/snapshots/test.js.md @@ -316,3 +316,22 @@ Generated by [AVA](https://avajs.dev). ␊ console.log('main');␊ ` + +## handles external dependencies when using the cache + +> Snapshot 1 + + `'use strict';␊ + ␊ + var require$$0 = require('external');␊ + ␊ + function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }␊ + ␊ + var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);␊ + ␊ + var second = require$$0__default["default"].second;␊ + ␊ + var main = require$$0.first + second;␊ + ␊ + module.exports = main;␊ + ` diff --git a/packages/commonjs/test/snapshots/test.js.snap b/packages/commonjs/test/snapshots/test.js.snap index 46745cca1e118eadba1549a887865b282670e4a0..dbca1b612c8afce4e7b746e26997480a0f0409e6 100644 GIT binary patch literal 1442 zcmV;T1zq|gCk3Dey)4x|L%O{r(FD3tq3H0q=Nm@B|WbTU_p1yGA@#k*UFTB?u6KHue?)?1w zXXZ%pwvX}$rnVk)evJwAJg)bz`{R>L-|*Rgj{SH`e(xg};{rXN2o{DZR`0cv^!3O= z%{-nRJ3TyPZF|RjDJIZwu9u`Aep>$glK%Yn&nmyYIR5V9J#m4)dSkHSTY`1XP56}h z1WU1AX-n*W*>@?p9y80HoKGgpj_o=YRV}BY$U3dbsZMa3m^MdS)%2W*b?6x{P13Tb z>eMi8I>DS8W!}6(=V}h~+ziN?M&136?psvOzyiz=%cnW;m`|b3P0kPo+!74ILLI-e zX`rrU+TMU}x};=LNN1Bt!?(+x>Db^_eZ#Qm9>&PLLileA7Lovl!$7g92PP^KxN1?m z;#FY)2J?tM2)pqyFc2IE;GudjiKq7yuc{iB<1j_MGGgn_w331}C?@QJ!4&$*MG8W1 z!7OG13x&yI5#p^&}K5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$?^19*$EbKxK9Q){mTQ|- zG3}}>X|yRN`@se=EQWTw895j0^D|doq!mxmsBTzv-m#n1@m6GdqG>yxn+8c$se9Pg ze3MFPScN5G`ZNcg<5Os{kW0h?773JUq_8*&m-ARDj`vPsO7nH0I-qESi*5sHhY651D|j|etOC1%h{w1V=w zb5^rx!Rl4IQN+4Gk%o}K;y4mZeA62*g?9O9YAXk7ws+eG?!E`O?Pw@bx52Om-z6n< zj}tiE=O*9hhE<2Fcu`Vi(cR2Ve?B8){cRL_n#e|dz4wW<$iFsd{$jg23!` z+veMGZ?+v3Aq4mYVvKWdrAciaB6oL9A&tHjsDUO0hs~=w_A*#>848Yrr%XCpqYjo- zvwc&8G6lfpw&lNaX@p0donSxA1i2| zs$Qx(Yqm+$XCr-)+6`Kz-T}Ybpq59q$4P^_O;Vw<6%fMNMUR#e=8+E?I3yFNFvZB# zIPY(f*$_s46d~w)F=|I$ASuT)DP3~k}@06oqv~WGrHMeXtD( z{|!>jZsE$L_Nwaqv4w)FrZQo|x { t.snapshot(await getCodeFromBundle(bundle)); }); +test('handles external dependencies when using the cache', async (t) => { + const modules = {}; + const resetModules = () => { + modules['main.js'] = + "import first from 'first.js';import second from 'second.js';export default first + second;"; + modules['first.js'] = "export {first as default} from 'external';"; + modules['second.js'] = "module.exports = require('external').second;"; + }; + const options = { + input: 'main.js', + external: ['external'], + plugins: [commonjs(), loader(modules)], + onwarn + }; + + resetModules(); + let bundle = await rollup(options); + t.is( + ( + await executeBundle(bundle, t, { + context: { + require(id) { + if (id === 'external') { + return { first: 'first', second: 'second' }; + } + throw new Error(`Unexpected require "${id}"`); + } + } + }) + ).exports, + 'firstsecond' + ); + const code = await getCodeFromBundle(bundle); + t.snapshot(code); + + options.cache = bundle.cache; + bundle = await rollup(options); + t.is(await getCodeFromBundle(bundle), code); +}); + test('allows the config to be reused', async (t) => { const config = { preserveModules: true,