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

Commit

Permalink
feat(*): remove bluebird, format filename for sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
s-panferov committed Jan 31, 2016
1 parent 5190488 commit 1dacfa8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"homepage": "https://github.com/s-panferov/awesome-typescript-loader",
"dependencies": {
"babel-polyfill": "^6.1.19",
"bluebird": "^2.10.2",
"colors": "^1.1.2",
"enhanced-resolve": "^0.9.1",
"es6-promisify": "^3.0.0",
"loader-utils": "^0.2.6",
"lodash": "^3.10.1",
"object-assign": "^2.1.1",
Expand Down Expand Up @@ -62,7 +62,7 @@
"ps-node": "0.0.5",
"rimraf": "^2.5.0",
"tslint": "^3.3.0-dev.1",
"typescript": "^1.8.0-dev.20160125",
"typescript": "^1.8.0",
"webpack": "^1.12.12",
"webpack-dev-server": "^1.14.1"
}
Expand Down
5 changes: 5 additions & 0 deletions src/defines.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ declare module "pinkie-promise" {
export = promise;
}

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

declare module 'tsconfig' {
export interface CompilerOptions {
[key: string]: any;
Expand Down
11 changes: 6 additions & 5 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as _ from 'lodash';
import * as path from 'path';
import * as Promise from 'bluebird';
import * as promisify from 'es6-promisify';

import { State } from './host';

Expand All @@ -22,7 +22,7 @@ export interface IExternals {
}

export function createResolver(externals: IExternals, webpackResolver: any): IResolver {
let resolver: IResolver = Promise.promisify(webpackResolver) as any;
let resolver: IResolver = promisify(webpackResolver) as any;

function resolve(base: string, dep: string): Promise<string> {
if (externals && externals.hasOwnProperty(dep)) {
Expand Down Expand Up @@ -139,11 +139,12 @@ export class FileAnalyzer {
});

let resolvedImports = await Promise.all(task);

return resolvedImports.filter(Boolean);
}

resolve(resolver: IResolver, fileName: string, defPath: string): Promise<string> {
let result;
let result: Promise<string>;

if (/^[a-z0-9].*\.d\.ts$/.test(defPath)) {
// Make import relative
Expand All @@ -156,7 +157,7 @@ export class FileAnalyzer {
result = Promise.resolve(path.resolve(path.dirname(fileName), defPath));
} else {
result = resolver(path.dirname(fileName), defPath)
.error(function (error) {
.catch(function (error) {
// Node builtin modules
try {
if (require.resolve(defPath) == defPath) {
Expand All @@ -171,7 +172,7 @@ export class FileAnalyzer {
}

return result
.error(function (error) {
.catch(function (error) {
let detailedError: any = new ResolutionError();
detailedError.message = error.message + "\n Required in " + fileName;
detailedError.cause = error;
Expand Down
4 changes: 2 additions & 2 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ts from "typescript";
import * as fs from 'fs';
import * as util from 'util';
import * as path from 'path';
import * as Promise from 'bluebird';
import * as promisify from 'es6-promisify';

import { FileAnalyzer } from './deps';
import { loadLib } from './helpers';
Expand Down Expand Up @@ -207,7 +207,7 @@ export class State {
this.compilerInfo = compilerInfo;
this.resolver = resolver;
this.fs = fsImpl;
this.readFileImpl = Promise.promisify(this.fs.readFile.bind(this.fs)) as any;
this.readFileImpl = promisify(this.fs.readFile.bind(this.fs)) as any;
this.host = new Host(this);
this.services = this.ts.createLanguageService(this.host, this.ts.createDocumentRegistry());
this.fileAnalyzer = new FileAnalyzer(this);
Expand Down
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require('babel-polyfill');

import * as Promise from 'bluebird';
import * as promisify from 'es6-promisify';
import * as _ from 'lodash';
import * as path from 'path';

Expand All @@ -15,10 +15,15 @@ import * as helpers from './helpers';
import { IWebPack, ensureInstance } from './instance';

let loaderUtils = require('loader-utils');
let cachePromise = Promise.promisify(cache);
let cachePromise: any = promisify(cache);

function loader(text) {
compiler.call(undefined, this, text);
async function loader(text) {
try {
await compiler.call(undefined, this, text);
} catch(e) {
console.error(e, e.stack);
throw e;
}
}

let externalsInvocation: Promise<void>;
Expand Down Expand Up @@ -110,10 +115,11 @@ async function compiler(webpack: IWebPack, text: string): Promise<void> {

resultText = result.text;

let sourceFileName = fileName.replace(process.cwd() + '/', '');
if (result.sourceMap) {
resultSourceMap = JSON.parse(result.sourceMap);
resultSourceMap.sources = [ loaderUtils.getRemainingRequest(webpack) ];
resultSourceMap.file = fileName;
resultSourceMap.sources = [ sourceFileName ];
resultSourceMap.file = sourceFileName;
resultSourceMap.sourcesContent = [ text ];

resultText = resultText.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
Expand Down

0 comments on commit 1dacfa8

Please sign in to comment.