Skip to content

Commit ae4e9b3

Browse files
authored
fix(alias): prepare for Rollup 3, handle latest node-resolve (rollup#1269)
BREAKING CHANGES: Requires Node 14
1 parent 7c8b910 commit ae4e9b3

File tree

7 files changed

+1721
-2067
lines changed

7 files changed

+1721
-2067
lines changed

packages/alias/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This plugin will work for any file type that Rollup natively supports, or those
3131

3232
## Requirements
3333

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

3636
## Install
3737

packages/alias/package.json

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
"author": "Johannes Stein",
1414
"homepage": "https://github.com/rollup/plugins/tree/master/packages/alias#readme",
1515
"bugs": "https://github.com/rollup/plugins/issues",
16-
"main": "dist/index.js",
17-
"module": "dist/index.es.js",
16+
"main": "./dist/cjs/index.js",
17+
"module": "./dist/es/index.js",
18+
"exports": {
19+
"import": "./dist/es/index.js",
20+
"types": "./types/index.d.ts",
21+
"default": "./dist/cjs/index.js"
22+
},
1823
"engines": {
19-
"node": ">=8.0.0"
24+
"node": ">=14.0.0"
2025
},
2126
"scripts": {
2227
"build": "rollup -c",
@@ -34,6 +39,7 @@
3439
},
3540
"files": [
3641
"dist",
42+
"!dist/**/*.map",
3743
"types",
3844
"README.md",
3945
"LICENSE"
@@ -45,19 +51,24 @@
4551
"alias"
4652
],
4753
"peerDependencies": {
48-
"rollup": "^1.20.0||^2.0.0"
54+
"rollup": "^1.20.0||^2.0.0||^3.0.0"
55+
},
56+
"peerDependenciesMeta": {
57+
"rollup": {
58+
"optional": true
59+
}
4960
},
5061
"dependencies": {
51-
"slash": "^3.0.0"
62+
"slash": "^4.0.0"
5263
},
5364
"devDependencies": {
54-
"@rollup/plugin-node-resolve": "^8.4.0",
55-
"@rollup/plugin-typescript": "^5.0.2",
56-
"del-cli": "^3.0.1",
57-
"rollup": "2.67.3",
58-
"typescript": "^4.1.2"
65+
"@rollup/plugin-node-resolve": "^14.1.0",
66+
"@rollup/plugin-typescript": "^8.5.0",
67+
"del-cli": "^5.0.0",
68+
"rollup": "^3.0.0-7",
69+
"typescript": "^4.8.3"
5970
},
60-
"types": "types/index.d.ts",
71+
"types": "./types/index.d.ts",
6172
"ava": {
6273
"files": [
6374
"!**/fixtures/**",

packages/alias/rollup.config.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/alias/rollup.config.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { readFileSync } from 'fs';
2+
3+
import { createConfig } from '../../shared/rollup.config.mjs';
4+
5+
export default createConfig({
6+
pkg: JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
7+
});

packages/alias/src/index.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Plugin } from 'rollup';
22

3-
import { ResolvedAlias, ResolverFunction, ResolverObject, RollupAliasOptions } from '../types';
3+
import type { ResolvedAlias, ResolverFunction, ResolverObject, RollupAliasOptions } from '../types';
44

55
function matches(pattern: string | RegExp, importee: string) {
66
if (pattern instanceof RegExp) {
@@ -38,16 +38,24 @@ function getEntries({ entries, customResolver }: RollupAliasOptions): readonly R
3838
});
3939
}
4040

41+
function getHookFunction<T extends Function>(hook: T | { handler?: T }): T | null {
42+
if (typeof hook === 'function') {
43+
return hook;
44+
}
45+
if (hook && 'handler' in hook && typeof hook.handler === 'function') {
46+
return hook.handler;
47+
}
48+
return null;
49+
}
50+
4151
function resolveCustomResolver(
4252
customResolver: ResolverFunction | ResolverObject | null | undefined
4353
): ResolverFunction | null {
54+
if (typeof customResolver === 'function') {
55+
return customResolver;
56+
}
4457
if (customResolver) {
45-
if (typeof customResolver === 'function') {
46-
return customResolver;
47-
}
48-
if (typeof customResolver.resolveId === 'function') {
49-
return customResolver.resolveId;
50-
}
58+
return getHookFunction(customResolver.resolveId);
5159
}
5260
return null;
5361
}
@@ -68,10 +76,7 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
6876
await Promise.all(
6977
[...(Array.isArray(options.entries) ? options.entries : []), options].map(
7078
({ customResolver }) =>
71-
customResolver &&
72-
typeof customResolver === 'object' &&
73-
typeof customResolver.buildStart === 'function' &&
74-
customResolver.buildStart.call(this, inputOptions)
79+
customResolver && getHookFunction(customResolver.buildStart)?.call(this, inputOptions)
7580
)
7681
);
7782
},

packages/alias/types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Plugin, PluginHooks } from 'rollup';
22

3-
export type ResolverFunction = PluginHooks['resolveId'];
3+
type MapToFunction<T> = T extends Function ? T : never;
4+
5+
export type ResolverFunction = MapToFunction<PluginHooks['resolveId']>;
46

57
export interface ResolverObject {
68
buildStart?: PluginHooks['buildStart'];

0 commit comments

Comments
 (0)