-
-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node-resolve): preserve moduleSideEffects when re-resolving files (β¦
β¦#1245) BREAKING CHANGES: Requires Rollup>=2.78.0
- Loading branch information
1 parent
3d3e459
commit 886deba
Showing
7 changed files
with
406 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { join } from 'path'; | ||
|
||
import test from 'ava'; | ||
import { rollup } from 'rollup'; | ||
|
||
import commonjs from '@rollup/plugin-commonjs'; | ||
|
||
import { nodeResolve } from '..'; | ||
import { getCode, testBundle } from '../../../util/test'; | ||
|
||
process.chdir(join(__dirname, 'fixtures')); | ||
|
||
const failOnWarn = (t) => (warning) => | ||
t.fail(`No warnings were expected, got:\n${warning.code}\n${warning.message}`); | ||
|
||
test('respects the package.json sideEffects property for files in root package by default', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'root-package-side-effect/index.js', | ||
onwarn: failOnWarn(t), | ||
plugins: [ | ||
nodeResolve({ | ||
rootDir: 'root-package-side-effect' | ||
}) | ||
] | ||
}); | ||
|
||
const code = await getCode(bundle); | ||
t.false(code.includes('side effect')); | ||
t.snapshot(code); | ||
}); | ||
|
||
test('respects the package.json sideEffects when commonjs plugin is used', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'root-package-side-effect/index.js', | ||
onwarn: failOnWarn(t), | ||
plugins: [ | ||
commonjs(), | ||
nodeResolve({ | ||
rootDir: 'root-package-side-effect' | ||
}) | ||
] | ||
}); | ||
|
||
const code = await getCode(bundle); | ||
t.false(code.includes('side effect')); | ||
t.snapshot(code); | ||
}); | ||
|
||
test('respects the package.json sideEffects when when another plugin uses this.load it its resolveId hook', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'root-package-side-effect/index.js', | ||
onwarn: failOnWarn(t), | ||
plugins: [ | ||
{ | ||
name: 'test', | ||
async resolveId(source, importer, resolveOptions) { | ||
const resolved = await this.resolve(source, importer, { | ||
...resolveOptions, | ||
skipSelf: true | ||
}); | ||
// This starts loading the module and fixes the value of | ||
// `moduleSideEffects` with whatever is contained in "resolved" | ||
await this.load(resolved); | ||
return resolved; | ||
} | ||
}, | ||
nodeResolve({ | ||
rootDir: 'root-package-side-effect' | ||
}) | ||
] | ||
}); | ||
|
||
const code = await getCode(bundle); | ||
t.false(code.includes('side effect')); | ||
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', | ||
onwarn: failOnWarn(t), | ||
plugins: [ | ||
nodeResolve({ | ||
rootDir: 'root-package-side-effect', | ||
ignoreSideEffectsForRoot: true | ||
}) | ||
] | ||
}); | ||
|
||
const code = await getCode(bundle); | ||
t.true(code.includes('side effect')); | ||
t.snapshot(code); | ||
}); | ||
|
||
test('handles package side-effects', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'side-effects.js', | ||
onwarn: failOnWarn(t), | ||
plugins: [nodeResolve()] | ||
}); | ||
await testBundle(t, bundle); | ||
t.snapshot(global.sideEffects); | ||
|
||
delete global.sideEffects; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Snapshot report for `test/side-effects.js` | ||
|
||
The actual snapshot is saved in `side-effects.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## respects the package.json sideEffects property for files in root package by default | ||
|
||
> Snapshot 1 | ||
`'use strict';β | ||
β | ||
console.log('main');β | ||
` | ||
|
||
## respects the package.json sideEffects when commonjs plugin is used | ||
|
||
> Snapshot 1 | ||
`'use strict';β | ||
β | ||
console.log('main');β | ||
` | ||
|
||
## respects the package.json sideEffects when when another plugin uses this.load it its resolveId hook | ||
|
||
> Snapshot 1 | ||
`'use strict';β | ||
β | ||
console.log('main');β | ||
` | ||
|
||
## 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');β | ||
` | ||
|
||
## ignores the package.json sideEffects property for files in root package with "ignoreSideEffectsForRoot" option | ||
|
||
> Snapshot 1 | ||
`'use strict';β | ||
β | ||
console.log('side effect');β | ||
β | ||
console.log('main');β | ||
` | ||
|
||
## handles package side-effects | ||
|
||
> Snapshot 1 | ||
[ | ||
'array-dep1', | ||
'array-dep3', | ||
'array-dep5', | ||
'array-index', | ||
'false-dep1', | ||
'true-dep1', | ||
'true-dep2', | ||
'true-index', | ||
] |
Binary file not shown.
Oops, something went wrong.