Skip to content
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

Add configFile option #607

Merged
merged 1 commit into from
Aug 26, 2017
Merged
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
59 changes: 42 additions & 17 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function getConfigFile(
log: logger.Logger,
compilerDetailsLogMessage: string
) {
const configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath), loaderOptions.configFileName);
const configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath), loaderOptions.configFile);
let configFileError: WebpackError | undefined;
let configFile: ConfigFile;

Expand Down Expand Up @@ -69,26 +69,51 @@ export function getConfigFile(
};
}


/**
* The tsconfig.json is found using the same method as `tsc`, starting in the current directory
* and continuing up the parent directory chain.
* Find a tsconfig file by name or by path.
* By name, the tsconfig.json is found using the same method as `tsc`, starting in the current
* directory and continuing up the parent directory chain.
* By path, the file will be found by resolving the given path relative to the requesting entry file.
*
* @param compiler The TypeScript compiler instance
* @param requestDirPath The directory in which the entry point requesting the tsconfig.json lies
* @param configFile The tsconfig file name to look for or a path to that file
* @return The absolute path to the tsconfig file, undefined if none was found.
*/
function findConfigFile(compiler: typeof typescript, searchPath: string, configFileName: string): string | undefined {
// Remove path.root from configFileName to fix windows specific issue. #604
configFileName = configFileName.substr(path.parse(configFileName).root.length)

while (true) {
const fileName = path.join(searchPath, configFileName);
if (compiler.sys.fileExists(fileName)) {
return fileName;
}
const parentPath = path.dirname(searchPath);
if (parentPath === searchPath) {
break;
function findConfigFile(compiler: typeof typescript, requestDirPath: string, configFile: string): string | undefined {
// If `configFile` is an absolute path, return it right away
if (path.isAbsolute(configFile)) {
return compiler.sys.fileExists(configFile)
? configFile
: undefined;
}

// If `configFile` is a relative path, resolve it.
// We define a relative path as: starts with
// one or two dots + a common directory delimiter
if (configFile.match(/^\.\.?(\/|\\)/)) {
const resolvedPath = path.resolve(configFile);
return compiler.sys.fileExists(resolvedPath)
? resolvedPath
: undefined;

// If `configFile` is a file name, find it in the directory tree
} else {
while (true) {
const fileName = path.join(requestDirPath, configFile);
if (compiler.sys.fileExists(fileName)) {
return fileName;
}
const parentPath = path.dirname(requestDirPath);
if (parentPath === requestDirPath) {
break;
}
requestDirPath = parentPath;
}
searchPath = parentPath;

return undefined;
}
return undefined;
}

export function getConfigParseResult(
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function getLoaderOptions(loader: Webpack) {
logLevel: 'INFO',
logInfoToStdOut: false,
compiler: 'typescript',
configFileName: 'tsconfig.json',
configFile: 'tsconfig.json',
transpileOnly: false,
visualStudioErrorFormat: false,
compilerOptions: {},
Expand All @@ -115,6 +115,15 @@ function getLoaderOptions(loader: Webpack) {
happyPackMode: false,
}, configFileOptions, queryOptions);

// Use deprecated `configFileName` as fallback for `configFile`
if (queryOptions.configFileName) {
if (queryOptions.configFile) {
throw new Error('ts-loader options `configFile` and `configFileName` are mutually exclusive');
} else {
options.configFile = queryOptions.configFileName;
}
}

options.ignoreDiagnostics = arrify(options.ignoreDiagnostics).map(Number);
options.logLevel = options.logLevel.toUpperCase();
options.instance = instanceName;
Expand Down
5 changes: 5 additions & 0 deletions src/instances.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as typescript from 'typescript';
import * as path from 'path';
import * as fs from 'fs';
import { yellow } from 'chalk';

import { makeAfterCompile } from './after-compile';
import { getConfigFile, getConfigParseResult } from './config';
Expand Down Expand Up @@ -38,6 +39,10 @@ export function getTypeScriptInstance(
const log = logger.makeLogger(loaderOptions);
const compiler = getCompiler(loaderOptions, log);

if (loaderOptions.configFileName) {
log.logWarning(yellow('Usage of ts-loader option `configFileName` is deprecated. Use `configFile` instead.'));
}

if (compiler.errorMessage !== undefined) {
return { error: makeError({ rawMessage: compiler.errorMessage }) };
}
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ export interface LoaderOptions {
logInfoToStdOut: boolean;
instance: string;
compiler: string;
configFileName: string;
configFile: string;
configFileName: string; // deprecated, remove in next major release
transpileOnly: boolean;
ignoreDiagnostics: number[];
visualStudioErrorFormat: boolean;
Expand Down