Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(alias): prepare for Rollup 3, handle latest node-resolve #1269

Merged
merged 1 commit into from Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/alias/README.md
Expand Up @@ -31,7 +31,7 @@ This plugin will work for any file type that Rollup natively supports, or those

## Requirements

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

## Install

Expand Down
33 changes: 22 additions & 11 deletions packages/alias/package.json
Expand Up @@ -13,10 +13,15 @@
"author": "Johannes Stein",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/alias#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index.js",
"module": "dist/index.es.js",
"main": "./dist/cjs/index.js",
"module": "./dist/es/index.js",
"exports": {
"import": "./dist/es/index.js",
"types": "./types/index.d.ts",
"default": "./dist/cjs/index.js"
},
"engines": {
"node": ">=8.0.0"
"node": ">=14.0.0"
},
"scripts": {
"build": "rollup -c",
Expand All @@ -34,6 +39,7 @@
},
"files": [
"dist",
"!dist/**/*.map",
"types",
"README.md",
"LICENSE"
Expand All @@ -45,19 +51,24 @@
"alias"
],
"peerDependencies": {
"rollup": "^1.20.0||^2.0.0"
"rollup": "^1.20.0||^2.0.0||^3.0.0"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
},
"dependencies": {
"slash": "^3.0.0"
"slash": "^4.0.0"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^8.4.0",
"@rollup/plugin-typescript": "^5.0.2",
"del-cli": "^3.0.1",
"rollup": "2.67.3",
"typescript": "^4.1.2"
"@rollup/plugin-node-resolve": "^14.1.0",
"@rollup/plugin-typescript": "^8.5.0",
"del-cli": "^5.0.0",
"rollup": "^3.0.0-7",
"typescript": "^4.8.3"
},
"types": "types/index.d.ts",
"types": "./types/index.d.ts",
"ava": {
"files": [
"!**/fixtures/**",
Expand Down
13 changes: 0 additions & 13 deletions packages/alias/rollup.config.js

This file was deleted.

7 changes: 7 additions & 0 deletions packages/alias/rollup.config.mjs
@@ -0,0 +1,7 @@
import { readFileSync } from 'fs';

import { createConfig } from '../../shared/rollup.config.mjs';

export default createConfig({
pkg: JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
});
27 changes: 16 additions & 11 deletions packages/alias/src/index.ts
@@ -1,6 +1,6 @@
import { Plugin } from 'rollup';

import { ResolvedAlias, ResolverFunction, ResolverObject, RollupAliasOptions } from '../types';
import type { ResolvedAlias, ResolverFunction, ResolverObject, RollupAliasOptions } from '../types';

function matches(pattern: string | RegExp, importee: string) {
if (pattern instanceof RegExp) {
Expand Down Expand Up @@ -38,16 +38,24 @@ function getEntries({ entries, customResolver }: RollupAliasOptions): readonly R
});
}

function getHookFunction<T extends Function>(hook: T | { handler?: T }): T | null {
if (typeof hook === 'function') {
return hook;
}
if (hook && 'handler' in hook && typeof hook.handler === 'function') {
return hook.handler;
}
return null;
}

function resolveCustomResolver(
customResolver: ResolverFunction | ResolverObject | null | undefined
): ResolverFunction | null {
if (typeof customResolver === 'function') {
return customResolver;
}
if (customResolver) {
if (typeof customResolver === 'function') {
return customResolver;
}
if (typeof customResolver.resolveId === 'function') {
return customResolver.resolveId;
}
return getHookFunction(customResolver.resolveId);
}
return null;
}
Expand All @@ -68,10 +76,7 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
await Promise.all(
[...(Array.isArray(options.entries) ? options.entries : []), options].map(
({ customResolver }) =>
customResolver &&
typeof customResolver === 'object' &&
typeof customResolver.buildStart === 'function' &&
customResolver.buildStart.call(this, inputOptions)
customResolver && getHookFunction(customResolver.buildStart)?.call(this, inputOptions)
)
);
},
Expand Down
4 changes: 3 additions & 1 deletion packages/alias/types/index.d.ts
@@ -1,6 +1,8 @@
import { Plugin, PluginHooks } from 'rollup';

export type ResolverFunction = PluginHooks['resolveId'];
type MapToFunction<T> = T extends Function ? T : never;

export type ResolverFunction = MapToFunction<PluginHooks['resolveId']>;

export interface ResolverObject {
buildStart?: PluginHooks['buildStart'];
Expand Down