Skip to content

Commit

Permalink
feat(typescript): add resolve options (#1015)
Browse files Browse the repository at this point in the history
* Add resolve options to typescript plugin.ts

* Add documentation for resolve options to typescript plugin.ts

* Change to filterRoot for resolve options to typescript plugin.ts

* Change to use parsed options rootDir
Update readme.md

* Added tests

* Prettier fix

* Lint fix

Co-authored-by: Josh <chiefidiot@users.noreply.github.com>
  • Loading branch information
janpe and chiefidiot committed Oct 15, 2021
1 parent 00e16d9 commit 8ff10f6
Show file tree
Hide file tree
Showing 14 changed files with 477 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .pnpm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"0 debug pnpm:scope": {
"selected": 1,
"workspacePrefix": "/Users/jani/projects/plugins"
}
}
23 changes: 23 additions & 0 deletions packages/typescript/.pnpm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"0 debug pnpm:scope": {
"selected": 1,
"workspacePrefix": "/Users/jani/projects/plugins"
},
"1 error pnpm": {
"code": "ELIFECYCLE",
"errno": "ENOENT",
"syscall": "spawn",
"file": "sh",
"pkgid": "@rollup/plugin-typescript@8.2.5",
"stage": "prebuild",
"script": "del-cli dist",
"pkgname": "@rollup/plugin-typescript",
"err": {
"name": "pnpm",
"message": "@rollup/plugin-typescript@8.2.5 prebuild: `del-cli dist`\nspawn ENOENT",
"code": "ELIFECYCLE",
"stack": "pnpm: @rollup/plugin-typescript@8.2.5 prebuild: `del-cli dist`\nspawn ENOENT\n at ChildProcess.<anonymous> (/Users/jani/.config/yarn/global/node_modules/pnpm/dist/pnpm.cjs:93136:22)\n at ChildProcess.emit (node:events:394:28)\n at maybeClose (node:internal/child_process:1064:16)\n at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)"
}
},
"2 warn pnpm:global": " Local package.json exists, but node_modules missing, did you mean to install?"
}
11 changes: 11 additions & 0 deletions packages/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all `.ts` and `.tsx` files are targeted.

### `filterRoot`

Type: `String` | `Boolean`<br>
Default: `rootDir` ?? `tsConfig.compilerOptions.rootDir` ?? `process.cwd()`

Optionally resolves the include and exclude patterns against a directory other than `process.cwd()`. If a String is specified, then the value will be used as the base directory. Relative paths will be resolved against `process.cwd()` first. If `false`, then the patterns will not be resolved against any directory.

By default, patterns resolve against the rootDir set in your TS config file.

This can fix plugin errors when parsing files outside the current working directory (process.cwd()).

### `tsconfig`

Type: `String` | `Boolean`<br>
Expand Down
13 changes: 10 additions & 3 deletions packages/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as path from 'path';

import { createFilter } from '@rollup/pluginutils';

import { Plugin, RollupOptions, SourceDescription } from 'rollup';
import type { Watch } from 'typescript';

Expand All @@ -19,18 +21,23 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
const {
cacheDir,
compilerOptions,
filter,
exclude,
filterRoot,
include,
outputToFilesystem,
transformers,
tsconfig,
tslib,
typescript: ts,
outputToFilesystem
typescript: ts
} = getPluginOptions(options);
const tsCache = new TSCache(cacheDir);
const emittedFiles = new Map<string, string>();
const watchProgramHelper = new WatchProgramHelper();

const parsedOptions = parseTypescriptConfig(ts, tsconfig, compilerOptions);
const filter = createFilter(include || ['*.ts+(|x)', '**/*.ts+(|x)'], exclude, {
resolve: filterRoot ?? parsedOptions.options.rootDir
});
parsedOptions.fileNames = parsedOptions.fileNames.filter(filter);

const formatHost = createFormattingHost(ts, parsedOptions.options);
Expand Down
8 changes: 4 additions & 4 deletions packages/typescript/src/options/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createFilter } from '@rollup/pluginutils';
import * as defaultTs from 'typescript';

import { RollupTypescriptOptions, PartialCompilerOptions } from '../../types';
Expand All @@ -19,6 +18,7 @@ export const getPluginOptions = (options: RollupTypescriptOptions) => {
cacheDir,
exclude,
include,
filterRoot,
transformers,
tsconfig,
tslib,
Expand All @@ -27,11 +27,11 @@ export const getPluginOptions = (options: RollupTypescriptOptions) => {
...compilerOptions
} = options;

const filter = createFilter(include || ['*.ts+(|x)', '**/*.ts+(|x)'], exclude);

return {
cacheDir,
filter,
include,
exclude,
filterRoot,
tsconfig,
compilerOptions: compilerOptions as PartialCompilerOptions,
typescript: typescript || defaultTs,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar' as string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { foo } from '../include';

const answer = 42;
// eslint-disable-next-line no-console
console.log(`the answer is ${answer}, ${foo}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { foo } from '../include';

const answer = 42;
// eslint-disable-next-line no-console
console.log(`the answer is ${answer}, ${foo}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
5 changes: 5 additions & 0 deletions packages/typescript/test/fixtures/root-dir/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"rootDir": "./"
}
}
34 changes: 34 additions & 0 deletions packages/typescript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,40 @@ test.serial('normalizes resolved ids to avoid duplicate output on windows', asyn
t.true(files[1].code.includes("import { one } from './one.js';"), files[1].code);
});

test.serial('does it support tsconfig.rootDir for filtering', async (t) => {
process.chdir('fixtures/root-dir/packages/test-1');
const bundle = await rollup({
input: 'main.ts',
plugins: [typescript({ tsconfig: 'tsconfig.json' })]
});

const files = await getCode(bundle, { format: 'esm' }, true);
// Compiles with no errors
t.is(files.length, 1);
});

test.serial('does it fail for filtering with incorrect rootDir in nested projects', async (t) => {
process.chdir('fixtures/root-dir/packages/test-2');
const error = await t.throwsAsync(
rollup({
input: 'main.ts',
plugins: [typescript({ tsconfig: 'tsconfig.json' })]
})
);
// Will throw parse error because it includes a typescript file outside CWD
t.is(error.code, 'PARSE_ERROR');
});

test.serial('does manually setting filterRoot resolve nested projects', async (t) => {
process.chdir('fixtures/root-dir/packages/test-2');
const bundle = await rollup({
input: 'main.ts',
plugins: [typescript({ tsconfig: 'tsconfig.json', filterRoot: '../../' })]
});
const files = await getCode(bundle, { format: 'esm' }, true);
t.is(files.length, 1);
});

test.serial('does not warn if sourceMap is set in Rollup and unset in Typescript', async (t) => {
const warnings = [];
const bundle = await rollup({
Expand Down
5 changes: 5 additions & 0 deletions packages/typescript/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export interface RollupTypescriptPluginOptions {
* Determine which files are ignored by Typescript
*/
exclude?: FilterPattern;
/**
* Sets the `resolve` value for the underlying filter function. If not set will use the `rootDir` property
* @see {@link https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter} @rollup/pluginutils `createFilter`
*/
filterRoot?: string | false;
/**
* When set to false, ignores any options specified in the config file.
* If set to a string that corresponds to a file path, the specified file
Expand Down
Loading

0 comments on commit 8ff10f6

Please sign in to comment.