Skip to content

#15 added basic tsconfig extends support #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { join } from 'path';
import path from 'path';
import { Plugin } from 'rollup';
import {
CompilerOptions,
findConfigFile,
nodeModuleNameResolver,
parseConfigFileTextToJson,
sys,
} from 'typescript';
import * as ts from 'typescript';

export const typescriptPaths = ({
absolute = true,
nonRelative = false,
preserveExtensions = false,
tsConfigPath = findConfigFile('./', sys.fileExists),
tsConfigPath = ts.findConfigFile('./', ts.sys.fileExists),
transform,
}: Options = {}): Plugin => {
const { compilerOptions, outDir } = getTsConfig(tsConfigPath);
const compilerOptions = getTsConfig(tsConfigPath);

return {
name: 'resolve-typescript-paths',
Expand Down Expand Up @@ -46,11 +40,11 @@ export const typescriptPaths = ({
return null; // never resolve relative modules, only non-relative
}

const { resolvedModule } = nodeModuleNameResolver(
const { resolvedModule } = ts.nodeModuleNameResolver(
importee,
importer,
compilerOptions,
sys,
ts.sys,
);

if (!resolvedModule) {
Expand All @@ -63,15 +57,16 @@ export const typescriptPaths = ({
return null;
}

const targetFileName = join(
outDir,
// TODO: Do we need outDir as "resolvedFileName" is already correct absolute path
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think resolvedFileName is only the correct absolute path with Rollup 3 but not Rollup 2 or so.

const targetFileName = path.join(
compilerOptions.outDir,
preserveExtensions
? resolvedFileName
: resolvedFileName.replace(/\.tsx?$/i, '.js'),
);

const resolved = absolute
? sys.resolvePath(targetFileName)
? ts.sys.resolvePath(targetFileName)
: targetFileName;

return transform ? transform(resolved) : resolved;
Expand All @@ -80,21 +75,45 @@ export const typescriptPaths = ({
};

const getTsConfig = (configPath?: string): TsConfig => {
const defaults: TsConfig = { compilerOptions: {}, outDir: '.' };

if (!configPath) {
const defaults: TsConfig = { outDir: '.' };
if (typeof configPath !== 'string') {
return defaults;
}

const configJson = sys.readFile(configPath);
// Define a host object that implements ParseConfigFileHost.
// The host provides file system operations and error handling for parsing the configuration file.
const host: ts.ParseConfigFileHost = {
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
getCurrentDirectory: ts.sys.getCurrentDirectory,
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
console.error(
'Unrecoverable error in config file:',
diagnostic.messageText,
);
process.exit(1);
},
};

if (!configJson) {
return defaults;
// Read in tsconfig.json
const parsedCommandLine = ts.getParsedCommandLineOfConfigFile(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a lot slower than what happened before, so probably it makes sense to memoize the getTsConfig function for performance. I can add this (hopefully i'll find some time to properly go through this PR in the next few weeks).

configPath,
{},
host,
);

// Access the parsed tsconfig.json file options
let resolvedConfig = {};
if (parsedCommandLine != null) {
resolvedConfig = parsedCommandLine.options;
} else {
console.error('Failed to parse TypeScript configuration file:', configPath);
process.exit(1);
}

const { config } = parseConfigFileTextToJson(configPath, configJson);

return { ...defaults, ...config };
return { ...defaults, ...resolvedConfig };
};

export interface Options {
Expand Down Expand Up @@ -131,10 +150,9 @@ export interface Options {
transform?(path: string): string;
}

interface TsConfig {
compilerOptions: CompilerOptions;
type TsConfig = {
outDir: string;
}
} & Omit<ts.CompilerOptions, 'outDir'>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be like

Suggested change
} & Omit<ts.CompilerOptions, 'outDir'>;
} & { compilerOptions: Omit<ts.CompilerOptions, 'outDir'> };

Anyway, I thought having outDir next to compilerOptions in the tsconfig was wrong in the first place?


/**
* For backwards compatibility.
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"scripts": {
"test": "npm run prepare && node test",
"preversion": "npm test",
"prepare": "rm -rf dist && tsc"
"prepare": "shx rm -rf dist && tsc",
"format": "prettier --write \"**/*.{js,ts,tsx,md}\""
},
"author": "Simon Haenisch (https://github.com/simonhaenisch)",
"repository": "simonhaenisch/rollup-plugin-typescript-paths",
Expand All @@ -28,6 +29,7 @@
"prettier": "2.8.8",
"prettier-plugin-organize-imports": "3.2.2",
"rollup": "2.79.1",
"shx": "^0.3.4",
"typescript": "5.1.3"
},
"peerDependencies": {
Expand Down