From 18afe15f44f07964150d2580e92709bb5e7aa8a6 Mon Sep 17 00:00:00 2001 From: Justin McMurdie <3059715+TheMcMurder@users.noreply.github.com> Date: Fri, 15 Apr 2022 06:52:11 -0600 Subject: [PATCH] =?UTF-8?q?fix(node-resolve):=20update=20side=20effects=20?= =?UTF-8?q?logic=20to=20be=20deep=20when=20glob=20doesn=E2=80=99t=20contai?= =?UTF-8?q?n=20`/`=20(#1148)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(node-resolve): deep side effects option * chore(node-resolve): Fixing linting errors * feat: deep side effects - prevent changes when side effects start with "./" || "/" * feat: support deep side effects by default - matching webpack implementation * chore(cleanup): pr feedback --- packages/node-resolve/src/util.js | 12 ++++++- .../deep/side-effect.js | 1 + .../index.js | 4 +++ .../package.json | 4 +++ .../shallow-side-effect.js | 1 + .../deep-side-effects/deep/side-effect.js | 1 + .../test/fixtures/deep-side-effects/index.js | 4 +++ .../fixtures/deep-side-effects/package.json | 4 +++ .../deep-side-effects/shallow-side-effect.js | 1 + .../node-resolve/test/snapshots/test.js.md | 24 +++++++++++++ .../node-resolve/test/snapshots/test.js.snap | Bin 1459 -> 1618 bytes packages/node-resolve/test/test.js | 32 ++++++++++++++++++ 12 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/deep/side-effect.js create mode 100644 packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/index.js create mode 100644 packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/package.json create mode 100644 packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/shallow-side-effect.js create mode 100644 packages/node-resolve/test/fixtures/deep-side-effects/deep/side-effect.js create mode 100644 packages/node-resolve/test/fixtures/deep-side-effects/index.js create mode 100644 packages/node-resolve/test/fixtures/deep-side-effects/package.json create mode 100644 packages/node-resolve/test/fixtures/deep-side-effects/shallow-side-effect.js diff --git a/packages/node-resolve/src/util.js b/packages/node-resolve/src/util.js index 7b21d250a..c61deb27d 100644 --- a/packages/node-resolve/src/util.js +++ b/packages/node-resolve/src/util.js @@ -144,7 +144,17 @@ export function getPackageInfo(options) { if (typeof packageSideEffects === 'boolean') { internalPackageInfo.hasModuleSideEffects = () => packageSideEffects; } else if (Array.isArray(packageSideEffects)) { - internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, { + const finalPackageSideEffects = packageSideEffects.map((sideEffect) => { + /* + * The array accepts simple glob patterns to the relevant files... Patterns like .css, which do not include a /, will be treated like **\/.css. + * https://webpack.js.org/guides/tree-shaking/ + */ + if (sideEffect.includes('/')) { + return sideEffect; + } + return `**/${sideEffect}`; + }); + internalPackageInfo.hasModuleSideEffects = createFilter(finalPackageSideEffects, null, { resolve: pkgRoot }); } diff --git a/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/deep/side-effect.js b/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/deep/side-effect.js new file mode 100644 index 000000000..a9932c054 --- /dev/null +++ b/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/deep/side-effect.js @@ -0,0 +1 @@ +console.log('deep side effect') \ No newline at end of file diff --git a/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/index.js b/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/index.js new file mode 100644 index 000000000..2b08cba9b --- /dev/null +++ b/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/index.js @@ -0,0 +1,4 @@ +import './deep/side-effect.js' +import './shallow-side-effect.js' + +console.log('main') \ No newline at end of file diff --git a/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/package.json b/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/package.json new file mode 100644 index 000000000..1254bc8ae --- /dev/null +++ b/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/package.json @@ -0,0 +1,4 @@ +{ + "main": "./index.js", + "sideEffects": ["./*.js"] +} diff --git a/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/shallow-side-effect.js b/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/shallow-side-effect.js new file mode 100644 index 000000000..dd89c4709 --- /dev/null +++ b/packages/node-resolve/test/fixtures/deep-side-effects-with-specific-side-effects/shallow-side-effect.js @@ -0,0 +1 @@ +console.log('shallow side effect') \ No newline at end of file diff --git a/packages/node-resolve/test/fixtures/deep-side-effects/deep/side-effect.js b/packages/node-resolve/test/fixtures/deep-side-effects/deep/side-effect.js new file mode 100644 index 000000000..a9932c054 --- /dev/null +++ b/packages/node-resolve/test/fixtures/deep-side-effects/deep/side-effect.js @@ -0,0 +1 @@ +console.log('deep side effect') \ No newline at end of file diff --git a/packages/node-resolve/test/fixtures/deep-side-effects/index.js b/packages/node-resolve/test/fixtures/deep-side-effects/index.js new file mode 100644 index 000000000..2b08cba9b --- /dev/null +++ b/packages/node-resolve/test/fixtures/deep-side-effects/index.js @@ -0,0 +1,4 @@ +import './deep/side-effect.js' +import './shallow-side-effect.js' + +console.log('main') \ No newline at end of file diff --git a/packages/node-resolve/test/fixtures/deep-side-effects/package.json b/packages/node-resolve/test/fixtures/deep-side-effects/package.json new file mode 100644 index 000000000..cef46cb21 --- /dev/null +++ b/packages/node-resolve/test/fixtures/deep-side-effects/package.json @@ -0,0 +1,4 @@ +{ + "main": "./index.js", + "sideEffects": ["*.js"] +} diff --git a/packages/node-resolve/test/fixtures/deep-side-effects/shallow-side-effect.js b/packages/node-resolve/test/fixtures/deep-side-effects/shallow-side-effect.js new file mode 100644 index 000000000..dd89c4709 --- /dev/null +++ b/packages/node-resolve/test/fixtures/deep-side-effects/shallow-side-effect.js @@ -0,0 +1 @@ +console.log('shallow side effect') \ No newline at end of file diff --git a/packages/node-resolve/test/snapshots/test.js.md b/packages/node-resolve/test/snapshots/test.js.md index 844b66a7c..de7267431 100644 --- a/packages/node-resolve/test/snapshots/test.js.md +++ b/packages/node-resolve/test/snapshots/test.js.md @@ -176,3 +176,27 @@ Generated by [AVA](https://avajs.dev). Error { message: 'node-resolve: `customResolveOptions.packageIterator` is no longer an option. If you need this, please open an issue.', } + +## respects the package.json sideEffects property for files in the root package and supports deep side effects + +> Snapshot 1 + + `'use strict';␊ + ␊ + console.log('deep side effect');␊ + ␊ + console.log('shallow side effect');␊ + ␊ + console.log('main');␊ + ` + +## does not prefix the sideEffects property if the side effect contains a "/" + +> Snapshot 1 + + `'use strict';␊ + ␊ + console.log('shallow side effect');␊ + ␊ + console.log('main');␊ + ` diff --git a/packages/node-resolve/test/snapshots/test.js.snap b/packages/node-resolve/test/snapshots/test.js.snap index 956e33b1a44fa8907def64051a9db40f8b7a0588..83f4e677a760ab0f96a3c69163da2314b484aff0 100644 GIT binary patch literal 1618 zcmV-Y2Cex)RzVj*X zZ2?iKk5o+%B{l>UqC8SX0wnTK5hYj+LBJ|R@ec(QOfcxT)7z#GDFjUUfHy3%`a%8u!Ckie{ z@3mpZW=A1py=!S(#dh`3cl+;tqq*YKI?UK-V-U(Lh;8af$=tbOR_&2ZEo+Wp#-0Jp zia&h(fhp#e=CU4vQwnHiU2z(6sEzJx@MzsJZm>C%nZ^ zO-#d#eKQ`RwWAKaoF}wLC1`Jt*wyA-h8cT&I6~`w`lU%ISDrt8VDF=ojy5)9#!gB= zXnk4MvHZ+7H2#NHdm!b#d6=;cfDMcGE^+2Bdg$t(%Y}}(_9Dz!Mf#L%7cgVj03uKfQyNagrs?pU1sI7<$v7%kmL+*4jy9SY8?B_IB!q_} z3<^oqJE!0XgrkyijGrnB=cUHGDoP$|m8l{LwKR)Vx-~_W{6(Gbg#ne9M8y{1+%+)9 zp;DPsCApID3K1nDNTQd@gcFIxI@*Y%iquOQ5h?W$)yFI8Bp^^uq0oVfdPiPSG-?}6 zhy39>3>H5T!l1rUMxy6O^7v|b@goNTD9V?oN3JO1b9~ohdcvX`)-#H;(A`0%`3bj$pH@37LMBzS6o*8zD@T}y9WVUhh+Si*w&rE?w5in8n?r^7k7|9F{Jqn-gP@zI+E zty4(d0@oQR;)6PHQ3wgh3Tzydn2nS|oGN>c;NgHvCqW7nAPL`MPcS1Kulv`f=~z}4`c8d+$b@lUTc%+% z^|ehiY!QYnuG7ZD)?a5BBfADxae_j7d@#ze4O3;UrzyIRDZ?}&rE)p(b} zy?YL{O8+fi^h<}R=*^+*OP^!#CW!$%m1}8nyp7u6-$SBDsyUCYx&hIZK*UD;@qf4ty4R=3~s!eT4{ZUUT`LXP-RQ)B5ex zvEXylCu5GTEkbDP?MGg%m-_4_`o7yfJQ!GmIr>g9Lfe*ox+1V}*`rtfSu6Dy^)+LT zY7-IK8T-B|9^D+Yc$$u;Ti)M;Ir>-$LT~T>amkAQYfERI*`yw8w17now8DkZ-YVPF z*%jN%U#sofTl?FE7cfU#f!_ToP(1hH^OvqZIPvfK=+`>T(P^a!on2G0aoI(5rhi&c zTfWVD0dsUEkQF(&fAM5&nFZqh&XBsrB2pUsDL{y2) zh{WU=Rfr%H#y9#%LkCGSoye3%i53^tN|KbQpiDonwC^+{Cl zA&9bpj%AJ|^Q+|ZMFm7;O8S;(nw`O@1HB1U581Wq#u;WCDZ@$@FKS)T5NKao+Z<>J zEEw5dmJMjI!CK<Bw7I!x9)s|c@(I5%L{U^6-@To36U{0- zyii$2Gm|Df4r2gF1ljW^RA(K)x2DnnEgHR`25h^c}R!C5=58H&e1Eeh#*0C N`7eWZv|>^c0017O$PEAh diff --git a/packages/node-resolve/test/test.js b/packages/node-resolve/test/test.js index 0979eb76a..50cbcac82 100755 --- a/packages/node-resolve/test/test.js +++ b/packages/node-resolve/test/test.js @@ -322,6 +322,38 @@ test('respects the package.json sideEffects property for files in root package b t.snapshot(code); }); +test('respects the package.json sideEffects property for files in the root package and supports deep side effects', async (t) => { + const bundle = await rollup({ + input: 'deep-side-effects/index.js', + onwarn: failOnWarn(t), + plugins: [ + nodeResolve({ + rootDir: 'deep-side-effects' + }) + ] + }); + const code = await getCode(bundle); + t.true(code.includes('shallow side effect')); + t.true(code.includes('deep side effect')); + t.snapshot(code); +}); + +test('does not prefix the sideEffects property if the side effect contains a "/"', async (t) => { + const bundle = await rollup({ + input: 'deep-side-effects-with-specific-side-effects/index.js', + onwarn: failOnWarn(t), + plugins: [ + nodeResolve({ + rootDir: 'deep-side-effects-with-specific-side-effects' + }) + ] + }); + const code = await getCode(bundle); + t.true(code.includes('shallow side effect')); + t.false(code.includes('deep side effects')); + t.snapshot(code); +}); + test('ignores the package.json sideEffects property for files in root package with "ignoreSideEffectsForRoot" option', async (t) => { const bundle = await rollup({ input: 'root-package-side-effect/index.js',