Skip to content

Commit aa02b21

Browse files
committed
feat(auto-install)!: ESM only; tsc build; migrate tests to Vitest per playbook\n\n- Use type: module and ESM-only exports at . with types/import/default\n- engines.node >=20.19.0; peerDependencies.rollup >=4.0.0\n- tsc build via shared .config/tsconfig.plugin.json (symlink); remove rollup config and hand-authored types/\n- Update source to use node: specifiers and inline public types\n- Convert AVA tests to Vitest; close bundles; restore CWD in teardown\n- Keep changes scoped to packages/auto-install only
1 parent 2c8b0e1 commit aa02b21

21 files changed

+282
-335
lines changed

packages/auto-install/package.json

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,33 @@
1313
"author": "Rich Harris",
1414
"homepage": "https://github.com/rollup/plugins/tree/master/packages/auto-install/#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+
".": {
19+
"types": "./dist/index.d.ts",
20+
"import": "./dist/index.js",
21+
"default": "./dist/index.js"
22+
}
2223
},
2324
"engines": {
24-
"node": ">=14.0.0"
25+
"node": ">=20.19.0"
2526
},
2627
"scripts": {
27-
"build": "rollup -c",
28+
"build": "tsc --project tsconfig.json",
2829
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
2930
"ci:lint": "pnpm build && pnpm lint",
3031
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
31-
"ci:test": "pnpm test -- --verbose",
32+
"ci:test": "pnpm test -- --reporter=verbose",
3233
"prebuild": "del-cli dist",
3334
"prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
3435
"prerelease": "pnpm build",
3536
"pretest": "pnpm build",
3637
"release": "pnpm --workspace-root package:release $(pwd)",
37-
"test": "ava",
38+
"test": "vitest --config ../../.config/vitest.config.mts run",
3839
"test:ts": "tsc --noEmit"
3940
},
4041
"files": [
4142
"dist",
42-
"!dist/**/*.map",
43-
"types",
4443
"README.md",
4544
"LICENSE"
4645
],
@@ -53,7 +52,7 @@
5352
"modules"
5453
],
5554
"peerDependencies": {
56-
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
55+
"rollup": ">=4.0.0"
5756
},
5857
"peerDependenciesMeta": {
5958
"rollup": {
@@ -62,13 +61,13 @@
6261
},
6362
"devDependencies": {
6463
"@rollup/plugin-node-resolve": "^15.0.0",
65-
"@rollup/plugin-typescript": "^9.0.1",
6664
"del": "^6.1.1",
65+
"del-cli": "^5.0.0",
6766
"node-noop": "^1.0.0",
68-
"rollup": "^4.0.0-24",
69-
"typescript": "^4.8.3"
67+
"rollup": "^4.0.0",
68+
"typescript": "catalog:"
7069
},
71-
"types": "./types/index.d.ts",
70+
"types": "./dist/index.d.ts",
7271
"ava": {
7372
"workerThreads": false,
7473
"files": [

packages/auto-install/rollup.config.mjs

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

packages/auto-install/src/index.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
1-
import * as fs from 'fs';
2-
import * as path from 'path';
3-
import mod from 'module';
4-
import { exec } from 'child_process';
5-
import { promisify } from 'util';
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { builtinModules } from 'node:module';
4+
import { exec } from 'node:child_process';
5+
import { promisify } from 'node:util';
66

77
import type { Plugin } from 'rollup';
88

9-
import type { RollupAutoInstallOptions } from '../types';
9+
export interface RollupAutoInstallOptions {
10+
/**
11+
* Specifies the location on disk of the target `package.json` file.
12+
* If the file doesn't exist, it will be created by the plugin,
13+
* as package managers need to populate the `dependencies` property.
14+
* @default '{cwd}/package.json'
15+
*/
16+
pkgFile?: string;
17+
18+
/**
19+
* Specifies the package manager to use.
20+
* If not specified, the plugin will default to `yarn` if `yarn.lock` exists,
21+
* to `pnpm` if `pnpm-lock.yaml` exists, or `npm` otherwise.
22+
*/
23+
manager?: 'npm' | 'yarn' | 'pnpm';
24+
}
1025

1126
const execAsync = promisify(exec);
1227

1328
export default function autoInstall(opts: RollupAutoInstallOptions = {}): Plugin {
14-
const defaults = {
15-
// intentionally undocumented options. used for tests
16-
commands: {
17-
npm: 'npm install',
18-
pnpm: 'pnpm install',
19-
yarn: 'yarn add'
20-
},
21-
manager: fs.existsSync('yarn.lock') ? 'yarn' : fs.existsSync('pnpm-lock.yaml') ? 'pnpm' : 'npm',
22-
pkgFile: path.resolve(opts.pkgFile || 'package.json')
29+
const commands: Record<'npm' | 'yarn' | 'pnpm', string> = {
30+
npm: 'npm install',
31+
pnpm: 'pnpm install',
32+
yarn: 'yarn add'
2333
};
2434

25-
const options = Object.assign({}, defaults, opts);
26-
const { manager, pkgFile } = options;
27-
const validManagers = ['npm', 'yarn', 'pnpm'];
35+
const manager = opts.manager
36+
? opts.manager
37+
: fs.existsSync('yarn.lock')
38+
? 'yarn'
39+
: fs.existsSync('pnpm-lock.yaml')
40+
? 'pnpm'
41+
: 'npm';
42+
43+
const pkgFile = path.resolve(opts.pkgFile || 'package.json');
44+
const validManagers: Array<RollupAutoInstallOptions['manager']> = ['npm', 'yarn', 'pnpm'];
2845

2946
if (!validManagers.includes(manager)) {
3047
throw new RangeError(
@@ -41,8 +58,8 @@ export default function autoInstall(opts: RollupAutoInstallOptions = {}): Plugin
4158
pkg = {};
4259
}
4360

44-
const installed = new Set(Object.keys(pkg.dependencies || {}).concat(mod.builtinModules));
45-
const cmd = options.commands[manager];
61+
const installed = new Set(Object.keys(pkg.dependencies || {}).concat(builtinModules));
62+
const cmd = commands[manager];
4663

4764
return {
4865
name: 'auto-install',

packages/auto-install/test/npm-bare.js

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
import del from 'del';
5+
import { it, afterAll } from 'vitest';
6+
import { rollup } from 'rollup';
7+
import nodeResolve from '@rollup/plugin-node-resolve';
8+
9+
import autoInstall from '~package';
10+
11+
const DIR = import.meta.dirname;
12+
const cwd = path.join(DIR, 'fixtures/npm-bare');
13+
const file = path.join(cwd, 'output/bundle.js');
14+
const input = path.join(cwd, '../input.js');
15+
const pkgFile = path.join(cwd, 'package.json');
16+
17+
const PREV_CWD = process.cwd();
18+
19+
it('npm, bare', async () => {
20+
process.chdir(cwd);
21+
const bundle = await rollup({
22+
input,
23+
// @ts-expect-error - rollup() ignores output here but tests kept it historically
24+
output: { file, format: 'es' },
25+
plugins: [autoInstall({ pkgFile, manager: 'npm' }), nodeResolve()]
26+
});
27+
await bundle.close();
28+
29+
const json = JSON.parse(fs.readFileSync(pkgFile, 'utf-8'));
30+
if (!json.dependencies || !json.dependencies['node-noop']) {
31+
throw new Error('Expected node-noop to be added to dependencies');
32+
}
33+
});
34+
35+
afterAll(async () => {
36+
await del(['node_modules', 'package.json', 'package-lock.json']);
37+
process.chdir(PREV_CWD);
38+
});

packages/auto-install/test/npm.js

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
import del from 'del';
5+
import { it, expect, afterAll } from 'vitest';
6+
import { rollup } from 'rollup';
7+
import nodeResolve from '@rollup/plugin-node-resolve';
8+
9+
import autoInstall from '~package';
10+
11+
const DIR = import.meta.dirname;
12+
const cwd = path.join(DIR, 'fixtures/npm');
13+
const file = path.join(cwd, 'output/bundle.js');
14+
const input = path.join(cwd, '../input.js');
15+
const pkgFile = path.join(cwd, 'package.json');
16+
17+
const PREV_CWD = process.cwd();
18+
19+
it('invalid manager', () => {
20+
expect(() => autoInstall({ pkgFile, manager: 'foo' as any })).toThrow(RangeError);
21+
});
22+
23+
it('npm', async () => {
24+
process.chdir(cwd);
25+
const bundle = await rollup({
26+
input,
27+
// @ts-expect-error - rollup() ignores output here but tests kept it historically
28+
output: { file, format: 'es' },
29+
plugins: [autoInstall({ pkgFile, manager: 'npm' }), nodeResolve()]
30+
});
31+
await bundle.close();
32+
33+
const json = JSON.parse(fs.readFileSync(pkgFile, 'utf-8'));
34+
expect(json.dependencies && json.dependencies['node-noop']).toBeDefined();
35+
});
36+
37+
afterAll(async () => {
38+
await del(['node_modules', 'package-lock.json']);
39+
fs.writeFileSync(pkgFile, '{}');
40+
process.chdir(PREV_CWD);
41+
});

packages/auto-install/test/pnpm-bare.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
import del from 'del';
5+
import { it, afterAll } from 'vitest';
6+
import { rollup } from 'rollup';
7+
import nodeResolve from '@rollup/plugin-node-resolve';
8+
9+
import autoInstall from '~package';
10+
11+
const DIR = import.meta.dirname;
12+
const cwd = path.join(DIR, 'fixtures/pnpm-bare');
13+
const file = path.join(cwd, 'output/bundle.js');
14+
const input = path.join(cwd, '../input.js');
15+
16+
const PREV_CWD = process.cwd();
17+
18+
it('pnpm, bare', async () => {
19+
process.chdir(cwd);
20+
const bundle = await rollup({
21+
input,
22+
// @ts-expect-error - rollup() ignores output here but tests kept it historically
23+
output: { file, format: 'es' },
24+
plugins: [autoInstall({ manager: 'pnpm' }), nodeResolve()]
25+
});
26+
await bundle.close();
27+
28+
const json = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf-8'));
29+
if (!json.dependencies || !json.dependencies['node-noop']) {
30+
throw new Error('Expected node-noop to be added to dependencies');
31+
}
32+
});
33+
34+
afterAll(async () => {
35+
await del(['node_modules', 'package.json', 'pnpm-lock.yaml']);
36+
process.chdir(PREV_CWD);
37+
});

0 commit comments

Comments
 (0)