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

Commit 6fe51a9

Browse files
committed
feat: fixed module resolution; add lib option support
1 parent a8e867e commit 6fe51a9

10 files changed

+167
-254
lines changed

package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@
3131
},
3232
"homepage": "https://github.com/s-panferov/awesome-typescript-loader",
3333
"dependencies": {
34-
"babel-polyfill": "^6.1.19",
3534
"colors": "^1.1.2",
36-
"enhanced-resolve": "^2.2.2",
37-
"es6-promisify": "^4.1.0",
3835
"loader-utils": "^0.2.6",
3936
"lodash": "^4.13.1",
4037
"source-map-support": "^0.4.0"
4138
},
4239
"devDependencies": {
43-
"bluebird": "^3.3.3",
4440
"chai": "^3.5.0",
4541
"git-hooks": "^1.0.2",
4642
"grunt": "^1.0.1",
@@ -59,7 +55,7 @@
5955
"sinon": "^1.17.4",
6056
"temp": "^0.8.3",
6157
"tslint": "3.11.0-dev.0",
62-
"typescript": "^1.9.0-dev.20160618-1.0",
58+
"typescript": "^1.9.0-dev.20160619-1.0",
6359
"webpack": "2.1.0-beta.4"
6460
}
6561
}

src/cache.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ function filename(source: string, identifier, options) {
7676
return hash.read().toString('hex') + '.json.gzip';
7777
};
7878

79-
interface CacheParams {
79+
interface CacheParams<T> {
8080
source: string;
8181
options: any;
82-
transform: (source: string, options: any) => string;
82+
transform: () => T;
8383
identifier: any;
8484
directory: string;
8585
}
8686

8787
/**
8888
* Retrieve file from cache, or create a new one for future reads
8989
*/
90-
export function cache(params: CacheParams) {
90+
export function cache<T>(params: CacheParams<T>): T {
9191
// Spread params into named variables
9292
// Forgive user whenever possible
9393
let source = params.source;
@@ -106,7 +106,7 @@ export function cache(params: CacheParams) {
106106
} catch(e) {
107107
// Otherwise just transform the file
108108
// return it to the user asap and write it in cache
109-
let result = transform(source, options);
109+
let result = transform();
110110
write(file, result);
111111
return result;
112112
}

src/checker-runtime.ts

+8-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { ICompilerInfo, IFile } from './host';
22
import { LoaderPlugin, LoaderPluginDef, LoaderConfig } from './instance';
3-
import createSyncResolver from './resolver';
43
import * as path from 'path';
5-
import * as fs from 'fs';
64

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

@@ -21,6 +19,7 @@ export interface IInitPayload {
2119
compilerOptions: ts.CompilerOptions;
2220
compilerInfo: ICompilerInfo;
2321
webpackOptions: any;
22+
defaultLib: string;
2423
plugins: LoaderPluginDef[];
2524
}
2625

@@ -41,6 +40,7 @@ export interface IEnv {
4140
program?: ts.Program;
4241
service?: ts.LanguageService;
4342
plugins?: LoaderPluginDef[];
43+
defaultLib?: string;
4444
initedPlugins?: LoaderPlugin[];
4545
}
4646

@@ -73,7 +73,6 @@ export class Host implements ts.LanguageServiceHost {
7373

7474
constructor() {
7575
this.moduleResolutionHost = new ModuleResolutionHost(this);
76-
this.resolver = createSyncResolver(env.webpackOptions);
7776
}
7877

7978
normalizePath(filePath: string): string {
@@ -90,26 +89,10 @@ export class Host implements ts.LanguageServiceHost {
9089
}
9190
}
9291

93-
getScriptSnapshot(fileName) {
92+
getScriptSnapshot(fileName: string) {
9493
let fileName_ = path.normalize(fileName);
9594
let file = env.files[fileName_];
96-
97-
if (!file) {
98-
try {
99-
file = {
100-
version: 0,
101-
text: fs.readFileSync(fileName, { encoding: 'utf8' }).toString()
102-
};
103-
104-
if (path.basename(fileName) !== 'package.json') {
105-
env.files[fileName_] = file;
106-
}
107-
}
108-
catch (e) {
109-
return;
110-
}
111-
}
112-
95+
!file && console.log(fileName, file)
11396
return env.compiler.ScriptSnapshot.fromString(file.text);
11497
}
11598

@@ -131,10 +114,8 @@ export class Host implements ts.LanguageServiceHost {
131114
});
132115
}
133116

134-
getDefaultLibFileName(options) {
135-
return options.target === env.compiler.ScriptTarget.ES6 ?
136-
env.compilerInfo.lib6.fileName :
137-
env.compilerInfo.lib5.fileName;
117+
getDefaultLibFileName(options: ts.CompilerOptions) {
118+
return env.defaultLib;
138119
}
139120

140121
log(message) {
@@ -148,6 +129,7 @@ function processInit(payload: IInitPayload) {
148129
env.loaderConfig = payload.loaderConfig;
149130
env.compilerOptions = payload.compilerOptions;
150131
env.webpackOptions = payload.webpackOptions;
132+
env.defaultLib = payload.defaultLib;
151133
env.host = new Host();
152134
env.service = env.compiler.createLanguageService(env.host, env.compiler.createDocumentRegistry());
153135
env.plugins = payload.plugins;
@@ -175,6 +157,7 @@ function processCompile(payload: ICompilePayload) {
175157

176158
env.files = payload.files;
177159
env.resolutionCache = payload.resolutionCache;
160+
178161
let program = env.program = env.service.getProgram();
179162

180163
let allDiagnostics: ts.Diagnostic[] = [];

src/checker.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface ChildProcess extends childProcess.ChildProcess {
99
compilerInfo?: ICompilerInfo;
1010
loaderConfig?: LoaderConfig;
1111
compilerOptions?: ts.CompilerOptions;
12+
defaultLib?: string;
1213
webpackOptions?: any;
1314
plugins?: LoaderPluginDef[];
1415
}
@@ -18,6 +19,7 @@ export function createChecker(
1819
loaderConfig: LoaderConfig,
1920
compilerOptions: ts.CompilerOptions,
2021
webpackOptions: any,
22+
defaultLib: string,
2123
plugins: LoaderPluginDef[]
2224
): ChildProcess {
2325
let checker: ChildProcess = childProcess.fork(path.join(__dirname, 'checker-runtime.js'));
@@ -29,6 +31,7 @@ export function createChecker(
2931
loaderConfig,
3032
compilerOptions,
3133
webpackOptions,
34+
defaultLib,
3235
plugins
3336
}
3437
}, null);
@@ -55,6 +58,7 @@ export function resetChecker(checker: ChildProcess) {
5558
checker.loaderConfig,
5659
checker.compilerOptions,
5760
checker.webpackOptions,
61+
checker.defaultLib,
5862
checker.plugins
5963
);
6064
} else {

src/deps.ts

+21-72
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as _ from 'lodash';
2-
import * as path from 'path';
31
import { State } from './host';
42

53
let objectAssign = require('object-assign');
@@ -21,43 +19,6 @@ export interface IExternals {
2119

2220
export type Exclude = string[];
2321

24-
export function createIgnoringResolver(
25-
externals: IExternals,
26-
exclude: Exclude,
27-
resolver: SyncResolver,
28-
): SyncResolver {
29-
function resolve(base: string, dep: string): string {
30-
let inWebpackExternals = externals && externals.hasOwnProperty(dep);
31-
let inTypeScriptExclude = false;
32-
33-
if ((inWebpackExternals || inTypeScriptExclude)) {
34-
return '%%ignore';
35-
} else {
36-
let resultPath = resolver(base, dep);
37-
if (Array.isArray(resultPath)) {
38-
resultPath = resultPath[0];
39-
}
40-
41-
// ignore excluded javascript
42-
if (!resultPath.match(/.tsx?$/)) {
43-
let matchedExcludes = exclude.filter((excl) => {
44-
return resultPath.indexOf(excl) !== -1;
45-
});
46-
47-
if (matchedExcludes.length > 0) {
48-
return '%%ignore';
49-
} else {
50-
return resultPath;
51-
}
52-
} else {
53-
return resultPath;
54-
}
55-
}
56-
}
57-
58-
return resolve;
59-
}
60-
6122
export function isTypeDeclaration(fileName: string): boolean {
6223
return /\.d.ts$/.test(fileName);
6324
}
@@ -71,10 +32,6 @@ function isImportEqualsDeclaration(node: ts.Node) {
7132
return !!(<any>node).moduleReference && (<any>node).moduleReference.hasOwnProperty('expression');
7233
}
7334

74-
function isIgnoreDependency(absulutePath: string) {
75-
return absulutePath == '%%ignore';
76-
}
77-
7835
export class FileAnalyzer {
7936
dependencies = new DependencyManager();
8037
validFiles = new ValidFilesManager();
@@ -84,7 +41,7 @@ export class FileAnalyzer {
8441
this.state = state;
8542
}
8643

87-
checkDependencies(fileName: string): boolean {
44+
checkDependencies(fileName: string, isDefaultLib = false): boolean {
8845
let isValid = this.validFiles.isFileValid(fileName);
8946
if (isValid) {
9047
return isValid;
@@ -97,7 +54,8 @@ export class FileAnalyzer {
9754

9855
try {
9956
if (!this.state.hasFile(fileName)) {
100-
changed = this.state.readFileAndUpdate(fileName);
57+
this.state.readFileAndAdd(fileName, isDefaultLib);
58+
changed = true;
10159
}
10260
this.checkDependenciesInternal(fileName);
10361
} catch (err) {
@@ -146,6 +104,17 @@ export class FileAnalyzer {
146104
}
147105

148106
resolve(fileName: string, depName: string): ts.ResolvedModule {
107+
108+
if (/^[a-z0-9].*\.d\.ts$/.test(depName)) {
109+
// Make import relative
110+
// We need this to be able to resolve directives like
111+
//
112+
// <reference path="lib.d.ts" />
113+
//
114+
// with resolver.
115+
depName = './' + depName;
116+
}
117+
149118
let resolution = this.state.ts.resolveModuleName(
150119
depName,
151120
fileName,
@@ -159,38 +128,18 @@ export class FileAnalyzer {
159128
this.state.fileAnalyzer.dependencies.addResolution(fileName, depName, resolvedModule);
160129
}
161130

162-
console.log(
163-
fileName,'\n',
164-
depName, '\n',
165-
resolvedModule && resolvedModule.resolvedFileName, '\n',
166-
!resolvedModule && resolution.failedLookupLocations, '\n\n');
131+
if (this.state.compilerConfig.options.traceResolution) {
132+
console.log(
133+
'Resolve', depName, '\n',
134+
'from', fileName,'\n',
135+
'resolved', resolvedModule && resolvedModule.resolvedFileName, '\n',
136+
'failedLookupLocations', resolution.failedLookupLocations, '\n\n');
137+
}
167138

168139
return resolvedModule;
169140
}
170141
}
171142

172-
let builtInCache = {};
173-
174-
function checkIfModuleBuiltInCached(modPath: string): boolean {
175-
return !!builtInCache[modPath];
176-
}
177-
178-
function checkIfModuleBuiltIn(modPath: string): boolean {
179-
if (builtInCache[modPath]) {
180-
return true;
181-
}
182-
183-
try {
184-
if (require.resolve(modPath) === modPath) {
185-
builtInCache[modPath] = true;
186-
return true;
187-
}
188-
} catch (e) {
189-
}
190-
191-
return false;
192-
}
193-
194143
export interface IDependencyGraphItem {
195144
fileName: string;
196145
dependencies: IDependencyGraphItem[];

0 commit comments

Comments
 (0)