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

Commit a8e867e

Browse files
committed
feat: no more async, simplify module resolution
1 parent 97e0b9c commit a8e867e

15 files changed

+1542
-1504
lines changed

.babelrc

-4
This file was deleted.

package.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
"name": "awesome-typescript-loader",
33
"version": "1.1.1",
44
"description": "Awesome TS loader for webpack",
5-
"main": "dist.babel/entry.js",
5+
"main": "dist/entry.js",
66
"scripts": {
77
"prepublish": "npm run test && grunt",
88
"pretest": "npm run build",
99
"test": "mocha dist.babel/test",
1010
"watch": "npm run watch:ts && npm run watch:babel",
1111
"watch:ts": "npm run build:ts -- --watch --diagnostics",
12-
"watch:babel": "npm run build:babel -- --watch",
1312
"prebuild": "npm run lint",
1413
"build": "npm run build:ts && npm run build:babel",
1514
"build:ts": "tsc -p src --pretty",
16-
"build:babel": "babel dist -d dist.babel",
1715
"lint": "tslint src/*.ts"
1816
},
1917
"author": "Stanislav Panferov <[email protected]> (http://panferov.me/)",
@@ -42,11 +40,6 @@
4240
"source-map-support": "^0.4.0"
4341
},
4442
"devDependencies": {
45-
"babel-cli": "^6.3.17",
46-
"babel-core": "^6.7.4",
47-
"babel-preset-es2015": "^6.1.2",
48-
"babel-preset-es2015-node4": "^2.1.0",
49-
"babel-preset-stage-2": "^6.1.2",
5043
"bluebird": "^3.3.3",
5144
"chai": "^3.5.0",
5245
"git-hooks": "^1.0.2",

src/cache.ts

+19-52
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,24 @@ export function findCompiledModule(fileName: string): CompiledModule {
3636

3737
/**
3838
* Read the contents from the compressed file.
39-
*
40-
* @async
4139
*/
42-
function read(filename: string, callback) {
43-
return fs.readFile(filename, function(err, data) {
44-
if (err) { return callback(err); }
45-
46-
return zlib.gunzip(data, function(err, content) {
47-
let result = {};
48-
49-
if (err) { return callback(err); }
50-
51-
try {
52-
result = JSON.parse(content);
53-
} catch (e) {
54-
return callback(e);
55-
}
56-
57-
return callback(null, result);
58-
});
59-
});
60-
};
40+
function read(filename: string) {
41+
let content = fs.readFileSync(filename);
42+
let jsonString = zlib.gunzipSync(content);
43+
return JSON.parse(jsonString);
44+
}
6145

6246
/**
6347
* Write contents into a compressed file.
6448
*
65-
* @async
6649
* @params {String} filename
6750
* @params {String} result
68-
* @params {Function} callback
6951
*/
70-
function write(filename: string, result: any, callback) {
71-
let content = JSON.stringify(result);
72-
73-
return zlib.gzip(content as any, function(err, data) {
74-
if (err) { return callback(err); }
75-
76-
return fs.writeFile(filename, data, callback);
77-
});
78-
};
52+
function write(filename: string, result: any) {
53+
let jsonString = JSON.stringify(result);
54+
let content = zlib.gzipSync(jsonString as any);
55+
return fs.writeFileSync(filename, content);
56+
}
7957

8058
/**
8159
* Build the filename for the cached file
@@ -108,11 +86,8 @@ interface CacheParams {
10886

10987
/**
11088
* Retrieve file from cache, or create a new one for future reads
111-
*
112-
* @async
113-
* @example
11489
*/
115-
export function cache(params: CacheParams, callback) {
90+
export function cache(params: CacheParams) {
11691
// Spread params into named variables
11792
// Forgive user whenever possible
11893
let source = params.source;
@@ -124,23 +99,15 @@ export function cache(params: CacheParams, callback) {
12499
os.tmpdir();
125100
let file = path.join(directory, filename(source, identifier, options));
126101

127-
return read(file, function(err, content) {
128-
let result = {};
102+
try {
129103
// No errors mean that the file was previously cached
130104
// we just need to return it
131-
if (!err) { return callback(null, content); }
132-
105+
return read(file);
106+
} catch(e) {
133107
// Otherwise just transform the file
134108
// return it to the user asap and write it in cache
135-
try {
136-
result = transform(source, options);
137-
} catch (error) {
138-
return callback(error);
139-
}
140-
141-
return write(file, result, function(err) {
142-
return callback(err, result);
143-
});
144-
145-
});
146-
};
109+
let result = transform(source, options);
110+
write(file, result);
111+
return result;
112+
}
113+
}

src/checker-runtime.ts

+5-48
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { ICompilerInfo, IFile } from './host';
22
import { LoaderPlugin, LoaderPluginDef, LoaderConfig } from './instance';
3-
import makeResolver from './resolver';
3+
import createSyncResolver from './resolver';
44
import * as path from 'path';
55
import * as fs from 'fs';
66

77
let colors = require('colors/safe');
88

9-
require('babel-polyfill');
10-
119
export enum MessageType {
1210
Init = <any>'init',
1311
Compile = <any>'compile'
@@ -75,7 +73,7 @@ export class Host implements ts.LanguageServiceHost {
7573

7674
constructor() {
7775
this.moduleResolutionHost = new ModuleResolutionHost(this);
78-
this.resolver = makeResolver(env.webpackOptions);
76+
this.resolver = createSyncResolver(env.webpackOptions);
7977
}
8078

8179
normalizePath(filePath: string): string {
@@ -128,50 +126,9 @@ export class Host implements ts.LanguageServiceHost {
128126
}
129127

130128
resolveModuleNames(moduleNames: string[], containingFile: string) {
131-
let resolvedModules: ts.ResolvedModule[] = [];
132-
133-
for (let moduleName of moduleNames) {
134-
let cached = env.resolutionCache[`${containingFile}::${moduleName}`];
135-
if (cached) {
136-
resolvedModules.push(cached);
137-
} else {
138-
let resolvedFileName: string;
139-
let resolvedModule: ts.ResolvedModule;
140-
141-
try {
142-
resolvedFileName = this.resolver(
143-
this.normalizePath(path.dirname(containingFile)),
144-
moduleName
145-
);
146-
147-
if (!resolvedFileName.match(/\.tsx?$/)) {
148-
resolvedFileName = null;
149-
}
150-
}
151-
catch (e) {
152-
resolvedFileName = null;
153-
}
154-
155-
let tsResolved = env.compiler.resolveModuleName(
156-
resolvedFileName || moduleName,
157-
containingFile,
158-
env.compilerOptions,
159-
this.moduleResolutionHost
160-
);
161-
162-
if (tsResolved.resolvedModule) {
163-
resolvedModule = tsResolved.resolvedModule;
164-
} else {
165-
resolvedModule = {
166-
resolvedFileName: resolvedFileName || ''
167-
};
168-
}
169-
170-
resolvedModules.push(resolvedModule);
171-
}
172-
}
173-
174-
return resolvedModules;
129+
return moduleNames.map(moduleName => {
130+
return env.resolutionCache[`${containingFile}::${moduleName}`];
131+
});
175132
}
176133

177134
getDefaultLibFileName(options) {

src/defines.d.ts

-37
Original file line numberDiff line numberDiff line change
@@ -1,37 +0,0 @@
1-
declare module "pinkie-promise" {
2-
let promise: typeof Promise;
3-
export = promise;
4-
}
5-
6-
declare module "es6-promisify" {
7-
let promise: (foo) => Promise<any>;
8-
export = promise;
9-
}
10-
11-
declare module 'tsconfig' {
12-
export interface CompilerOptions {
13-
[key: string]: any;
14-
}
15-
export interface TSConfig {
16-
compilerOptions?: CompilerOptions;
17-
files?: string[];
18-
exclude?: string[];
19-
filesGlob?: string[];
20-
[key: string]: any;
21-
}
22-
export interface Options {
23-
compilerOptions?: CompilerOptions;
24-
filterDefinitions?: boolean;
25-
resolvePaths?: boolean;
26-
}
27-
export function resolve(dir: string): Promise<string>;
28-
export function resolveSync(dir: string): string;
29-
export function load(dir: string, options?: Options): Promise<TSConfig>;
30-
export function loadSync(dir: string, options?: Options): TSConfig;
31-
export function readFile(filename: string, options?: Options): Promise<{}>;
32-
export function readFileSync(filename: string, options?: Options): TSConfig;
33-
export function parseFile(contents: string, filename: string, options?: Options): Promise<TSConfig>;
34-
export function parseFileSync(contents: string, filename: string, options?: Options): TSConfig;
35-
export function resolveConfig(data: TSConfig, filename: string, options?: Options): Promise<TSConfig>;
36-
export function resolveConfigSync(data: TSConfig, filename: string, options?: Options): TSConfig;
37-
}

0 commit comments

Comments
 (0)