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 29, 2022
1 parent 4e85ed7 commit 2c88637
Show file tree
Hide file tree
Showing 19 changed files with 2,847 additions and 3,319 deletions.
14 changes: 4 additions & 10 deletions .eslintrc.js
Expand Up @@ -7,21 +7,15 @@ module.exports = {
rules: {
// disabling sort keys for now so we can get the rest of the linting shored up
'sort-keys': 'off',
'typescript-sort-keys/interface': 'off'
'typescript-sort-keys/interface': 'off',
'import/extensions': 'off',
'import/no-unresolved': 'off'
},
overrides: [
{
files: ['**/fixtures/**'],
rules: {
'no-console': 'off',
'import/extensions': 'off',
'import/no-unresolved': 'off'
}
},
{
files: ['**/test/**'],
rules: {
'import/extensions': 'off'
'no-console': 'off'
}
}
]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,6 +11,7 @@ coverage.lcov
.idea
.rollup.cache

!packages/*/test/**/node_modules
!packages/node-resolve/test/fixtures/**/node_modules
!packages/commonjs/test/**/node_modules
!packages/typescript/test/fixtures/**/node_modules
Expand Down
23 changes: 13 additions & 10 deletions package.json
Expand Up @@ -3,10 +3,10 @@
"private": true,
"scripts": {
"ci:coverage:submit": "node scripts/codecov",
"fix:js": "eslint --cache packages scripts shared util --ext .js,.ts --fix",
"fix:js": "eslint --cache packages scripts shared util --ext .js,.ts,.mjs --fix",
"lint": "pnpm lint:docs && pnpm --no-bail lint:json && pnpm lint:package && pnpm lint:js",
"lint:docs": "prettier --write .github/**/*.md **/README.md **/CHANGELOG.md",
"lint:js": "eslint --cache packages scripts shared util --ext .js,.ts",
"lint:js": "eslint --cache packages scripts shared util --ext .js,.ts,.mjs",
"lint:json": "prettier --write .github/**/*.yml **/tsconfig.json tsconfig.*.json pnpm-workspace.yaml",
"lint:package": "prettier --write **/package.json",
"plugin:release": "ts-node ./scripts/release.ts",
Expand All @@ -18,32 +18,35 @@
},
"devDependencies": {
"@ava/babel": "2.0.0",
"@rollup/plugin-typescript": "^8.5.0",
"@types/conventional-commits-parser": "^3.0.2",
"@types/node": "14.18.30",
"@types/semver": "^7.3.7",
"@types/source-map-support": "^0.5.4",
"@types/yargs-parser": "^20.2.1",
"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
"ava": "^3.15.0",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"ava": "^4.3.3",
"chalk": "^4.1.0",
"codecov-lite": "2.0.0",
"conventional-commits-parser": "^3.2.1",
"del-cli": "4.0.0",
"eslint-config-rollup": "^2.0.4",
"del-cli": "^5.0.0",
"eslint": "^8.24.0",
"eslint-config-rollup": "^3.0.0",
"esm": "^3.2.25",
"execa": "^5.1.1",
"globby": "^11.0.1",
"husky": "8.0.1",
"lint-staged": "11.0.1",
"nyc": "^15.1.0",
"pnpm": "^7.12.2",
"prettier": "^2.2.1",
"prettier": "^2.7.1",
"prettier-plugin-package": "^1.3.0",
"semver": "^7.3.2",
"source-map-support": "^0.5.19",
"source-map-support": "^0.5.21",
"ts-node": "10.9.1",
"tsconfig-paths": "^3.9.0",
"typescript": "4.8.3",
"typescript": "^4.8.3",
"write-pkg": "^4.0.0",
"yaml": "^1.10.0",
"yargs-parser": "^20.2.9"
Expand Down
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
40 changes: 20 additions & 20 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,29 +49,25 @@
"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": {
"babel": {
"compileEnhancements": false
},
"extensions": [
"ts"
],
"require": [
"ts-node/register"
],
"files": [
"!**/fixtures/**",
"!**/output/**",
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
1 change: 1 addition & 0 deletions packages/alias/test/node_modules/current-package

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

57 changes: 48 additions & 9 deletions packages/alias/test/test.js → packages/alias/test/test.mjs
@@ -1,15 +1,14 @@
import path, { posix } from 'path';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';

import test from 'ava';
import { rollup } from 'rollup';

// eslint-disable-next-line import/no-unresolved, import/extensions
import nodeResolvePlugin from '@rollup/plugin-node-resolve';

import alias from '../dist';
import alias from 'current-package';

const normalizePath = (pathToNormalize) => pathToNormalize;
const DIRNAME = normalizePath(__dirname);
const DIRNAME = fileURLToPath(new URL('.', import.meta.url));

/**
* Helper function to test configuration with Rollup
Expand Down Expand Up @@ -197,9 +196,7 @@ test('Windows absolute path aliasing', (t) =>
importer: posix.resolve(DIRNAME, './fixtures/index.js')
}
]
).then((result) =>
t.deepEqual(result, [normalizePath('E:\\react\\node_modules\\fbjs\\lib\\warning')])
));
).then((result) => t.deepEqual(result, ['E:\\react\\node_modules\\fbjs\\lib\\warning'])));

/**
* Helper function to get moduleIDs from final Rollup bundle
Expand All @@ -211,7 +208,7 @@ const getModuleIdsFromBundle = (bundle) => {
return Promise.resolve(bundle.modules.map((module) => module.id));
}
return bundle
.generate({ format: 'esm' })
.generate({ format: 'es' })
.then((generated) => {
if (generated.output) {
return generated.output.length
Expand Down Expand Up @@ -565,3 +562,45 @@ test('CustomResolver plugin-like object with buildStart', (t) => {
})
);
});

test('Works as CJS plugin', async (t) => {
const require = createRequire(import.meta.url);
const aliasCjs = require('current-package');
const bundle = await rollup({
input: './test/fixtures/index.js',
plugins: [
aliasCjs({
entries: [
{ find: 'fancyNumber', replacement: './aliasMe' },
{
find: './anotherFancyNumber',
replacement: './localAliasMe'
},
{ find: 'numberFolder', replacement: './folder' },
{
find: './numberFolder',
replacement: './folder'
}
]
})
]
});
const moduleIds = await getModuleIdsFromBundle(bundle);
const normalizedIds = moduleIds.map((id) => path.resolve(id)).sort();
t.is(normalizedIds.length, 5);
[
'/fixtures/aliasMe.js',
'/fixtures/folder/anotherNumber.js',
'/fixtures/index.js',
'/fixtures/localAliasMe.js',
'/fixtures/nonAliased.js'
]
.map((id) => path.normalize(id))
.forEach((expectedId, index) =>
t.is(
normalizedIds[index].endsWith(expectedId),
true,
`expected ${normalizedIds[index]} to end with ${expectedId}`
)
);
});
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 2c88637

Please sign in to comment.