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

+8-7
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

+6-1
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

+1-5
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

+1-2
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "option-context",
3+
"license": "MIT",
4+
"version": "1.0.0",
5+
"main": "index.js",
6+
"devDependencies": {
7+
"@types/jasmine": "^2.8.2",
8+
"@types/react": "^16.0.27",
9+
"@types/react-dom": "^16.0.3",
10+
"@types/react-test-renderer": "^16.0.0",
11+
"babel": "^6.23.0",
12+
"babel-core": "^6.26.0",
13+
"babel-loader": "^7.1.2",
14+
"babel-preset-react-app": "^3.1.0",
15+
"jasmine-core": "^2.8.0",
16+
"react-click-outside": "^3.0.0",
17+
"react-test-renderer": "^16.2.0"
18+
},
19+
"dependencies": {
20+
"babel-polyfill": "^6.26.0",
21+
"react": "^16.2.0",
22+
"react-dom": "^16.2.0"
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'react-click-outside';
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import React from 'react';
2+
import enhanceWithClickOutside from 'react-click-outside';
23

34
const App: React.SFC<{ name: string }> = ({ name }) =>
45
<div className="container-fluid">
56
<h1>Hello {name}!</h1>
67
</div>;
78

9+
const COApp = enhanceWithClickOutside(App)
10+
811
export default App;
12+
export {
13+
COApp
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import enhanceWithClickOutside from 'react-click-outside';
3+
4+
const DeepApp: React.SFC<{ name: string }> = ({ name }) =>
5+
<div className="container-fluid">
6+
<h1>Hello {name}!</h1>
7+
</div>;
8+
9+
const CODeepApp = enhanceWithClickOutside(DeepApp)
10+
11+
export default DeepApp;
12+
export {
13+
CODeepApp
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import enhanceWithClickOutside from 'react-click-outside';
3+
4+
const DeeperApp: React.SFC<{ name: string }> = ({ name }) =>
5+
<div className="container-fluid">
6+
<h1>Hello {name}!</h1>
7+
</div>;
8+
9+
const CODeeperApp = enhanceWithClickOutside(DeeperApp)
10+
11+
export default DeeperApp;
12+
export {
13+
CODeeperApp
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React from 'react';
2+
import { createRenderer } from 'react-test-renderer/shallow';
3+
4+
import App, { COApp } from '../../src/components/App';
5+
import DeepApp, { CODeepApp } from '../../src/components/deep/DeepApp';
6+
import DeeperApp, { CODeeperApp } from '../../src/components/deep/deeper/DeeperApp';
7+
8+
describe('App', () => {
9+
it('simple', () => expect(1).toBe(1));
10+
11+
it('renders App as expected HTML', () => {
12+
const shallowRenderer = createRenderer();
13+
14+
shallowRenderer.render(<App name="Christian" />);
15+
const app = shallowRenderer.getRenderOutput();
16+
17+
expect(app).toEqual(
18+
<div className="container-fluid">
19+
<h1>Hello { "Christian" }!</h1>
20+
</div>
21+
);
22+
});
23+
24+
it('renders ClickOutsideApp', () => {
25+
const shallowRenderer = createRenderer();
26+
27+
shallowRenderer.render(<COApp name="Christian" />);
28+
});
29+
30+
31+
it('renders DeepApp as expected HTML', () => {
32+
const shallowRenderer = createRenderer();
33+
34+
shallowRenderer.render(<DeepApp name="Christian" />);
35+
const app = shallowRenderer.getRenderOutput();
36+
37+
expect(app).toEqual(
38+
<div className="container-fluid">
39+
<h1>Hello { "Christian" }!</h1>
40+
</div>
41+
);
42+
});
43+
44+
it('renders ClickOutsideDeeApp', () => {
45+
const shallowRenderer = createRenderer();
46+
47+
shallowRenderer.render(<CODeepApp name="Christian" />);
48+
});
49+
50+
51+
it('renders DeeperApp as expected HTML', () => {
52+
const shallowRenderer = createRenderer();
53+
54+
shallowRenderer.render(<DeeperApp name="Christian" />);
55+
const app = shallowRenderer.getRenderOutput();
56+
57+
expect(app).toEqual(
58+
<div className="container-fluid">
59+
<h1>Hello { "Christian" }!</h1>
60+
</div>
61+
);
62+
});
63+
64+
it('renders ClickOutsideDeeperApp', () => {
65+
const shallowRenderer = createRenderer();
66+
67+
shallowRenderer.render(<CODeeperApp name="Christian" />);
68+
});
69+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"jsx": "preserve",
5+
"moduleResolution": "node"
6+
},
7+
"files": [
8+
"./src/app-shim.d.ts"
9+
]
10+
}

test/execution-tests/option-contextAsConfigBasePath/webpack.config.js renamed to test/execution-tests/option-context/webpack.config.js

+11-19
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@
44
var path = require('path');
55
var webpack = require('webpack');
66

7-
var babelOptions = {
8-
"presets": [
9-
"react",
10-
[
11-
"es2015",
12-
{
13-
"modules": false
14-
}
15-
],
16-
"es2016"
17-
]
18-
};
19-
207
module.exports = {
218
entry: './src/main.tsx',
229
output: {
@@ -25,35 +12,40 @@ module.exports = {
2512
module: {
2613
rules: [{
2714
test: /\.ts(x?)$/,
28-
exclude: /node_modules/,
2915
use: [
3016
{
3117
loader: 'babel-loader',
32-
options: babelOptions
18+
options: {
19+
babelrc: false,
20+
presets: [require.resolve('babel-preset-react-app')]
21+
}
3322
},
3423
{
3524
loader: 'ts-loader',
3625
options: {
37-
contextAsConfigBasePath: true,
26+
context: __dirname,
27+
onlyCompileBundledFiles: true,
3828
configFile: require.resolve('./tsconfig-container/tsconfig.json')
3929
}
4030
}
4131
]
4232
}, {
4333
test: /\.js$/,
44-
exclude: /node_modules/,
4534
use: [
4635
{
4736
loader: 'babel-loader',
48-
options: babelOptions
37+
options: {
38+
babelrc: false,
39+
presets: [require.resolve('babel-preset-react-app')]
40+
}
4941
}
5042
]
5143
}]
5244
},
5345
resolve: {
5446
// Add `.ts` and `.tsx` as a resolvable extension.
5547
extensions: ['.ts', '.tsx', '.js']
56-
},
48+
}
5749
};
5850

5951
// for test harness purposes only, you would not need this in a normal project

0 commit comments

Comments
 (0)