Skip to content

Commit

Permalink
fix(alias): prepare for Rollup 3, handle latest node-resolve
Browse files Browse the repository at this point in the history
BREAKING CHANGES: Requires Node 14
  • Loading branch information
lukastaegert committed Sep 24, 2022
1 parent 6090005 commit b11421c
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 70 deletions.
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
18 changes: 9 additions & 9 deletions packages/alias/package.json
Expand Up @@ -16,7 +16,7 @@
"main": "dist/index.js",
"module": "dist/index.es.js",
"engines": {
"node": ">=8.0.0"
"node": ">=14.0.0"
},
"scripts": {
"build": "rollup -c",
Expand All @@ -28,7 +28,7 @@
"prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
"prerelease": "pnpm build",
"pretest": "pnpm build",
"release": "pnpm plugin:release --workspace-root -- --pkg $npm_package_name",
"release": "pnpm --workspace-root plugin:release --pkg $npm_package_name",
"test": "ava",
"test:ts": "tsc --noEmit"
},
Expand All @@ -45,17 +45,17 @@
"alias"
],
"peerDependencies": {
"rollup": "^1.20.0||^2.0.0"
"rollup": "^1.20.0||^2.0.0||^3.0.0"
},
"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",
"ava": {
Expand Down
@@ -1,6 +1,7 @@
import typescript from '@rollup/plugin-typescript';
import { readFileSync } from 'fs';

import pkg from './package.json';
const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));

export default {
input: 'src/index.ts',
Expand All @@ -9,5 +10,6 @@ export default {
{ file: pkg.main, format: 'cjs', exports: 'auto' },
{ file: pkg.module, format: 'es' }
],
plugins: [typescript({ sourceMap: false })]
plugins: [typescript({ sourceMap: false })],
strictDeprecations: true
};
25 changes: 15 additions & 10 deletions packages/alias/src/index.ts
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

0 comments on commit b11421c

Please sign in to comment.