This repository was archived by the owner on Dec 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathindex.ts
182 lines (152 loc) · 4.84 KB
/
index.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
import * as _ from 'lodash'
import * as path from 'path'
import { findCompiledModule, cache } from './cache'
import * as helpers from './helpers'
import { QueryOptions, Loader, ensureInstance, Instance, getRootCompiler } from './instance'
import { PathPlugin } from './paths-plugin'
import { CheckerPlugin as _CheckerPlugin } from './watch-mode'
const loaderUtils = require('loader-utils')
function loader(text) {
try {
compiler.call(undefined, this, text)
} catch (e) {
console.error(e, e.stack)
throw e
}
}
namespace loader {
export const TsConfigPathsPlugin = PathPlugin
export const CheckerPlugin = _CheckerPlugin
}
interface Transformation {
text: string
map: any
deps: string[]
fresh?: boolean
}
const DECLARATION = /\.d.ts$/i
function compiler(loader: Loader, text: string): void {
if (loader.cacheable) {
loader.cacheable()
}
const rootCompiler = getRootCompiler(loader._compiler)
const query = <QueryOptions>(loaderUtils.getOptions(loader) || {})
const options = (loader.options && loader.options.ts) || {}
const instanceName = query.instance || 'at-loader'
const instance = ensureInstance(loader, query, options, instanceName, rootCompiler)
const callback = loader.async()
let fileName = helpers.toUnix(loader.resourcePath)
instance.compiledFiles[fileName] = true
if (DECLARATION.test(fileName)) {
loader.emitWarning(
new Error(`[${instanceName}] TypeScript declaration files should never be required`)
)
return callback(null, '')
}
let compiledModule
if (instance.loaderConfig.usePrecompiledFiles) {
compiledModule = findCompiledModule(fileName)
}
let transformation: Promise<{ cached: boolean; result: Transformation }> = null
if (compiledModule) {
transformation = Promise.resolve({
deps: [],
text: compiledModule.text,
map: compiledModule.map ? JSON.parse(compiledModule.map) : null
}).then(result => ({ cached: true, result }))
} else {
const transformationFunction = () => transform(loader, instance, fileName, text)
if (instance.loaderConfig.useCache) {
transformation = cache<Transformation>({
source: text,
identifier: instance.cacheIdentifier,
directory: instance.loaderConfig.cacheDirectory,
options: loader.query,
transform: transformationFunction
})
} else {
transformation = transformationFunction().then(result => ({ cached: false, result }))
}
}
transformation
.then(async ({ cached, result }) => {
const isolated =
instance.loaderConfig.forceIsolatedModules ||
instance.compilerConfig.options.isolatedModules
if (!isolated && result.deps) {
// If our modules are isolated we don"t need to recompile all the deps
result.deps.forEach(dep => loader.addDependency(path.normalize(dep)))
}
if (cached) {
// Update file in checker in case we read it from the cache
const updated = await instance.checker.updateFile(fileName, text)
if (updated) {
if (typeof loader._module.meta.tsLoaderFileVersion === 'number') {
loader._module.meta.tsLoaderFileVersion++
} else {
loader._module.meta.tsLoaderFileVersion = 0
}
}
}
return result
})
.then(({ text, map }) => {
callback(null, text, map)
})
.catch(callback)
.catch(e => {
console.error('Error in bail mode:', e, e.stack.join ? e.stack.join('\n') : e.stack)
process.exit(1)
})
}
function transform(
webpack: Loader,
instance: Instance,
fileName: string,
text: string
): Promise<Transformation> {
let resultText
let resultSourceMap = null
return instance.checker.emitFile(fileName, text).then(({ emitResult, deps }) => {
resultSourceMap = emitResult.sourceMap
resultText = emitResult.text
let sourceFileName = fileName.replace(instance.context + '/', '')
if (resultSourceMap) {
resultSourceMap = JSON.parse(resultSourceMap)
resultSourceMap.sources = [sourceFileName]
resultSourceMap.file = sourceFileName
resultSourceMap.sourcesContent = [text]
resultText = resultText.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '')
}
if (instance.loaderConfig.useBabel) {
let defaultOptions = {
inputSourceMap: resultSourceMap,
sourceRoot: instance.context,
filename: fileName,
sourceMap: true
}
let babelOptions = _.assign({}, defaultOptions, instance.loaderConfig.babelOptions)
let babelResult = instance.babelImpl.transform(resultText, babelOptions)
resultText = babelResult.code
resultSourceMap = babelResult.map
}
if (resultSourceMap) {
let sourcePath = path.relative(
instance.compilerConfig.options.sourceRoot || instance.context,
loaderUtils.getRemainingRequest(webpack)
)
resultSourceMap.sources = [sourcePath]
resultSourceMap.file = fileName
resultSourceMap.sourcesContent = [text]
}
if (emitResult.declaration) {
instance.compiledDeclarations.push(emitResult.declaration)
}
return {
text: resultText,
map: resultSourceMap,
deps
}
})
}
export = loader