Skip to content

Commit

Permalink
Merge pull request #688 from christiantinauer/master
Browse files Browse the repository at this point in the history
Removed option "contextAsConfigBasePath" because the context given to…
  • Loading branch information
johnnyreilly committed Jan 20, 2018
2 parents 3d41633 + c6354c1 commit e8a4bb0
Show file tree
Hide file tree
Showing 24 changed files with 425 additions and 212 deletions.
15 changes: 8 additions & 7 deletions README.md
Expand Up @@ -443,25 +443,26 @@ loading only those files that are actually bundled by webpack, as well as any `.
by the `tsconfig.json` settings. `.d.ts` files are still included because they may be needed for
compilation without being explicitly imported, and therefore not picked up by webpack.

#### contextAsConfigBasePath *(boolean) (default=false)*
#### context *(string) (default=undefined)*

If true, will parse the TypeScript configuration file with
[webpack.context](https://webpack.js.org/configuration/entry-context/#context) as base path.
If set, will parse the TypeScript configuration file with given **absolute path** as base path.
Per default the directory of the configuration file is used as base path. Relative paths in the configuration
file are resolved with respect to the base path when parsed. Option `contextAsConfigBasePath` allows to set option
`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).
file are resolved with respect to the base path when parsed. Option `context` allows to set option
`configFile` to a path other than the project root (e.g. a NPM package), while the base path for `ts-loader`
can remain the project root.

Keep in mind that **not** having a `tsconfig.json` in your project root can cause different behaviour between `ts-loader` and `tsc`.
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
referenced in option `configFile`. For more information [please read the PR](https://github.com/TypeStrong/ts-loader/pull/681) that contributed this option.
referenced in option `configFile`. For more information please [read the PR](https://github.com/TypeStrong/ts-loader/pull/681) that
is the base and [read the PR](https://github.com/TypeStrong/ts-loader/pull/688) that contributed this option.

Webpack:

```javascript
{
loader: require.resolve('ts-loader'),
options: {
contextAsConfigBasePath: true,
context: __dirname,
configFile: require.resolve('ts-config-react-app')
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Expand Up @@ -111,7 +111,7 @@ function getLoaderOptions(loader: Webpack) {
}

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

/**
* Validate the supplied loader options.
Expand All @@ -131,6 +131,10 @@ ${ validLoaderOptions.join(' / ')}
`);
}
}

if (loaderOptions.context && !path.isAbsolute(loaderOptions.context)) {
throw new Error(`Option 'context' has to be an absolute path. Given '${loaderOptions.context}'.`);
}
}

function makeLoaderOptions(instanceName: string, configFileOptions: Partial<LoaderOptions>, loaderOptions: LoaderOptions) {
Expand All @@ -140,6 +144,7 @@ function makeLoaderOptions(instanceName: string, configFileOptions: Partial<Load
logInfoToStdOut: false,
compiler: 'typescript',
configFile: 'tsconfig.json',
context: undefined,
transpileOnly: false,
compilerOptions: {},
appendTsSuffixTo: [],
Expand Down
6 changes: 1 addition & 5 deletions src/instances.ts
Expand Up @@ -79,11 +79,7 @@ function successfulTypeScriptInstance(
}

const { configFilePath, configFile } = configFileAndPath;

const basePath = loaderOptions.contextAsConfigBasePath
? loader.context
: path.dirname(configFilePath || '');

const basePath = loaderOptions.context || path.dirname(configFilePath || '');
const configParseResult = getConfigParseResult(compiler, configFile, basePath);

if (configParseResult.errors.length > 0 && !loaderOptions.happyPackMode) {
Expand Down
3 changes: 1 addition & 2 deletions src/interfaces.ts
Expand Up @@ -278,8 +278,7 @@ export interface LoaderOptions {
instance: string;
compiler: string;
configFile: string;
/** DEPRECATED */
contextAsConfigBasePath: boolean;
context: string;
transpileOnly: boolean;
ignoreDiagnostics: number[];
reportFiles: string[];
Expand Down
Expand Up @@ -67,7 +67,7 @@
/* 0 */
/***/ (function(module, exports) {

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)");
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)");

/***/ })
/******/ ]);
Expand Up @@ -6,7 +6,7 @@ ERROR in ./.test/validateLoaderOptionNames/app.ts
Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption

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

at validateLoaderOptions (dist\index.js:92:19)
at getLoaderOptions (dist\index.js:75:5)
Expand Down
Expand Up @@ -67,7 +67,7 @@
/* 0 */
/***/ (function(module, exports) {

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)");
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)");

/***/ })
/******/ ]);
Expand Up @@ -6,7 +6,7 @@ ERROR in ./.test/validateLoaderOptionNames/app.ts
Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption

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

at validateLoaderOptions (dist\index.js:92:19)
at getLoaderOptions (dist\index.js:75:5)
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions test/execution-tests/option-context/package.json
@@ -0,0 +1,24 @@
{
"name": "option-context",
"license": "MIT",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {
"@types/jasmine": "^2.8.2",
"@types/react": "^16.0.27",
"@types/react-dom": "^16.0.3",
"@types/react-test-renderer": "^16.0.0",
"babel": "^6.23.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-react-app": "^3.1.0",
"jasmine-core": "^2.8.0",
"react-click-outside": "^3.0.0",
"react-test-renderer": "^16.2.0"
},
"dependencies": {
"babel-polyfill": "^6.26.0",
"react": "^16.2.0",
"react-dom": "^16.2.0"
}
}
1 change: 1 addition & 0 deletions test/execution-tests/option-context/src/app-shim.d.ts
@@ -0,0 +1 @@
declare module 'react-click-outside';
@@ -1,8 +1,14 @@
import React from 'react';
import enhanceWithClickOutside from 'react-click-outside';

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

const COApp = enhanceWithClickOutside(App)

export default App;
export {
COApp
}
@@ -0,0 +1,14 @@
import React from 'react';
import enhanceWithClickOutside from 'react-click-outside';

const DeepApp: React.SFC<{ name: string }> = ({ name }) =>
<div className="container-fluid">
<h1>Hello {name}!</h1>
</div>;

const CODeepApp = enhanceWithClickOutside(DeepApp)

export default DeepApp;
export {
CODeepApp
}
@@ -0,0 +1,14 @@
import React from 'react';
import enhanceWithClickOutside from 'react-click-outside';

const DeeperApp: React.SFC<{ name: string }> = ({ name }) =>
<div className="container-fluid">
<h1>Hello {name}!</h1>
</div>;

const CODeeperApp = enhanceWithClickOutside(DeeperApp)

export default DeeperApp;
export {
CODeeperApp
}
69 changes: 69 additions & 0 deletions test/execution-tests/option-context/test/components/App.tests.tsx
@@ -0,0 +1,69 @@
import React from 'react';
import { createRenderer } from 'react-test-renderer/shallow';

import App, { COApp } from '../../src/components/App';
import DeepApp, { CODeepApp } from '../../src/components/deep/DeepApp';
import DeeperApp, { CODeeperApp } from '../../src/components/deep/deeper/DeeperApp';

describe('App', () => {
it('simple', () => expect(1).toBe(1));

it('renders App as expected HTML', () => {
const shallowRenderer = createRenderer();

shallowRenderer.render(<App name="Christian" />);
const app = shallowRenderer.getRenderOutput();

expect(app).toEqual(
<div className="container-fluid">
<h1>Hello { "Christian" }!</h1>
</div>
);
});

it('renders ClickOutsideApp', () => {
const shallowRenderer = createRenderer();

shallowRenderer.render(<COApp name="Christian" />);
});


it('renders DeepApp as expected HTML', () => {
const shallowRenderer = createRenderer();

shallowRenderer.render(<DeepApp name="Christian" />);
const app = shallowRenderer.getRenderOutput();

expect(app).toEqual(
<div className="container-fluid">
<h1>Hello { "Christian" }!</h1>
</div>
);
});

it('renders ClickOutsideDeeApp', () => {
const shallowRenderer = createRenderer();

shallowRenderer.render(<CODeepApp name="Christian" />);
});


it('renders DeeperApp as expected HTML', () => {
const shallowRenderer = createRenderer();

shallowRenderer.render(<DeeperApp name="Christian" />);
const app = shallowRenderer.getRenderOutput();

expect(app).toEqual(
<div className="container-fluid">
<h1>Hello { "Christian" }!</h1>
</div>
);
});

it('renders ClickOutsideDeeperApp', () => {
const shallowRenderer = createRenderer();

shallowRenderer.render(<CODeeperApp name="Christian" />);
});
});
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es6",
"jsx": "preserve",
"moduleResolution": "node"
},
"files": [
"./src/app-shim.d.ts"
]
}
Expand Up @@ -4,19 +4,6 @@
var path = require('path');
var webpack = require('webpack');

var babelOptions = {
"presets": [
"react",
[
"es2015",
{
"modules": false
}
],
"es2016"
]
};

module.exports = {
entry: './src/main.tsx',
output: {
Expand All @@ -25,35 +12,40 @@ module.exports = {
module: {
rules: [{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: babelOptions
options: {
babelrc: false,
presets: [require.resolve('babel-preset-react-app')]
}
},
{
loader: 'ts-loader',
options: {
contextAsConfigBasePath: true,
context: __dirname,
onlyCompileBundledFiles: true,
configFile: require.resolve('./tsconfig-container/tsconfig.json')
}
}
]
}, {
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: babelOptions
options: {
babelrc: false,
presets: [require.resolve('babel-preset-react-app')]
}
}
]
}]
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ['.ts', '.tsx', '.js']
},
}
};

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

0 comments on commit e8a4bb0

Please sign in to comment.