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

Commit

Permalink
fix: allow to pass context to PathPlugin, basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
s-panferov committed Feb 6, 2017
1 parent c8628c5 commit 207b164
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
32 changes: 32 additions & 0 deletions src/__test__/paths-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
src, webpackConfig, tsconfig,
compile, expectErrors, spec
} from './utils';

import { PathPlugin } from '../paths-plugin';

spec(__filename, async function() {
src('index.ts', `
import App from 'ui/app'
const app = new App()
app.render();
`);

src('./ui/app/index.ts', `
export default class App { render() { return 'App' } }
`);

tsconfig({
baseUrl: '.',
paths: {
"ui/*": [ "./src/ui/*" ]
}
});

const config = webpackConfig();
(config.resolve as any).plugins = [ new PathPlugin() ];
let stats = await compile(config);

expectErrors(stats, 0);
});
12 changes: 8 additions & 4 deletions src/__test__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function webpackConfig(...enchance: any[]): webpack.Configuration {
filename: '[name].js'
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
extensions: ['.ts', '.tsx', '.js', '.jsx']
},
module: {
loaders: [
Expand Down Expand Up @@ -236,10 +236,14 @@ export function expectErrors(stats: any, count: number, errors: string[] = []) {
expect(stats.compilation.errors.length).eq(count);
}

export function tsconfig(compilerOptions?: any, config?: any) {
export function tsconfig(
compilerOptions?: any,
config?: any
) {
const res = _.merge({
compilerOptions: _.merge({
target: 'es6'
target: 'es6',
moduleResolution: 'node'
}, compilerOptions)
}, config);
return file('tsconfig.json', json(res));
Expand Down Expand Up @@ -271,7 +275,7 @@ export function touchFile(fileName: string): Promise<any> {
.then(source => writeFile(fileName, source));
}

export function compile(config?): Promise<any> {
export function compile(config): Promise<any> {
return new Promise((resolve, reject) => {
const compiler = webpack(config);

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path';
import { findCompiledModule, cache } from './cache';
import * as helpers from './helpers';
import { QueryOptions, Loader, ensureInstance, Instance, getRootCompiler } from './instance';
import { PathsPlugin } from './paths-plugin';
import { PathPlugin } from './paths-plugin';
import { CheckerPlugin as _CheckerPlugin } from './watch-mode';

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

namespace loader {
export const TsConfigPathsPlugin = PathsPlugin;
export const TsConfigPathsPlugin = PathPlugin;
export const CheckerPlugin = _CheckerPlugin;
}

Expand Down
9 changes: 7 additions & 2 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@ export function ensureInstance(
}

const watching = isWatching(rootCompiler);
const context = process.cwd();

const context = rootCompiler.context;
let compilerInfo = setupTs(query.compiler);
let { tsImpl } = compilerInfo;

let { configFilePath, compilerConfig, loaderConfig } = readConfigFile(context, query, options, tsImpl);
let { configFilePath, compilerConfig, loaderConfig } = readConfigFile(
context,
query,
options,
tsImpl
);

applyDefaults(
configFilePath,
Expand Down
12 changes: 9 additions & 3 deletions src/paths-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

export class PathsPlugin implements ResolverPlugin {
export interface PathPluginOptions {
context?: string;
}

export class PathPlugin implements ResolverPlugin {
source: string;
target: string;
ts: typeof ts;
Expand All @@ -61,13 +65,15 @@ export class PathsPlugin implements ResolverPlugin {
mappings: Mapping[];
absoluteBaseUrl: string;

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

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

let { configFilePath, compilerConfig } = readConfigFile(process.cwd(), config, {}, this.ts);
let context = config.context || process.cwd();
let { configFilePath, compilerConfig } = readConfigFile(context, config, {}, this.ts);

this.options = compilerConfig.options;
this.configFilePath = configFilePath;

Expand Down

0 comments on commit 207b164

Please sign in to comment.