66var fs = require ( 'fs' ) ;
77var util = require ( 'util' ) ;
88var path = require ( 'path' ) ;
9- var ts = require ( 'typescript' ) ;
109var objectAssign = require ( 'object-assign' ) ;
1110var Promise = require ( 'bluebird' ) ;
1211
@@ -19,21 +18,20 @@ function prepareStaticSource(moduleId) {
1918var RUNTIME = prepareStaticSource ( './webpack-runtime.d.ts' ) ;
2019var LIB = prepareStaticSource ( 'typescript/bin/lib.d.ts' ) ;
2120
22- var DEFAULT_OPTIONS = {
23- target : ts . ScriptTarget . ES5 ,
24- module : ts . ModuleKind . CommonJS ,
25- sourceMap : true ,
26- verbose : false
27- } ;
28-
29- function TypeScriptWebpackHost ( options , fs ) {
21+ function TypeScriptWebpackHost ( options , fs , ts ) {
22+ ts = ts || require ( 'typescript' ) ;
23+ this . ts = ts ;
3024 this . options = { } ;
31- objectAssign ( this . options , DEFAULT_OPTIONS ) ;
25+ objectAssign ( this . options , {
26+ target : this . ts . ScriptTarget . ES5 ,
27+ module : this . ts . ModuleKind . CommonJS ,
28+ sourceMap : true ,
29+ verbose : false
30+ } ) ;
3231 objectAssign ( this . options , options ) ;
33-
3432 this . _fs = fs ;
3533 this . _files = { } ;
36- this . _services = ts . createLanguageService ( this , ts . createDocumentRegistry ( ) ) ;
34+ this . _services = this . ts . createLanguageService ( this , this . ts . createDocumentRegistry ( ) ) ;
3735 this . _runtimeRead = null ;
3836
3937 this . _addFile ( RUNTIME . filename , RUNTIME . text ) ;
@@ -118,19 +116,18 @@ TypeScriptWebpackHost.prototype.log = function log(message) {
118116TypeScriptWebpackHost . prototype . _findImportDeclarations = function _findImportDeclarations ( filename ) {
119117 var node = this . _services . getSourceFile ( filename ) ;
120118 var result = [ ] ;
121- visit ( node ) ;
122- return result ;
123-
124- function visit ( node ) {
125- if ( node . kind === ts . SyntaxKind . ImportDeclaration ) {
119+ var visit = function ( node ) {
120+ if ( node . kind === this . ts . SyntaxKind . ImportDeclaration ) {
126121 result . push ( node . moduleReference . expression . text ) ;
127- } else if ( node . kind === ts . SyntaxKind . SourceFile ) {
122+ } else if ( node . kind === this . ts . SyntaxKind . SourceFile ) {
128123 result = result . concat ( node . referencedFiles . map ( function ( f ) {
129124 return path . resolve ( path . dirname ( node . filename ) , f . filename ) ;
130125 } ) ) ;
131126 }
132- ts . forEachChild ( node , visit ) ;
133- }
127+ this . ts . forEachChild ( node , visit ) ;
128+ } . bind ( this ) ;
129+ visit ( node ) ;
130+ return result ;
134131} ;
135132
136133TypeScriptWebpackHost . prototype . _addFile = function _addFile ( filename , text ) {
@@ -187,7 +184,7 @@ TypeScriptWebpackHost.prototype.emit = function emit(resolver, filename, text) {
187184
188185 return this . _addDependencies ( resolver , filename ) . then ( function ( ) {
189186 var output = this . _services . getEmitOutput ( filename ) ;
190- if ( output . emitOutputStatus === ts . EmitReturnStatus . Succeeded ) {
187+ if ( output . emitOutputStatus === this . ts . EmitReturnStatus . Succeeded ) {
191188 return output ;
192189 } else {
193190 var diagnostics = this . _services
0 commit comments