-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypescript-simple-loader.ts
309 lines (256 loc) · 8.41 KB
/
typescript-simple-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { resolve, relative, dirname } from 'path'
import { readFileSync, statSync } from 'fs'
import * as ts from 'typescript'
import extend = require('xtend')
import { parseQuery, urlToRequest } from 'loader-utils'
import * as tsconfig from 'tsconfig'
import arrify = require('arrify')
interface WebPackLoader {
cacheable(flag?: boolean): void
query: string
resourcePath: string
context: string
sourceMap: boolean
loaderIndex: number
_compiler: any
addDependency(fileName: string): void
clearDependencies(): void
emitWarning(warning: string): void
emitError(error: string): void
callback(err: Error): void
callback(err: void, contents: string, sourceMap?: SourceMap): void
options: {
context: string
}
}
interface SourceMap {
sources: string[]
file: string
sourcesContent: string[]
}
interface Options {
compiler?: string
configFile?: string
ignoreWarnings?: (string | number)[]
}
type FilesMap = ts.Map<{ version: number, text: string }>
interface LoaderInstance {
files: FilesMap
service: ts.LanguageService
}
/**
* Hold a cache of loader instances.
*/
const loaderInstances: { [id: string]: LoaderInstance } = {}
/**
* Keep temporary references to the current webpack loader for dependencies.
*/
let currentLoader: WebPackLoader
/**
* Support TypeScript in Webpack.
*/
function loader (content: string): void {
const loader: WebPackLoader = this
const fileName = this.resourcePath
const { files, service } = getLoaderInstance(this)
let file = files[fileName]
this.cacheable()
// Set content on the first load. The watch task maintains reloads and
// the version doesn't need to change when every dependency is re-run.
if (!file) {
file = files[fileName] = { version: 0, text: '' }
}
file.text = content
file.version++
currentLoader = loader
const output = service.getEmitOutput(fileName)
currentLoader = undefined
if (output.emitSkipped) {
loader.callback(new Error(`Emitting ${fileName} failed`))
return
}
const result = output.outputFiles[loader.sourceMap ? 1 : 0].text
let sourceMap: SourceMap
if (loader.sourceMap) {
sourceMap = JSON.parse(output.outputFiles[0].text)
sourceMap.sources = [fileName]
sourceMap.file = fileName
sourceMap.sourcesContent = [content]
}
loader.callback(null, result, sourceMap)
}
/**
* Read the configuration into an object.
*/
function readConfig (filename: string, loader: WebPackLoader, TS: typeof ts) {
const config = filename ? tsconfig.readFileSync(filename) : {
files: [],
compilerOptions: {}
}
// Merge all possible compiler options together.
config.compilerOptions = extend({
target: 'es5',
module: 'commonjs'
}, config.compilerOptions, {
sourceMap: loader.sourceMap,
inlineSourceMap: false,
inlineSources: false,
declaration: false
})
return TS.parseConfigFile(config, TS.sys, filename)
}
/**
* Create a TypeScript language service from the first instance.
*/
function createInstance (loader: WebPackLoader, options: Options): LoaderInstance {
const context = loader.context
const rootFile = loader.resourcePath
const files: FilesMap = {}
const ignoreWarnings = arrify(options.ignoreWarnings).map(Number)
// Allow custom TypeScript compilers to be used.
const TS: typeof ts = require(options.compiler || 'typescript')
// Allow `configFile` option to override `tsconfig.json` lookup.
const configFile = options.configFile ?
resolve(context, options.configFile) :
tsconfig.resolveSync(context)
const config = readConfig(configFile, loader, TS)
// Emit configuration errors.
config.errors.forEach((error: ts.Diagnostic) => {
loader.emitError(formatDiagnostic(error, TS))
})
const serviceHost: ts.LanguageServiceHost = {
getScriptFileNames (): string[] {
// Return an array of all file names. We can't return just the default
// files because webpack may have traversed through a regular JS file
// back to a TypeScript file and if we don't have that file in the array,
// TypeScript will give us a file not found compilation error.
return config.fileNames.concat(Object.keys(files))
},
getScriptVersion (fileName) {
return files[fileName] && files[fileName].version.toString()
},
getScriptSnapshot (fileName: string): ts.IScriptSnapshot {
const exists = fileExists(fileName)
let file = files[fileName]
// Load all files from the filesystem when they don't exist yet. This
// is required for definition files and nested type information.
if (exists) {
if (!file) {
try {
file = files[fileName] = {
version: 0,
text: readFileSync(fileName, 'utf-8')
}
} catch (e) {
return
}
}
// Always add dependencies. This dependency could be a `.d.ts` file or
// an external module that failed to compile the first time.
if (currentLoader) {
currentLoader.addDependency(fileName)
}
return TS.ScriptSnapshot.fromString(file.text)
}
delete files[fileName]
},
getCurrentDirectory: () => context,
getCompilationSettings: () => config.options,
getDefaultLibFileName: (options: ts.CompilerOptions) => {
return TS.getDefaultLibFilePath(config.options)
}
}
const service = TS.createLanguageService(serviceHost, TS.createDocumentRegistry())
// Hook into the watch plugin to update file dependencies in TypeScript
// before the files are reloaded. This is required because we need type
// information to propagate upward and Webpack reloads from the top down.
loader._compiler.plugin('watch-run', function (watching: any, cb: () => void) {
const mtimes = watching.compiler.watchFileSystem.watcher.mtimes
Object.keys(mtimes)
.forEach((fileName) => {
const file = files[fileName]
// Reload when a definition changes.
if (file && isDefinition(fileName)) {
file.text = readFileSync(fileName, 'utf8')
file.version++
}
})
cb()
})
// Push all semantic and outstanding compilation errors on emit. This allows
// us to notify of all errors, including files outside webpacks knowledge.
loader._compiler.plugin('emit', function (compilation: any, cb: () => void) {
const program = service.getProgram()
program.getGlobalDiagnostics()
.concat(program.getSemanticDiagnostics())
.forEach((diagnostic) => {
if (ignoreWarnings.indexOf(diagnostic.code) === -1) {
compilation.warnings.push(new DiagnosticError(diagnostic, context, TS))
}
})
program.getSyntacticDiagnostics()
.forEach((diagnostic) => {
compilation.errors.push(new DiagnosticError(diagnostic, context, TS))
})
cb()
})
return { service, files }
}
/**
* Check a file exists in the file system.
*/
function fileExists (fileName: string): boolean {
try {
return statSync(fileName).isFile()
} catch (e) {
return false
}
}
/**
* Format a diagnostic object into a string.
*/
function formatDiagnostic (diagnostic: ts.Diagnostic, TS: typeof ts): string {
const message = TS.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
if (diagnostic.file) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start)
return `(${line + 1},${character + 1}): ${message} (${diagnostic.code})`
}
return `${message} (${diagnostic.code})`
}
/**
* Create a Webpack-compatible diagnostic error.
*/
class DiagnosticError implements Error {
name = 'DiagnosticError'
message: string
file: string
constructor (diagnostic: ts.Diagnostic, context: string, TS: typeof ts) {
this.message = formatDiagnostic(diagnostic, TS)
if (diagnostic.file) {
this.file = urlToRequest(relative(context, diagnostic.file.fileName))
}
}
}
/**
* Get the current TypeScript instance for the loader.
*
* @param {WebPackLoader} loader
* @return {LoaderInstance}
*/
function getLoaderInstance (loader: WebPackLoader): LoaderInstance {
const id = loader.options.context + loader.sourceMap + loader.query
const query = parseQuery(loader.query)
if (loaderInstances[id]) {
return loaderInstances[id]
}
const instance = createInstance(loader, query)
loaderInstances[id] = instance
return instance
}
/**
* Check if a file is a defintion.
*/
function isDefinition (fileName: string): boolean {
return /\.d\.ts$/.test(fileName)
}
export = loader