Skip to content

Commit

Permalink
feat: support custom tsconfig.json (#75)
Browse files Browse the repository at this point in the history
Co-authored-by: Filippo Bovo <[email protected]>
Co-authored-by: Hiroki Osame <[email protected]>
  • Loading branch information
3 people authored Jul 19, 2024
1 parent 9e99817 commit 6e80985
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ Pass in a Node.js target that that doesn't support it to strip the `node:` proto
pkgroll --target=node12.19
```

### Custom `tsconfig.json` path

By default, _Pkgroll_ looks for `tsconfig.json` configuration file in the current working directory. You can pass in a custom `tsconfig.json` path with the `--tsconfig` flag:

```sh
pkgroll --tsconfig=tsconfig.build.json
```

### Export condition

Similarly to the target, the export condition specifies which fields to read from when evaluating [export](https://nodejs.org/api/packages.html#exports) and [import](https://nodejs.org/api/packages.html#imports) maps.
Expand Down
9 changes: 8 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getAliases } from './utils/parse-package-json/get-aliases.js';
import { normalizePath } from './utils/normalize-path.js';
import { getSourcePath } from './utils/get-source-path.js';
import { getRollupConfigs } from './utils/get-rollup-configs.js';
import { tsconfig } from './utils/tsconfig.js';
import { getTsconfig } from './utils/get-tsconfig';
import { log } from './utils/log.js';
import { cleanDist } from './utils/clean-dist.js';

Expand Down Expand Up @@ -42,6 +42,11 @@ const argv = cli({
description: 'Environments to support. `target` in tsconfig.json is automatically added. Defaults to the current Node.js version.',
alias: 't',
},
tsconfig: {
type: String,
description: 'Custom tsconfig.json file path',
alias: 'p',
},
watch: {
type: Boolean,
description: 'Watch mode',
Expand Down Expand Up @@ -110,6 +115,7 @@ const cwd = process.cwd();
const sourcePath = normalizePath(argv.flags.src, true);
const distPath = normalizePath(argv.flags.dist, true);

const tsconfig = getTsconfig(argv.flags.tsconfig);
const tsconfigTarget = tsconfig?.config.compilerOptions?.target;
if (tsconfigTarget) {
argv.flags.target.push(tsconfigTarget);
Expand Down Expand Up @@ -153,6 +159,7 @@ if (tsconfigTarget) {
argv.flags,
getAliases(packageJson, cwd),
packageJson,
tsconfig,
);

if (argv.flags.cleanDist) {
Expand Down
8 changes: 7 additions & 1 deletion src/utils/get-rollup-configs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fs from 'fs';
import path from 'path';
import type { OutputOptions, RollupOptions, Plugin } from 'rollup';
import type { TransformOptions } from 'esbuild';
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import type { PackageJson } from 'type-fest';
import type { TsConfigResult } from 'get-tsconfig';
import type { ExportEntry, AliasMap } from '../types.js';
import { isFormatEsm, createRequire } from './rollup-plugins/create-require.js';
import { esbuildTransform, esbuildMinify } from './rollup-plugins/esbuild.js';
Expand Down Expand Up @@ -76,9 +78,11 @@ const getConfig = {
aliases: AliasMap,
env: EnvObject,
executablePaths: string[],
tsconfig: TsConfigResult | null,
) => {
const esbuildConfig = {
const esbuildConfig: TransformOptions = {
target: options.target,
tsconfigRaw: tsconfig?.config,
};

return {
Expand Down Expand Up @@ -144,6 +148,7 @@ export const getRollupConfigs = async (
flags: Options,
aliases: AliasMap,
packageJson: PackageJson,
tsconfig: TsConfigResult | null,
) => {

Check warning on line 152 in src/utils/get-rollup-configs.ts

View workflow job for this annotation

GitHub Actions / Release

Async arrow function has too many parameters (7). Maximum allowed is 5
const executablePaths = inputs
.filter(({ exportEntry }) => exportEntry.isExecutable)
Expand Down Expand Up @@ -204,6 +209,7 @@ export const getRollupConfigs = async (
aliases,
env,
executablePaths,
tsconfig,
);
config.external = externalDependencies;
configs.app = config;
Expand Down
17 changes: 17 additions & 0 deletions src/utils/get-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'path';
import { getTsconfig as _getTsconfig, parseTsconfig } from 'get-tsconfig';

export const getTsconfig = (
tscFile?: string,
) => {
if (!tscFile) {
return _getTsconfig();
}

const resolvedTscFile = path.resolve(tscFile);
const config = parseTsconfig(resolvedTscFile);
return {
path: resolvedTscFile,
config,
};
};
3 changes: 0 additions & 3 deletions src/utils/rollup-plugins/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Plugin, InternalModuleFormat } from 'rollup';
import { createFilter } from '@rollup/pluginutils';
import { transform, type TransformOptions, type Format } from 'esbuild';
import { tsconfig } from '../tsconfig.js';

export const esbuildTransform = (
options?: TransformOptions,
Expand All @@ -24,8 +23,6 @@ export const esbuildTransform = (

// https://github.com/evanw/esbuild/issues/1932#issuecomment-1013380565
sourcefile: id.replace(/\.[cm]ts/, '.ts'),

tsconfigRaw: tsconfig?.config as TransformOptions['tsconfigRaw'],
});

return {
Expand Down
3 changes: 0 additions & 3 deletions src/utils/tsconfig.ts

This file was deleted.

72 changes: 71 additions & 1 deletion tests/specs/builds/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { pkgroll } from '../../utils.js';
import { createPackageJson } from '../../fixtures.js';
import { createPackageJson, createTsconfigJson } from '../../fixtures.js';

export default testSuite(({ describe }, nodePath: string) => {
describe('TypeScript', ({ test }) => {
Expand Down Expand Up @@ -53,4 +53,74 @@ export default testSuite(({ describe }, nodePath: string) => {
expect(content).toBe('console.log(1);\n');
});
});

describe('custom tsconfig.json path', ({ test }) => {
test('respects compile target', async () => {
await using fixture = await createFixture({
src: {
'index.ts': 'export default () => "foo";',
},
'package.json': createPackageJson({
main: './dist/index.js',
}),
'tsconfig.json': createTsconfigJson({
compilerOptions: {
target: 'ES6',
},
}),
'tsconfig.build.json': createTsconfigJson({
compilerOptions: {
target: 'ES5',
},
}),
});

const pkgrollProcess = await pkgroll([
'--env.NODE_ENV=test',
'--tsconfig=tsconfig.build.json',
], {
cwd: fixture.path,
nodePath,
});

expect(pkgrollProcess.exitCode).toBe(0);
expect(pkgrollProcess.stderr).toBe('');

const content = await fixture.readFile('dist/index.js', 'utf8');
expect(content.includes('function')).toBe(true);
});

test('error on invalid tsconfig.json path', async () => {
const fixture = await createFixture({
src: {
'index.ts': 'export default () => "foo";',
},
'package.json': createPackageJson({
main: './dist/index.js',
}),
'tsconfig.json': createTsconfigJson({
compilerOptions: {
target: 'ES6',
},
}),
'tsconfig.build.json': createTsconfigJson({
compilerOptions: {
target: 'ES5',
},
}),
});

const pkgrollProcess = await pkgroll([
'--env.NODE_ENV=test',
'--tsconfig=tsconfig.invalid.json',
], {
cwd: fixture.path,
nodePath,
reject: false,
});

expect(pkgrollProcess.exitCode).toBe(1);
// expect(pkgrollProcess.stderr).toMatch('Cannot resolve tsconfig at path:');
});
});
});

0 comments on commit 6e80985

Please sign in to comment.