1 | "use strict";
|
2 | var _ = require("lodash");
|
3 | var path = require("path");
|
4 | var cache_1 = require("./cache");
|
5 | var helpers = require("./helpers");
|
6 | var instance_1 = require("./instance");
|
7 | var paths_plugin_1 = require("./paths-plugin");
|
8 | var watch_mode_1 = require("./watch-mode");
|
9 | var loaderUtils = require('loader-utils');
|
10 | function loader(text) {
|
11 | try {
|
12 | compiler.call(undefined, this, text);
|
13 | }
|
14 | catch (e) {
|
15 | console.error(e, e.stack);
|
16 | throw e;
|
17 | }
|
18 | }
|
19 | (function (loader) {
|
20 | loader.TsConfigPathsPlugin = paths_plugin_1.PathPlugin;
|
21 | loader.CheckerPlugin = watch_mode_1.CheckerPlugin;
|
22 | })(loader || (loader = {}));
|
23 | var DECLARATION = /\.d.ts$/i;
|
24 | function compiler(loader, text) {
|
25 | if (loader.cacheable) {
|
26 | loader.cacheable();
|
27 | }
|
28 | var rootCompiler = instance_1.getRootCompiler(loader._compiler);
|
29 | var query = (loaderUtils.getOptions(loader) || {});
|
30 | var options = (loader.options && loader.options.ts) || {};
|
31 | var instanceName = query.instance || 'at-loader';
|
32 | var instance = instance_1.ensureInstance(loader, query, options, instanceName, rootCompiler);
|
33 | var callback = loader.async();
|
34 | var fileName = helpers.toUnix(loader.resourcePath);
|
35 | instance.compiledFiles[fileName] = true;
|
36 | if (DECLARATION.test(fileName)) {
|
37 | loader.emitWarning("[" + instanceName + "] TypeScript declaration files should never be required");
|
38 | return callback(null, '');
|
39 | }
|
40 | var compiledModule;
|
41 | if (instance.loaderConfig.usePrecompiledFiles) {
|
42 | compiledModule = cache_1.findCompiledModule(fileName);
|
43 | }
|
44 | var transformation = null;
|
45 | if (compiledModule) {
|
46 | transformation = Promise.resolve({
|
47 | text: compiledModule.text,
|
48 | map: compiledModule.map
|
49 | ? JSON.parse(compiledModule.map)
|
50 | : null
|
51 | }).then(function (result) { return ({ cached: true, result: result }); });
|
52 | }
|
53 | else {
|
54 | var transformationFunction = function () { return transform(loader, instance, fileName, text); };
|
55 | if (instance.loaderConfig.useCache) {
|
56 | transformation = cache_1.cache({
|
57 | source: text,
|
58 | identifier: instance.cacheIdentifier,
|
59 | directory: instance.loaderConfig.cacheDirectory,
|
60 | options: loader.query,
|
61 | transform: transformationFunction
|
62 | });
|
63 | }
|
64 | else {
|
65 | transformation = transformationFunction().then(function (result) { return ({ cached: false, result: result }); });
|
66 | }
|
67 | }
|
68 | transformation
|
69 | .then(function (_a) {
|
70 | var cached = _a.cached, result = _a.result;
|
71 | if (!instance.compilerConfig.options.isolatedModules && result.deps) {
|
72 | result.deps.forEach(function (dep) { return loader.addDependency(path.normalize(dep)); });
|
73 | }
|
74 | if (cached) {
|
75 | instance.checker.updateFile(fileName, text);
|
76 | }
|
77 | return result;
|
78 | })
|
79 | .then(function (_a) {
|
80 | var text = _a.text, map = _a.map;
|
81 | callback(null, text, map);
|
82 | })
|
83 | .catch(callback)
|
84 | .catch(function (e) {
|
85 | console.error('Error in bail mode:', e, e.stack.join
|
86 | ? e.stack.join('\n')
|
87 | : e.stack);
|
88 | process.exit(1);
|
89 | });
|
90 | }
|
91 | function transform(webpack, instance, fileName, text) {
|
92 | var resultText;
|
93 | var resultSourceMap = null;
|
94 | return instance.checker.emitFile(fileName, text).then((function (_a) {
|
95 | var emitResult = _a.emitResult, deps = _a.deps;
|
96 | resultSourceMap = emitResult.sourceMap;
|
97 | resultText = emitResult.text;
|
98 | var sourceFileName = fileName.replace(instance.context + '/', '');
|
99 | if (resultSourceMap) {
|
100 | resultSourceMap = JSON.parse(resultSourceMap);
|
101 | resultSourceMap.sources = [sourceFileName];
|
102 | resultSourceMap.file = sourceFileName;
|
103 | resultSourceMap.sourcesContent = [text];
|
104 | resultText = resultText.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
|
105 | }
|
106 | if (instance.loaderConfig.useBabel) {
|
107 | var defaultOptions = {
|
108 | inputSourceMap: resultSourceMap,
|
109 | sourceRoot: instance.context,
|
110 | filename: fileName,
|
111 | sourceMap: true
|
112 | };
|
113 | var babelOptions = _.assign({}, defaultOptions, instance.loaderConfig.babelOptions);
|
114 | var babelResult = instance.babelImpl.transform(resultText, babelOptions);
|
115 | resultText = babelResult.code;
|
116 | resultSourceMap = babelResult.map;
|
117 | }
|
118 | if (resultSourceMap) {
|
119 | var sourcePath = path.relative(instance.compilerConfig.options.sourceRoot || instance.context, loaderUtils.getRemainingRequest(webpack));
|
120 | resultSourceMap.sources = [sourcePath];
|
121 | resultSourceMap.file = fileName;
|
122 | resultSourceMap.sourcesContent = [text];
|
123 | }
|
124 | if (emitResult.declaration) {
|
125 | var declPath = path.relative(instance.context, emitResult.declaration.name);
|
126 | webpack.emitFile(declPath, emitResult.declaration.text);
|
127 | }
|
128 | return {
|
129 | text: resultText,
|
130 | map: resultSourceMap,
|
131 | deps: deps,
|
132 | };
|
133 | }));
|
134 | }
|
135 | module.exports = loader;
|
136 |
|
\ | No newline at end of file |