Skip to content

Commit 4656bc2

Browse files
feat(alias)!: ESM only. Update Node and Rollup minimum versions (#1926)
* feat(alias)!: ESM only; tsc-only build; Node >=20.19.0 and Rollup >=4\n\n- Convert @rollup/plugin-alias to ESM-only with "type": "module" and exports { import, types } to dist\n- Drop Rollup from package build; build with tsc to dist (JS, d.ts, and maps)\n- Raise engines.node to >=20.19.0 and peer rollup to >=4.0.0; keep dev rollup for tests\n- Inline and export public types from src; remove hand-authored types/\n- Add shared .config/{tsconfig.base.json,tsconfig.plugin.json,vitest.config.ts}\n- Switch tests to Vitest (TypeScript), import from ~package, drop CJS branch\n- Update CONTRIBUTING: Vitest + ~package alias + snapshots\n- Bump workspace TypeScript and @types/node; install Vitest at the root * chore(alias): address self-review feedback\n\n- Keep library maps out of published package\n- Normalize backslashes reliably in warning test\n- Retain function-only ResolverFunction mapping for Rollup hook typing\n- Remove tsconfig baseUrl/paths from build tsconfig (alias is runtime via Vitest) * chore(alias): address review feedback\n\n- tests: close Rollup bundle in helper to avoid leaks/flakes\n- types: narrow MapToFunction to explicit callable form (avoid global Function) * merge: resolve conflicts in alias types/tests; adopt robust bundle close in tests * chore(repo): use .mts for shared Vitest config; update alias test script/docs - Rename .config/vitest.config.ts -> .config/vitest.config.mts per review - Update packages/alias test script to reference .mts - Update CONTRIBUTING to mention .mts config * chore(alias): fix ci:test to use Vitest reporter instead of unsupported --verbose * chore(repo): use pnpm catalog for TypeScript and align alias (typescript -> catalog:) - Add default catalog in pnpm-workspace.yaml: typescript ^5.9.3\n- Update packages/alias to reference catalog for TypeScript\n- Bump packageManager to pnpm@9.5.0 to support catalogs * chore(repo): use pnpm catalog for TypeScript in root package.json --------- Co-authored-by: CharlieHelps <charlie@charlielabs.ai>
1 parent 7ebe5eb commit 4656bc2

File tree

15 files changed

+1619
-844
lines changed

15 files changed

+1619
-844
lines changed

.config/tsconfig.base.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": true,
4+
"esModuleInterop": true,
5+
"lib": ["ESNext"],
6+
"module": "esnext",
7+
"moduleResolution": "bundler",
8+
"noEmit": true,
9+
"noUnusedLocals": true,
10+
"noUnusedParameters": true,
11+
"pretty": true,
12+
"sourceMap": true,
13+
"strict": true,
14+
"target": "ES2022"
15+
},
16+
"exclude": ["dist", "node_modules", "test/types"]
17+
}

.config/tsconfig.plugin.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// Important: this config is symlinked into package directories as `tsconfig.json`.
3+
// The `extends` path must therefore be relative to the symlink location (e.g. packages/<pkg>/).
4+
"extends": "../../.config/tsconfig.base.json",
5+
"compilerOptions": {
6+
"noEmit": false,
7+
"outDir": "dist",
8+
"rootDir": "src",
9+
"declaration": true,
10+
"declarationMap": true
11+
},
12+
"include": ["src/**/*"]
13+
}

.config/vitest.config.mts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from 'vitest/config';
2+
import path from 'node:path';
3+
4+
export default defineConfig({
5+
test: {
6+
// Store snapshots next to each test in a .snapshots folder
7+
resolveSnapshotPath: (testPath, snapExt) =>
8+
path.join(path.dirname(testPath), '.snapshots', path.basename(testPath) + snapExt)
9+
},
10+
resolve: {
11+
// Allow importing the current package under test via `~package`
12+
alias: [{ find: /^~package$/, replacement: path.resolve(process.cwd()) }]
13+
}
14+
});

.github/CONTRIBUTING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ Any code change should be submitted as a pull request. Our guidelines for Pull R
2626
- Before submitting your Pull Request, please lint your changes by running `pnpm lint` in the root directory
2727
- If any checks fail for your Pull Request, please resolve them. Always feel free to ask for help if unable to resolve issues with checks
2828

29+
## Testing packages
30+
31+
Some packages use Vitest for tests. When converting or running tests locally inside a package directory (e.g. `packages/alias`):
32+
33+
- Tests import the package under test via `~package`. A shared config at `.config/vitest.config.mts` provides a runtime alias that resolves `~package` to the current working directory.
34+
- Snapshots are written alongside each test under a local `.snapshots` directory.
35+
36+
Example (from a package directory):
37+
38+
```
39+
pnpm build
40+
pnpm test
41+
```
42+
2943
## Code review process
3044

3145
The bigger the pull request, the longer it will take to review and merge. Try to break down large pull requests in smaller chunks that are easier to review and merge.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"devDependencies": {
2020
"@dot/versioner": "^0.3.1",
2121
"@rollup/plugin-typescript": "^9.0.1",
22-
"@types/node": "14.18.30",
22+
"@types/node": "^20.19.0",
2323
"@types/source-map-support": "^0.5.4",
2424
"@typescript-eslint/eslint-plugin": "^5.38.0",
2525
"@typescript-eslint/parser": "^5.38.0",
@@ -39,7 +39,8 @@
3939
"prettier-plugin-package": "^1.3.0",
4040
"source-map-support": "^0.5.21",
4141
"ts-node": "10.9.1",
42-
"typescript": "^4.8.3"
42+
"typescript": "catalog:",
43+
"vitest": "^4.0.2"
4344
},
4445
"ava": {
4546
"files": [
@@ -77,5 +78,5 @@
7778
"test/"
7879
]
7980
},
80-
"packageManager": "pnpm@9.4.0"
81+
"packageManager": "pnpm@9.5.0"
8182
}

packages/alias/package.json

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,30 @@
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/cjs/index.js",
17-
"module": "./dist/es/index.js",
16+
"type": "module",
1817
"exports": {
19-
"types": "./types/index.d.ts",
20-
"import": "./dist/es/index.js",
21-
"default": "./dist/cjs/index.js"
18+
"types": "./dist/index.d.ts",
19+
"import": "./dist/index.js"
2220
},
2321
"engines": {
24-
"node": ">=14.0.0"
22+
"node": ">=20.19.0"
2523
},
2624
"scripts": {
27-
"build": "rollup -c",
25+
"build": "tsc --project tsconfig.json",
2826
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
2927
"ci:lint": "pnpm build && pnpm lint",
3028
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
31-
"ci:test": "pnpm test -- --verbose",
29+
"ci:test": "pnpm test -- --reporter=verbose",
3230
"prebuild": "del-cli dist",
3331
"prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
3432
"prerelease": "pnpm build",
3533
"pretest": "pnpm build",
3634
"release": "pnpm --workspace-root package:release $(pwd)",
37-
"test": "ava",
38-
"test:ts": "tsc --noEmit"
35+
"test": "vitest --config ../../.config/vitest.config.mts run"
3936
},
4037
"files": [
4138
"dist",
4239
"!dist/**/*.map",
43-
"types",
4440
"README.md",
4541
"LICENSE"
4642
],
@@ -51,7 +47,7 @@
5147
"alias"
5248
],
5349
"peerDependencies": {
54-
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
50+
"rollup": ">=4.0.0"
5551
},
5652
"peerDependenciesMeta": {
5753
"rollup": {
@@ -60,19 +56,9 @@
6056
},
6157
"devDependencies": {
6258
"@rollup/plugin-node-resolve": "^15.0.0",
63-
"@rollup/plugin-typescript": "^9.0.1",
6459
"del-cli": "^5.0.0",
65-
"rollup": "^4.0.0-24",
66-
"typescript": "^4.8.3"
60+
"rollup": "^4.0.0",
61+
"typescript": "catalog:"
6762
},
68-
"types": "./types/index.d.ts",
69-
"ava": {
70-
"files": [
71-
"!**/fixtures/**",
72-
"!**/output/**",
73-
"!**/helpers/**",
74-
"!**/recipes/**",
75-
"!**/types.ts"
76-
]
77-
}
63+
"types": "./dist/index.d.ts"
7864
}

packages/alias/rollup.config.mjs

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

packages/alias/src/index.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
1-
import path from 'path';
1+
import path from 'node:path';
22

3-
import type { Plugin } from 'rollup';
3+
// Public types are exported directly from source so tsc can emit declarations
4+
import type { Plugin, PluginHooks } from 'rollup';
45

5-
import type { ResolvedAlias, ResolverFunction, ResolverObject, RollupAliasOptions } from '../types';
6+
// Narrow to explicit callable form instead of the global `Function` type.
7+
type MapToFunction<T> = T extends (...args: any[]) => any ? T : never;
8+
9+
export type ResolverFunction = MapToFunction<PluginHooks['resolveId']>;
10+
11+
export interface ResolverObject {
12+
buildStart?: PluginHooks['buildStart'];
13+
resolveId: ResolverFunction;
14+
}
15+
16+
export interface Alias {
17+
find: string | RegExp;
18+
replacement: string;
19+
customResolver?: ResolverFunction | ResolverObject | null;
20+
}
21+
22+
export interface ResolvedAlias {
23+
find: string | RegExp;
24+
replacement: string;
25+
resolverFunction: ResolverFunction | null;
26+
}
27+
28+
export interface RollupAliasOptions {
29+
/**
30+
* Instructs the plugin to use an alternative resolving algorithm,
31+
* rather than the Rollup's resolver.
32+
* @default null
33+
*/
34+
customResolver?: ResolverFunction | ResolverObject | null;
35+
36+
/**
37+
* Specifies an `Object`, or an `Array` of `Object`,
38+
* which defines aliases used to replace values in `import` or `require` statements.
39+
* With either format, the order of the entries is important,
40+
* in that the first defined rules are applied first.
41+
*/
42+
entries?: readonly Alias[] | { [find: string]: string };
43+
}
644

745
function matches(pattern: string | RegExp, importee: string) {
846
if (pattern instanceof RegExp) {
@@ -40,7 +78,7 @@ function getEntries({ entries, customResolver }: RollupAliasOptions): readonly R
4078
});
4179
}
4280

43-
function getHookFunction<T extends Function>(hook: T | { handler?: T }): T | null {
81+
function getHookFunction<T extends (...args: any[]) => any>(hook: T | { handler?: T }): T | null {
4482
if (typeof hook === 'function') {
4583
return hook;
4684
}
@@ -104,7 +142,7 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
104142

105143
if (!path.isAbsolute(updatedId)) {
106144
this.warn(
107-
`rewrote ${importee} to ${updatedId} but was not an abolute path and was not handled by other plugins. ` +
145+
`rewrote ${importee} to ${updatedId} but was not an absolute path and was not handled by other plugins. ` +
108146
`This will lead to duplicated modules for the same path. ` +
109147
`To avoid duplicating modules, you should resolve to an absolute path.`
110148
);

0 commit comments

Comments
 (0)