Skip to content

Commit e8a4bb0

Browse files
authored
Merge pull request #688 from christiantinauer/master
Removed option "contextAsConfigBasePath" because the context given to…
2 parents 3d41633 + c6354c1 commit e8a4bb0

File tree

24 files changed

+425
-212
lines changed

24 files changed

+425
-212
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,25 +443,26 @@ loading only those files that are actually bundled by webpack, as well as any `.
443443
by the `tsconfig.json` settings. `.d.ts` files are still included because they may be needed for
444444
compilation without being explicitly imported, and therefore not picked up by webpack.
445445
446-
#### contextAsConfigBasePath *(boolean) (default=false)*
446+
#### context *(string) (default=undefined)*
447447
448-
If true, will parse the TypeScript configuration file with
449-
[webpack.context](https://webpack.js.org/configuration/entry-context/#context) as base path.
448+
If set, will parse the TypeScript configuration file with given **absolute path** as base path.
450449
Per default the directory of the configuration file is used as base path. Relative paths in the configuration
451-
file are resolved with respect to the base path when parsed. Option `contextAsConfigBasePath` allows to set option
452-
`configFile` to a path other than the project root (e.g. a NPM package) and the base path for `ts-loader` is [webpack.context](https://webpack.js.org/configuration/entry-context/#context) (which is most of the time the project root).
450+
file are resolved with respect to the base path when parsed. Option `context` allows to set option
451+
`configFile` to a path other than the project root (e.g. a NPM package), while the base path for `ts-loader`
452+
can remain the project root.
453453
454454
Keep in mind that **not** having a `tsconfig.json` in your project root can cause different behaviour between `ts-loader` and `tsc`.
455455
When using editors like `VS Code` it is advised to add a `tsconfig.json` file to the root of the project and extend the config file
456-
referenced in option `configFile`. For more information [please read the PR](https://github.com/TypeStrong/ts-loader/pull/681) that contributed this option.
456+
referenced in option `configFile`. For more information please [read the PR](https://github.com/TypeStrong/ts-loader/pull/681) that
457+
is the base and [read the PR](https://github.com/TypeStrong/ts-loader/pull/688) that contributed this option.
457458
458459
Webpack:
459460
460461
```javascript
461462
{
462463
loader: require.resolve('ts-loader'),
463464
options: {
464-
contextAsConfigBasePath: true,
465+
context: __dirname,
465466
configFile: require.resolve('ts-config-react-app')
466467
}
467468
}

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function getLoaderOptions(loader: Webpack) {
111111
}
112112

113113
type ValidLoaderOptions = keyof LoaderOptions;
114-
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'contextAsConfigBasePath', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'onlyCompileBundledFiles', 'happyPackMode', 'getCustomTransformers', 'reportFiles', 'experimentalWatchApi'];
114+
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'context', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'onlyCompileBundledFiles', 'happyPackMode', 'getCustomTransformers', 'reportFiles', 'experimentalWatchApi'];
115115

116116
/**
117117
* Validate the supplied loader options.
@@ -131,6 +131,10 @@ ${ validLoaderOptions.join(' / ')}
131131
`);
132132
}
133133
}
134+
135+
if (loaderOptions.context && !path.isAbsolute(loaderOptions.context)) {
136+
throw new Error(`Option 'context' has to be an absolute path. Given '${loaderOptions.context}'.`);
137+
}
134138
}
135139

136140
function makeLoaderOptions(instanceName: string, configFileOptions: Partial<LoaderOptions>, loaderOptions: LoaderOptions) {
@@ -140,6 +144,7 @@ function makeLoaderOptions(instanceName: string, configFileOptions: Partial<Load
140144
logInfoToStdOut: false,
141145
compiler: 'typescript',
142146
configFile: 'tsconfig.json',
147+
context: undefined,
143148
transpileOnly: false,
144149
compilerOptions: {},
145150
appendTsSuffixTo: [],

src/instances.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ function successfulTypeScriptInstance(
7979
}
8080

8181
const { configFilePath, configFile } = configFileAndPath;
82-
83-
const basePath = loaderOptions.contextAsConfigBasePath
84-
? loader.context
85-
: path.dirname(configFilePath || '');
86-
82+
const basePath = loaderOptions.context || path.dirname(configFilePath || '');
8783
const configParseResult = getConfigParseResult(compiler, configFile, basePath);
8884

8985
if (configParseResult.errors.length > 0 && !loaderOptions.happyPackMode) {

src/interfaces.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ export interface LoaderOptions {
278278
instance: string;
279279
compiler: string;
280280
configFile: string;
281-
/** DEPRECATED */
282-
contextAsConfigBasePath: boolean;
281+
context: string;
283282
transpileOnly: boolean;
284283
ignoreDiagnostics: number[];
285284
reportFiles: string[];

test/comparison-tests/validateLoaderOptionNames/expectedOutput-2.5/bundle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
/* 0 */
6868
/***/ (function(module, exports) {
6969

70-
throw new Error("Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption\n\nPlease take a look at the options you are supplying; the following are valid options:\nsilent / logLevel / logInfoToStdOut / instance / compiler / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers\n\n at validateLoaderOptions (C:/source/ts-loader/dist/index.js:92:19)\n at getLoaderOptions (C:/source/ts-loader/dist/index.js:75:5)\n at Object.loader (C:/source/ts-loader/dist/index.js:23:19)");
70+
throw new Error("Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption\n\nPlease take a look at the options you are supplying; the following are valid options:\nsilent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers\n\n at validateLoaderOptions (C:/source/ts-loader/dist/index.js:92:19)\n at getLoaderOptions (C:/source/ts-loader/dist/index.js:75:5)\n at Object.loader (C:/source/ts-loader/dist/index.js:23:19)");
7171

7272
/***/ })
7373
/******/ ]);

test/comparison-tests/validateLoaderOptionNames/expectedOutput-2.5/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ERROR in ./.test/validateLoaderOptionNames/app.ts
66
Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption
77

88
Please take a look at the options you are supplying; the following are valid options:
9-
silent / logLevel / logInfoToStdOut / instance / compiler / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers
9+
silent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers
1010

1111
at validateLoaderOptions (dist\index.js:92:19)
1212
at getLoaderOptions (dist\index.js:75:5)

test/comparison-tests/validateLoaderOptionNames/expectedOutput-2.6/bundle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
/* 0 */
6868
/***/ (function(module, exports) {
6969

70-
throw new Error("Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption\n\nPlease take a look at the options you are supplying; the following are valid options:\nsilent / logLevel / logInfoToStdOut / instance / compiler / contextAsConfigBasePath / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi\n\n at validateLoaderOptions (C:/source/ts-loader/dist/index.js:92:19)\n at getLoaderOptions (C:/source/ts-loader/dist/index.js:75:5)\n at Object.loader (C:/source/ts-loader/dist/index.js:23:19)");
70+
throw new Error("Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption\n\nPlease take a look at the options you are supplying; the following are valid options:\nsilent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers\n\n at validateLoaderOptions (C:/source/ts-loader/dist/index.js:92:19)\n at getLoaderOptions (C:/source/ts-loader/dist/index.js:75:5)\n at Object.loader (C:/source/ts-loader/dist/index.js:23:19)");
7171

7272
/***/ })
7373
/******/ ]);

test/comparison-tests/validateLoaderOptionNames/expectedOutput-2.6/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ERROR in ./.test/validateLoaderOptionNames/app.ts
66
Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption
77

88
Please take a look at the options you are supplying; the following are valid options:
9-
silent / logLevel / logInfoToStdOut / instance / compiler / contextAsConfigBasePath / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi
9+
silent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers
1010

1111
at validateLoaderOptions (dist\index.js:92:19)
1212
at getLoaderOptions (dist\index.js:75:5)

0 commit comments

Comments
 (0)