forked from TypeStrong/ts-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
278 lines (229 loc) · 9.62 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
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
import * as path from 'path';
import * as loaderUtils from 'loader-utils';
import { getTypeScriptInstance } from './instances';
import { appendSuffixesIfMatch, arrify, formatErrors, hasOwnProperty, registerWebpackErrors } from './utils';
import * as constants from './constants';
import {
AsyncCallback,
Compiler,
LoaderOptions,
LoaderOptionsCache,
TSFile,
TSInstance,
Webpack,
LogLevel
} from './interfaces';
const webpackInstances: Compiler[] = [];
const loaderOptionsCache: LoaderOptionsCache = {};
type PartialLoaderOptions = Partial<LoaderOptions>;
/**
* The entry point for ts-loader
*/
function loader(this: Webpack, contents: string) {
this.cacheable && this.cacheable();
const callback = this.async();
const options = getLoaderOptions(this);
const instanceOrError = getTypeScriptInstance(options, this);
if (instanceOrError.error !== undefined) {
callback(instanceOrError.error);
return;
}
return successLoader(this, contents, callback, options, instanceOrError.instance!);
}
function successLoader(
loader: Webpack,
contents: string,
callback: AsyncCallback,
options: LoaderOptions,
instance: TSInstance
) {
const rawFilePath = path.normalize(loader.resourcePath);
const filePath = options.appendTsSuffixTo.length > 0 || options.appendTsxSuffixTo.length > 0
? appendSuffixesIfMatch({
'.ts': options.appendTsSuffixTo,
'.tsx': options.appendTsxSuffixTo,
}, rawFilePath)
: rawFilePath;
const fileVersion = updateFileInCache(filePath, contents, instance);
const { outputText, sourceMapText } = options.transpileOnly
? getTranspilationEmit(filePath, contents, instance, loader)
: getEmit(rawFilePath, filePath, instance, loader);
if (outputText === null || outputText === undefined) {
const additionalGuidance = filePath.indexOf('node_modules') !== -1
? '\nYou should not need to recompile .ts files in node_modules.\nPlease contact the package author to advise them to use --declaration --outDir.\nMore https://github.com/Microsoft/TypeScript/issues/12358'
: '';
throw new Error(`Typescript emitted no output for ${filePath}.${additionalGuidance}`);
}
const { sourceMap, output } = makeSourceMap(sourceMapText, outputText, filePath, contents, loader);
// _module.meta is not available inside happypack
if (!options.happyPackMode) {
// Make sure webpack is aware that even though the emitted JavaScript may be the same as
// a previously cached version the TypeScript may be different and therefore should be
// treated as new
loader._module.meta.tsLoaderFileVersion = fileVersion;
}
callback(null, output, sourceMap);
}
/**
* either retrieves loader options from the cache
* or creates them, adds them to the cache and returns
*/
function getLoaderOptions(loader: Webpack) {
// differentiate the TypeScript instance based on the webpack instance
let webpackIndex = webpackInstances.indexOf(loader._compiler);
if (webpackIndex === -1) {
webpackIndex = webpackInstances.push(loader._compiler) - 1;
}
const loaderOptions = loaderUtils.getOptions<LoaderOptions>(loader) || {} as LoaderOptions;
const configFileOptions: PartialLoaderOptions = loader.options.ts || {};
const instanceName = webpackIndex + '_' + (loaderOptions.instance || configFileOptions.instance || 'default');
if (hasOwnProperty(loaderOptionsCache, instanceName)) {
return loaderOptionsCache[instanceName];
}
validateLoaderOptions(loaderOptions);
const options = makeLoaderOptions(instanceName, configFileOptions, loaderOptions);
loaderOptionsCache[instanceName] = options;
return options;
}
type ValidLoaderOptions = keyof LoaderOptions;
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'onlyCompileBundledFiles', 'happyPackMode', 'getCustomTransformers'];
/**
* Validate the supplied loader options.
* At present this validates the option names only; in future we may look at validating the values too
* @param loaderOptions
*/
function validateLoaderOptions(loaderOptions: LoaderOptions) {
const loaderOptionKeys = Object.keys(loaderOptions);
for (let i = 0; i < loaderOptionKeys.length; i++) {
const option = loaderOptionKeys[i];
const isUnexpectedOption = (validLoaderOptions as string[]).indexOf(option) === -1;
if (isUnexpectedOption) {
throw new Error(`ts-loader was supplied with an unexpected loader option: ${option}
Please take a look at the options you are supplying; the following are valid options:
${ validLoaderOptions.join(' / ')}
`);
}
}
}
function makeLoaderOptions(instanceName: string, configFileOptions: Partial<LoaderOptions>, loaderOptions: LoaderOptions) {
const options = Object.assign({}, {
silent: false,
logLevel: 'WARN',
logInfoToStdOut: false,
compiler: 'typescript',
configFile: 'tsconfig.json',
transpileOnly: false,
compilerOptions: {},
appendTsSuffixTo: [],
appendTsxSuffixTo: [],
transformers: {},
entryFileCannotBeJs: false,
happyPackMode: false,
colors: true,
onlyCompileBundledFiles: false
} as Partial<LoaderOptions>, configFileOptions, loaderOptions);
options.ignoreDiagnostics = arrify(options.ignoreDiagnostics).map(Number);
options.logLevel = options.logLevel.toUpperCase() as LogLevel;
options.instance = instanceName;
// happypack can be used only together with transpileOnly mode
options.transpileOnly = options.happyPackMode ? true : options.transpileOnly;
return options;
}
/**
* Either add file to the overall files cache or update it in the cache when the file contents have changed
* Also add the file to the modified files
*/
function updateFileInCache(filePath: string, contents: string, instance: TSInstance) {
// Update file contents
let file = instance.files[filePath];
if (file === undefined) {
file = instance.files[filePath] = <TSFile>{ version: 0 };
}
if (file.text !== contents) {
file.version++;
file.text = contents;
instance.version!++;
}
// push this file to modified files hash.
if (!instance.modifiedFiles) {
instance.modifiedFiles = {};
}
instance.modifiedFiles[filePath] = file;
return file.version;
}
function getEmit(
rawFilePath: string,
filePath: string,
instance: TSInstance,
loader: Webpack
) {
// Emit Javascript
const output = instance.languageService!.getEmitOutput(filePath);
loader.clearDependencies();
loader.addDependency(rawFilePath);
const allDefinitionFiles = Object.keys(instance.files).filter(defFilePath => defFilePath.match(constants.dtsDtsxRegex));
// Make this file dependent on *all* definition files in the program
const addDependency = loader.addDependency.bind(loader);
allDefinitionFiles.forEach(addDependency);
// Additionally make this file dependent on all imported files
const fileDependencies = instance.dependencyGraph[filePath];
const additionalDependencies = fileDependencies === undefined
? []
: fileDependencies.map(module => module.originalFileName);
if (additionalDependencies) {
additionalDependencies.forEach(addDependency);
}
loader._module.meta.tsLoaderDefinitionFileVersions = allDefinitionFiles
.concat(additionalDependencies)
.map(defFilePath => defFilePath + '@' + (instance.files[defFilePath] || { version: '?' }).version);
const outputFile = output.outputFiles.filter(outputFile => outputFile.name.match(constants.jsJsx)).pop();
const outputText = (outputFile) ? outputFile.text : undefined;
const sourceMapFile = output.outputFiles.filter(outputFile => outputFile.name.match(constants.jsJsxMap)).pop();
const sourceMapText = (sourceMapFile) ? sourceMapFile.text : undefined;
return { outputText, sourceMapText };
}
/**
* Transpile file
*/
function getTranspilationEmit(
filePath: string,
contents: string,
instance: TSInstance,
loader: Webpack
) {
const fileName = path.basename(filePath);
const { outputText, sourceMapText, diagnostics } = instance.compiler.transpileModule(contents, {
compilerOptions: { ...instance.compilerOptions, rootDir: undefined },
transformers: instance.transformers,
reportDiagnostics: true,
fileName,
});
// _module.errors is not available inside happypack - see https://github.com/TypeStrong/ts-loader/issues/336
if (!instance.loaderOptions.happyPackMode) {
registerWebpackErrors(
loader._module.errors,
formatErrors(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { module: loader._module })
);
}
return { outputText, sourceMapText };
}
function makeSourceMap(
sourceMapText: string | undefined,
outputText: string,
filePath: string,
contents: string,
loader: Webpack
) {
if (sourceMapText === undefined) {
return { output: outputText, sourceMap: undefined };
}
return {
output: outputText.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''),
sourceMap: Object.assign(JSON.parse(sourceMapText), {
sources: [loaderUtils.getRemainingRequest(loader)],
file: filePath,
sourcesContent: [contents]
})
};
}
export = loader;