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 30, 2022
1 parent 32aa6d2 commit 520fbcd
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 82 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
31 changes: 20 additions & 11 deletions packages/alias/package.json
Expand Up @@ -13,10 +13,14 @@
"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": {
"require": "./dist/cjs/index.js",
"import": "./dist/es/index.js"
},
"engines": {
"node": ">=8.0.0"
"node": ">=14.0.0"
},
"scripts": {
"build": "rollup -c",
Expand All @@ -33,7 +37,7 @@
"test:ts": "tsc --noEmit"
},
"files": [
"dist",
"dist/**/*.{js,json}",
"types",
"README.md",
"LICENSE"
Expand All @@ -45,17 +49,22 @@
"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",
"ava": {
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
86 changes: 44 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions shared/rollup.config.mjs
Expand Up @@ -23,14 +23,14 @@ export function createConfig({ pkg, external = [] }) {
output: [
{
format: 'cjs',
file: pkg.main,
file: pkg.exports.require,
exports: 'named',
footer: 'module.exports = Object.assign(exports.default, exports);',
sourcemap: true
},
{
format: 'es',
file: pkg.module,
file: pkg.exports.import,
plugins: [emitModulePackageFile()],
sourcemap: true
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Expand Up @@ -12,7 +12,7 @@
"pretty": true,
"sourceMap": true,
"strict": true,
"target": "es2017"
"target": "es2019"
},
"exclude": ["dist", "node_modules", "test/types"]
}

0 comments on commit 520fbcd

Please sign in to comment.