-
Notifications
You must be signed in to change notification settings - Fork 13
#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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', | ||||||
|
@@ -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) { | ||||||
|
@@ -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 | ||||||
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; | ||||||
|
@@ -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( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
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 { | ||||||
|
@@ -131,10 +150,9 @@ export interface Options { | |||||
transform?(path: string): string; | ||||||
} | ||||||
|
||||||
interface TsConfig { | ||||||
compilerOptions: CompilerOptions; | ||||||
type TsConfig = { | ||||||
outDir: string; | ||||||
} | ||||||
} & Omit<ts.CompilerOptions, 'outDir'>; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it be like
Suggested change
Anyway, I thought having |
||||||
|
||||||
/** | ||||||
* For backwards compatibility. | ||||||
|
There was a problem hiding this comment.
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.