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

Commit

Permalink
feat: no more async, simplify module resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
s-panferov committed Jun 19, 2016
1 parent 97e0b9c commit a8e867e
Show file tree
Hide file tree
Showing 15 changed files with 1,542 additions and 1,504 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

9 changes: 1 addition & 8 deletions package.json
Expand Up @@ -2,18 +2,16 @@
"name": "awesome-typescript-loader",
"version": "1.1.1",
"description": "Awesome TS loader for webpack",
"main": "dist.babel/entry.js",
"main": "dist/entry.js",
"scripts": {
"prepublish": "npm run test && grunt",
"pretest": "npm run build",
"test": "mocha dist.babel/test",
"watch": "npm run watch:ts && npm run watch:babel",
"watch:ts": "npm run build:ts -- --watch --diagnostics",
"watch:babel": "npm run build:babel -- --watch",
"prebuild": "npm run lint",
"build": "npm run build:ts && npm run build:babel",
"build:ts": "tsc -p src --pretty",
"build:babel": "babel dist -d dist.babel",
"lint": "tslint src/*.ts"
},
"author": "Stanislav Panferov <fnight.m@gmail.com> (http://panferov.me/)",
Expand Down Expand Up @@ -42,11 +40,6 @@
"source-map-support": "^0.4.0"
},
"devDependencies": {
"babel-cli": "^6.3.17",
"babel-core": "^6.7.4",
"babel-preset-es2015": "^6.1.2",
"babel-preset-es2015-node4": "^2.1.0",
"babel-preset-stage-2": "^6.1.2",
"bluebird": "^3.3.3",
"chai": "^3.5.0",
"git-hooks": "^1.0.2",
Expand Down
71 changes: 19 additions & 52 deletions src/cache.ts
Expand Up @@ -36,46 +36,24 @@ export function findCompiledModule(fileName: string): CompiledModule {

/**
* Read the contents from the compressed file.
*
* @async
*/
function read(filename: string, callback) {
return fs.readFile(filename, function(err, data) {
if (err) { return callback(err); }

return zlib.gunzip(data, function(err, content) {
let result = {};

if (err) { return callback(err); }

try {
result = JSON.parse(content);
} catch (e) {
return callback(e);
}

return callback(null, result);
});
});
};
function read(filename: string) {
let content = fs.readFileSync(filename);
let jsonString = zlib.gunzipSync(content);
return JSON.parse(jsonString);
}

/**
* Write contents into a compressed file.
*
* @async
* @params {String} filename
* @params {String} result
* @params {Function} callback
*/
function write(filename: string, result: any, callback) {
let content = JSON.stringify(result);

return zlib.gzip(content as any, function(err, data) {
if (err) { return callback(err); }

return fs.writeFile(filename, data, callback);
});
};
function write(filename: string, result: any) {
let jsonString = JSON.stringify(result);
let content = zlib.gzipSync(jsonString as any);
return fs.writeFileSync(filename, content);
}

/**
* Build the filename for the cached file
Expand Down Expand Up @@ -108,11 +86,8 @@ interface CacheParams {

/**
* Retrieve file from cache, or create a new one for future reads
*
* @async
* @example
*/
export function cache(params: CacheParams, callback) {
export function cache(params: CacheParams) {
// Spread params into named variables
// Forgive user whenever possible
let source = params.source;
Expand All @@ -124,23 +99,15 @@ export function cache(params: CacheParams, callback) {
os.tmpdir();
let file = path.join(directory, filename(source, identifier, options));

return read(file, function(err, content) {
let result = {};
try {
// No errors mean that the file was previously cached
// we just need to return it
if (!err) { return callback(null, content); }

return read(file);
} catch(e) {
// Otherwise just transform the file
// return it to the user asap and write it in cache
try {
result = transform(source, options);
} catch (error) {
return callback(error);
}

return write(file, result, function(err) {
return callback(err, result);
});

});
};
let result = transform(source, options);
write(file, result);
return result;
}
}
53 changes: 5 additions & 48 deletions src/checker-runtime.ts
@@ -1,13 +1,11 @@
import { ICompilerInfo, IFile } from './host';
import { LoaderPlugin, LoaderPluginDef, LoaderConfig } from './instance';
import makeResolver from './resolver';
import createSyncResolver from './resolver';
import * as path from 'path';
import * as fs from 'fs';

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

require('babel-polyfill');

export enum MessageType {
Init = <any>'init',
Compile = <any>'compile'
Expand Down Expand Up @@ -75,7 +73,7 @@ export class Host implements ts.LanguageServiceHost {

constructor() {
this.moduleResolutionHost = new ModuleResolutionHost(this);
this.resolver = makeResolver(env.webpackOptions);
this.resolver = createSyncResolver(env.webpackOptions);
}

normalizePath(filePath: string): string {
Expand Down Expand Up @@ -128,50 +126,9 @@ export class Host implements ts.LanguageServiceHost {
}

resolveModuleNames(moduleNames: string[], containingFile: string) {
let resolvedModules: ts.ResolvedModule[] = [];

for (let moduleName of moduleNames) {
let cached = env.resolutionCache[`${containingFile}::${moduleName}`];
if (cached) {
resolvedModules.push(cached);
} else {
let resolvedFileName: string;
let resolvedModule: ts.ResolvedModule;

try {
resolvedFileName = this.resolver(
this.normalizePath(path.dirname(containingFile)),
moduleName
);

if (!resolvedFileName.match(/\.tsx?$/)) {
resolvedFileName = null;
}
}
catch (e) {
resolvedFileName = null;
}

let tsResolved = env.compiler.resolveModuleName(
resolvedFileName || moduleName,
containingFile,
env.compilerOptions,
this.moduleResolutionHost
);

if (tsResolved.resolvedModule) {
resolvedModule = tsResolved.resolvedModule;
} else {
resolvedModule = {
resolvedFileName: resolvedFileName || ''
};
}

resolvedModules.push(resolvedModule);
}
}

return resolvedModules;
return moduleNames.map(moduleName => {
return env.resolutionCache[`${containingFile}::${moduleName}`];
});
}

getDefaultLibFileName(options) {
Expand Down
37 changes: 0 additions & 37 deletions src/defines.d.ts
@@ -1,37 +0,0 @@
declare module "pinkie-promise" {
let promise: typeof Promise;
export = promise;
}

declare module "es6-promisify" {
let promise: (foo) => Promise<any>;
export = promise;
}

declare module 'tsconfig' {
export interface CompilerOptions {
[key: string]: any;
}
export interface TSConfig {
compilerOptions?: CompilerOptions;
files?: string[];
exclude?: string[];
filesGlob?: string[];
[key: string]: any;
}
export interface Options {
compilerOptions?: CompilerOptions;
filterDefinitions?: boolean;
resolvePaths?: boolean;
}
export function resolve(dir: string): Promise<string>;
export function resolveSync(dir: string): string;
export function load(dir: string, options?: Options): Promise<TSConfig>;
export function loadSync(dir: string, options?: Options): TSConfig;
export function readFile(filename: string, options?: Options): Promise<{}>;
export function readFileSync(filename: string, options?: Options): TSConfig;
export function parseFile(contents: string, filename: string, options?: Options): Promise<TSConfig>;
export function parseFileSync(contents: string, filename: string, options?: Options): TSConfig;
export function resolveConfig(data: TSConfig, filename: string, options?: Options): Promise<TSConfig>;
export function resolveConfigSync(data: TSConfig, filename: string, options?: Options): TSConfig;
}

0 comments on commit a8e867e

Please sign in to comment.