Skip to content

Commit ec4d28e

Browse files
authored
fix(node-resolve): prepare for Rollup 3 (#1288)
BREAKING CHANGES: Requires Node 14
1 parent cafc32b commit ec4d28e

25 files changed

+143
-205
lines changed

β€Žpackages/node-resolve/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
## Requirements
1515

16-
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
16+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v2.78.0+.
1717

1818
## Install
1919

β€Žpackages/node-resolve/package.json

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
"bugs": "https://github.com/rollup/plugins/issues",
1616
"main": "./dist/cjs/index.js",
1717
"module": "./dist/es/index.js",
18-
"type": "commonjs",
1918
"exports": {
20-
"require": "./dist/cjs/index.js",
21-
"import": "./dist/es/index.js"
19+
"types": "./types/index.d.ts",
20+
"import": "./dist/es/index.js",
21+
"default": "./dist/cjs/index.js"
2222
},
2323
"engines": {
24-
"node": ">= 10.0.0"
24+
"node": ">=14.0.0"
2525
},
2626
"scripts": {
2727
"build": "rollup -c",
@@ -40,6 +40,7 @@
4040
},
4141
"files": [
4242
"dist",
43+
"!dist/**/*.map",
4344
"types",
4445
"README.md",
4546
"LICENSE"
@@ -52,32 +53,35 @@
5253
"modules"
5354
],
5455
"peerDependencies": {
55-
"rollup": "^2.78.0"
56+
"rollup": "^2.78.0||^3.0.0"
57+
},
58+
"peerDependenciesMeta": {
59+
"rollup": {
60+
"optional": true
61+
}
5662
},
5763
"dependencies": {
58-
"@rollup/pluginutils": "^3.1.0",
59-
"@types/resolve": "1.17.1",
64+
"@rollup/pluginutils": "^4.2.1",
65+
"@types/resolve": "1.20.2",
6066
"deepmerge": "^4.2.2",
61-
"is-builtin-module": "^3.1.0",
67+
"is-builtin-module": "^3.2.0",
6268
"is-module": "^1.0.0",
63-
"resolve": "^1.19.0"
69+
"resolve": "^1.22.1"
6470
},
6571
"devDependencies": {
66-
"@babel/core": "^7.10.5",
72+
"@babel/core": "^7.19.1",
6773
"@babel/plugin-transform-typescript": "^7.10.5",
6874
"@rollup/plugin-babel": "^5.1.0",
6975
"@rollup/plugin-commonjs": "^22.0.2",
7076
"@rollup/plugin-json": "^4.1.0",
71-
"es5-ext": "^0.10.53",
72-
"rollup": "^2.78.1",
73-
"source-map": "^0.7.3",
77+
"es5-ext": "^0.10.62",
78+
"rollup": "^3.0.0-7",
79+
"source-map": "^0.7.4",
7480
"string-capitalize": "^1.0.1"
7581
},
76-
"types": "types/index.d.ts",
82+
"types": "./types/index.d.ts",
7783
"ava": {
78-
"babel": {
79-
"compileEnhancements": false
80-
},
84+
"workerThreads": false,
8185
"files": [
8286
"!**/fixtures/**",
8387
"!**/helpers/**",

β€Žpackages/node-resolve/rollup.config.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { readFileSync } from 'fs';
2+
3+
import json from '@rollup/plugin-json';
4+
5+
import { createConfig } from '../../shared/rollup.config.mjs';
6+
7+
export default {
8+
...createConfig({
9+
pkg: JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
10+
}),
11+
input: 'src/index.js',
12+
plugins: [json()]
13+
};

β€Žpackages/node-resolve/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import isBuiltinModule from 'is-builtin-module';
55
import deepMerge from 'deepmerge';
66
import isModule from 'is-module';
77

8-
import { version } from '../package.json';
8+
import { version, peerDependencies } from '../package.json';
99

1010
import { isDirCached, isFileCached, readCachedFile } from './cache';
1111
import handleDeprecatedOptions from './deprecated-options';
1212
import { fileExists, readFile, realpath } from './fs';
1313
import resolveImportSpecifiers from './resolveImportSpecifiers';
14+
import validateVersion from './rollup-version.js';
1415
import { getMainFields, getPackageName, normalizeInput } from './util';
1516

1617
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
@@ -249,6 +250,7 @@ export function nodeResolve(opts = {}) {
249250
version,
250251

251252
buildStart(buildOptions) {
253+
validateVersion(this.meta.rollupVersion, peerDependencies.rollup);
252254
rollupOptions = buildOptions;
253255

254256
for (const warning of warnings) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
2+
3+
export default function validateVersion(actualVersion, peerDependencyVersion) {
4+
let minMajor = Infinity;
5+
let minMinor = Infinity;
6+
let minPatch = Infinity;
7+
let foundVersion;
8+
// eslint-disable-next-line no-cond-assign
9+
while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
10+
const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
11+
if (foundMajor < minMajor) {
12+
minMajor = foundMajor;
13+
minMinor = foundMinor;
14+
minPatch = foundPatch;
15+
}
16+
}
17+
if (!actualVersion) {
18+
throw new Error(
19+
`Insufficient Rollup version: "@rollup/plugin-node-resolve" requires at least rollup@${minMajor}.${minMinor}.${minPatch}.`
20+
);
21+
}
22+
const [major, minor, patch] = actualVersion.split('.').map(Number);
23+
if (
24+
major < minMajor ||
25+
(major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
26+
) {
27+
throw new Error(
28+
`Insufficient rollup version: "@rollup/plugin-node-resolve" requires at least rollup@${minMajor}.${minMinor}.${minPatch} but found rollup@${actualVersion}.`
29+
);
30+
}
31+
}

β€Žpackages/node-resolve/test/jail.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ test('mark module outside the jail as external', async (t) => {
2222
]
2323
});
2424
const imports = await getImports(bundle);
25-
26-
t.snapshot(warnings);
27-
t.is(warnings.length, 1);
2825
t.deepEqual(imports, ['string/uppercase.js']);
26+
27+
t.is(warnings.length, 1, 'number of warnings');
28+
const [{ exporter, id }] = warnings;
29+
t.is(exporter, 'string/uppercase.js', 'exporter');
30+
t.is(id.endsWith('jail.js'), true, 'id');
2931
});
3032

3133
test('bundle module defined inside the jail', async (t) => {

β€Žpackages/node-resolve/test/node_modules/current-package

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žpackages/node-resolve/test/side-effects.js renamed to β€Žpackages/node-resolve/test/side-effects.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { join } from 'path';
2+
import { fileURLToPath } from 'url';
23

34
import test from 'ava';
45
import { rollup } from 'rollup';
5-
66
import commonjs from '@rollup/plugin-commonjs';
77

8-
import { nodeResolve } from '..';
9-
import { getCode, testBundle } from '../../../util/test';
8+
import { nodeResolve } from 'current-package';
9+
10+
import { getCode, testBundle } from '../../../util/test.js';
1011

11-
process.chdir(join(__dirname, 'fixtures'));
12+
const DIRNAME = fileURLToPath(new URL('.', import.meta.url));
13+
process.chdir(join(DIRNAME, 'fixtures'));
1214

1315
const failOnWarn = (t) => (warning) =>
1416
t.fail(`No warnings were expected, got:\n${warning.code}\n${warning.message}`);
Binary file not shown.
Binary file not shown.

β€Žpackages/node-resolve/test/snapshots/jail.js.md

Lines changed: 0 additions & 20 deletions
This file was deleted.
-433 Bytes
Binary file not shown.
Binary file not shown.

β€Žpackages/node-resolve/test/snapshots/prefer-builtins.js.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ Generated by [AVA](https://avajs.dev).
1010
1111
[
1212
{
13-
chunkName: 'prefer-builtin',
1413
code: 'EMPTY_BUNDLE',
15-
message: 'Generated an empty chunk: "prefer-builtin"',
14+
message: 'Generated an empty chunk: "prefer-builtin".',
15+
names: [
16+
'prefer-builtin',
17+
],
1618
toString: Function {},
1719
},
1820
]
Binary file not shown.
Binary file not shown.
Binary file not shown.

β€Žpackages/node-resolve/test/snapshots/side-effects.js.md renamed to β€Žpackages/node-resolve/test/snapshots/side-effects.mjs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Snapshot report for `test/side-effects.js`
1+
# Snapshot report for `test/side-effects.mjs`
22

3-
The actual snapshot is saved in `side-effects.js.snap`.
3+
The actual snapshot is saved in `side-effects.mjs.snap`.
44

55
Generated by [AVA](https://avajs.dev).
66

Binary file not shown.
Binary file not shown.

β€Žpackages/node-resolve/test/snapshots/test.js.md renamed to β€Žpackages/node-resolve/test/snapshots/test.mjs.md

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Snapshot report for `test/test.js`
1+
# Snapshot report for `test/test.mjs`
22

3-
The actual snapshot is saved in `test.js.snap`.
3+
The actual snapshot is saved in `test.mjs.snap`.
44

55
Generated by [AVA](https://avajs.dev).
66

77
## throws error if local id is not resolved
88

99
> Snapshot 1
1010
11-
'Could not resolve \'./foo\' from unresolved-local.js'
11+
'Could not resolve "./foo" from "unresolved-local.js"'
1212

1313
## allows custom moduleDirectories with legacy customResolveOptions.moduleDirectory
1414

@@ -23,56 +23,6 @@ Generated by [AVA](https://avajs.dev).
2323
},
2424
]
2525

26-
## ignores deep-import non-modules
27-
28-
> Snapshot 1
29-
30-
[
31-
{
32-
code: 'UNRESOLVED_IMPORT',
33-
importer: 'deep-import-non-module.js',
34-
message: '\'foo/deep\' is imported by deep-import-non-module.js, but could not be resolved – treating it as an external dependency',
35-
source: 'foo/deep',
36-
toString: Function {},
37-
url: 'https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency',
38-
},
39-
]
40-
41-
## respects the package.json sideEffects property for files in root package by default
42-
43-
> Snapshot 1
44-
45-
`'use strict';␊
46-
␊
47-
console.log('main');␊
48-
`
49-
50-
## ignores the package.json sideEffects property for files in root package with "ignoreSideEffectsForRoot" option
51-
52-
> Snapshot 1
53-
54-
`'use strict';␊
55-
␊
56-
console.log('side effect');␊
57-
␊
58-
console.log('main');␊
59-
`
60-
61-
## handles package side-effects
62-
63-
> Snapshot 1
64-
65-
[
66-
'array-dep1',
67-
'array-dep3',
68-
'array-dep5',
69-
'array-index',
70-
'false-dep1',
71-
'true-dep1',
72-
'true-dep2',
73-
'true-index',
74-
]
75-
7626
## throws error for removed customResolveOptions.preserveSymlinks option
7727

7828
> Snapshot 1
@@ -176,27 +126,3 @@ Generated by [AVA](https://avajs.dev).
176126
Error {
177127
message: 'node-resolve: `customResolveOptions.packageIterator` is no longer an option. If you need this, please open an issue.',
178128
}
179-
180-
## respects the package.json sideEffects property for files in the root package and supports deep side effects
181-
182-
> Snapshot 1
183-
184-
`'use strict';␊
185-
␊
186-
console.log('deep side effect');␊
187-
␊
188-
console.log('shallow side effect');␊
189-
␊
190-
console.log('main');␊
191-
`
192-
193-
## does not prefix the sideEffects property if the side effect contains a "/"
194-
195-
> Snapshot 1
196-
197-
`'use strict';␊
198-
␊
199-
console.log('shallow side effect');␊
200-
␊
201-
console.log('main');␊
202-
`
Binary file not shown.

0 commit comments

Comments
Β (0)