Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 207b164

Browse files
committed
fix: allow to pass context to PathPlugin, basic tests
1 parent c8628c5 commit 207b164

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

src/__test__/paths-plugin.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
src, webpackConfig, tsconfig,
3+
compile, expectErrors, spec
4+
} from './utils';
5+
6+
import { PathPlugin } from '../paths-plugin';
7+
8+
spec(__filename, async function() {
9+
src('index.ts', `
10+
import App from 'ui/app'
11+
12+
const app = new App()
13+
app.render();
14+
`);
15+
16+
src('./ui/app/index.ts', `
17+
export default class App { render() { return 'App' } }
18+
`);
19+
20+
tsconfig({
21+
baseUrl: '.',
22+
paths: {
23+
"ui/*": [ "./src/ui/*" ]
24+
}
25+
});
26+
27+
const config = webpackConfig();
28+
(config.resolve as any).plugins = [ new PathPlugin() ];
29+
let stats = await compile(config);
30+
31+
expectErrors(stats, 0);
32+
});

src/__test__/utils.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function webpackConfig(...enchance: any[]): webpack.Configuration {
6565
filename: '[name].js'
6666
},
6767
resolve: {
68-
extensions: ['.ts', '.tsx', '.js', '.jsx'],
68+
extensions: ['.ts', '.tsx', '.js', '.jsx']
6969
},
7070
module: {
7171
loaders: [
@@ -236,10 +236,14 @@ export function expectErrors(stats: any, count: number, errors: string[] = []) {
236236
expect(stats.compilation.errors.length).eq(count);
237237
}
238238

239-
export function tsconfig(compilerOptions?: any, config?: any) {
239+
export function tsconfig(
240+
compilerOptions?: any,
241+
config?: any
242+
) {
240243
const res = _.merge({
241244
compilerOptions: _.merge({
242-
target: 'es6'
245+
target: 'es6',
246+
moduleResolution: 'node'
243247
}, compilerOptions)
244248
}, config);
245249
return file('tsconfig.json', json(res));
@@ -271,7 +275,7 @@ export function touchFile(fileName: string): Promise<any> {
271275
.then(source => writeFile(fileName, source));
272276
}
273277

274-
export function compile(config?): Promise<any> {
278+
export function compile(config): Promise<any> {
275279
return new Promise((resolve, reject) => {
276280
const compiler = webpack(config);
277281

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from 'path';
44
import { findCompiledModule, cache } from './cache';
55
import * as helpers from './helpers';
66
import { QueryOptions, Loader, ensureInstance, Instance, getRootCompiler } from './instance';
7-
import { PathsPlugin } from './paths-plugin';
7+
import { PathPlugin } from './paths-plugin';
88
import { CheckerPlugin as _CheckerPlugin } from './watch-mode';
99

1010
const loaderUtils = require('loader-utils');
@@ -19,7 +19,7 @@ function loader(text) {
1919
}
2020

2121
namespace loader {
22-
export const TsConfigPathsPlugin = PathsPlugin;
22+
export const TsConfigPathsPlugin = PathPlugin;
2323
export const CheckerPlugin = _CheckerPlugin;
2424
}
2525

src/instance.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,17 @@ export function ensureInstance(
8585
}
8686

8787
const watching = isWatching(rootCompiler);
88+
const context = process.cwd();
8889

89-
const context = rootCompiler.context;
9090
let compilerInfo = setupTs(query.compiler);
9191
let { tsImpl } = compilerInfo;
9292

93-
let { configFilePath, compilerConfig, loaderConfig } = readConfigFile(context, query, options, tsImpl);
93+
let { configFilePath, compilerConfig, loaderConfig } = readConfigFile(
94+
context,
95+
query,
96+
options,
97+
tsImpl
98+
);
9499

95100
applyDefaults(
96101
configFilePath,

src/paths-plugin.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ function escapeRegExp(str) {
5050
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
5151
}
5252

53-
export class PathsPlugin implements ResolverPlugin {
53+
export interface PathPluginOptions {
54+
context?: string;
55+
}
56+
57+
export class PathPlugin implements ResolverPlugin {
5458
source: string;
5559
target: string;
5660
ts: typeof ts;
@@ -61,13 +65,15 @@ export class PathsPlugin implements ResolverPlugin {
6165
mappings: Mapping[];
6266
absoluteBaseUrl: string;
6367

64-
constructor(config: LoaderConfig & ts.CompilerOptions = {} as any) {
68+
constructor(config: LoaderConfig & ts.CompilerOptions & PathPluginOptions = {} as any) {
6569
this.source = 'described-resolve';
6670
this.target = 'resolve';
6771

6872
this.ts = setupTs(config.compiler).tsImpl;
6973

70-
let { configFilePath, compilerConfig } = readConfigFile(process.cwd(), config, {}, this.ts);
74+
let context = config.context || process.cwd();
75+
let { configFilePath, compilerConfig } = readConfigFile(context, config, {}, this.ts);
76+
7177
this.options = compilerConfig.options;
7278
this.configFilePath = configFilePath;
7379

0 commit comments

Comments
 (0)