Skip to content

Commit eba5ee0

Browse files
committed
feat(auto-install)!: ESM only; tsc build and Vitest tests
1 parent 2c8b0e1 commit eba5ee0

File tree

19 files changed

+217
-294
lines changed

19 files changed

+217
-294
lines changed

packages/auto-install/package.json

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,30 @@
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+
"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
],
@@ -53,7 +49,7 @@
5349
"modules"
5450
],
5551
"peerDependencies": {
56-
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
52+
"rollup": ">=4.0.0"
5753
},
5854
"peerDependenciesMeta": {
5955
"rollup": {
@@ -62,21 +58,11 @@
6258
},
6359
"devDependencies": {
6460
"@rollup/plugin-node-resolve": "^15.0.0",
65-
"@rollup/plugin-typescript": "^9.0.1",
6661
"del": "^6.1.1",
62+
"del-cli": "^5.0.0",
6763
"node-noop": "^1.0.0",
68-
"rollup": "^4.0.0-24",
69-
"typescript": "^4.8.3"
64+
"rollup": "^4.0.0",
65+
"typescript": "catalog:"
7066
},
71-
"types": "./types/index.d.ts",
72-
"ava": {
73-
"workerThreads": false,
74-
"files": [
75-
"!**/fixtures/**",
76-
"!**/output/**",
77-
"!**/helpers/**",
78-
"!**/recipes/**",
79-
"!**/types.ts"
80-
]
81-
}
67+
"types": "./dist/index.d.ts"
8268
}

packages/auto-install/rollup.config.mjs

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

packages/auto-install/src/index.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
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+
/**
10+
* Options for the auto-install plugin.
11+
*
12+
* Note: The `commands` option is intentionally undocumented and is used
13+
* only in tests to stub package manager invocations.
14+
*/
15+
export interface RollupAutoInstallOptions {
16+
/**
17+
* Specifies the location on disk of the target `package.json` file.
18+
* If the file doesn't exist, it will be created by the plugin,
19+
* as package managers need to populate the `dependencies` property.
20+
* @default '{cwd}/package.json'
21+
*/
22+
pkgFile?: string;
23+
24+
/**
25+
* Specifies the package manager to use.
26+
* If not specified, the plugin will default to `yarn` if `yarn.lock` exists,
27+
* to `pnpm` if `pnpm-lock.yaml` exists, or `npm` otherwise.
28+
*/
29+
manager?: 'npm' | 'yarn' | 'pnpm';
30+
31+
/** Test-only override of package manager commands. */
32+
commands?: Partial<Record<'npm' | 'yarn' | 'pnpm', string>>;
33+
}
1034

1135
const execAsync = promisify(exec);
1236

@@ -41,7 +65,7 @@ export default function autoInstall(opts: RollupAutoInstallOptions = {}): Plugin
4165
pkg = {};
4266
}
4367

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

4771
return {

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

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
import del from 'del';
6+
import { it, expect, afterAll } from 'vitest';
7+
import { rollup } from 'rollup';
8+
import nodeResolve from '@rollup/plugin-node-resolve';
9+
10+
import autoInstall from '~package';
11+
12+
const DIRNAME = fileURLToPath(new URL('.', import.meta.url));
13+
const cwd = path.join(DIRNAME, 'fixtures/npm-bare');
14+
const file = path.join(cwd, 'output/bundle.js');
15+
const input = path.join(cwd, '../input.js');
16+
17+
it('npm, bare', async () => {
18+
process.chdir(cwd);
19+
const bundle = await rollup({
20+
input,
21+
output: { file, format: 'cjs' },
22+
plugins: [autoInstall({ manager: 'npm' }), nodeResolve()]
23+
});
24+
await bundle.close();
25+
26+
const pkgJson = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
27+
expect(pkgJson).toHaveProperty('dependencies');
28+
expect(Object.keys(pkgJson.dependencies)).toContain('node-noop');
29+
expect(fs.readFileSync('package-lock.json', 'utf-8')).toMatch(/"node-noop"/);
30+
});
31+
32+
afterAll(async () => {
33+
await del(['node_modules', 'package.json', 'package-lock.json']);
34+
});

packages/auto-install/test/npm.js

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

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

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
import del from 'del';
6+
import { it, expect, afterAll } from 'vitest';
7+
import { rollup } from 'rollup';
8+
import nodeResolve from '@rollup/plugin-node-resolve';
9+
10+
import autoInstall from '~package';
11+
12+
const DIRNAME = fileURLToPath(new URL('.', import.meta.url));
13+
const cwd = path.join(DIRNAME, 'fixtures/pnpm-bare');
14+
const file = path.join(cwd, 'output/bundle.js');
15+
const input = path.join(cwd, '../input.js');
16+
17+
it('pnpm, bare', async () => {
18+
process.chdir(cwd);
19+
const bundle = await rollup({
20+
input,
21+
output: { file, format: 'cjs' },
22+
plugins: [autoInstall({ manager: 'pnpm' }), nodeResolve()]
23+
});
24+
await bundle.close();
25+
26+
const json = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
27+
expect(json).toHaveProperty('dependencies');
28+
expect(Object.keys(json.dependencies)).toContain('node-noop');
29+
});
30+
31+
afterAll(async () => {
32+
await del(['node_modules', 'package.json', 'pnpm-lock.yaml']);
33+
});

0 commit comments

Comments
 (0)