UNPKG

2.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var path_1 = require("path");
4var helpers_1 = require("../util/helpers");
5var logger_1 = require("../logger/logger");
6function webpackLoader(source, map, webpackContex) {
7 webpackContex.cacheable();
8 var callback = webpackContex.async();
9 var context = helpers_1.getContext();
10 var absolutePath = path_1.resolve(path_1.normalize(webpackContex.resourcePath));
11 logger_1.Logger.debug("[Webpack] webpackLoader: processing the following file: " + absolutePath);
12 var javascriptPath = helpers_1.changeExtension(absolutePath, '.js');
13 var sourceMapPath = javascriptPath + '.map';
14 Promise.all([
15 readFile(context.fileCache, javascriptPath),
16 readFile(context.fileCache, sourceMapPath)
17 ]).then(function (_a) {
18 var javascriptFile = _a[0], mapFile = _a[1];
19 var sourceMapObject = map;
20 if (mapFile) {
21 try {
22 sourceMapObject = JSON.parse(mapFile.content);
23 }
24 catch (ex) {
25 logger_1.Logger.debug("[Webpack] loader: Attempted to parse the JSON sourcemap for " + mapFile.path + " and failed -\n using the original, webpack provided source map");
26 }
27 if (sourceMapObject) {
28 sourceMapObject.sources = [absolutePath];
29 if (!sourceMapObject.sourcesContent || sourceMapObject.sourcesContent.length === 0) {
30 sourceMapObject.sourcesContent = [source];
31 }
32 }
33 }
34 callback(null, javascriptFile.content, sourceMapObject);
35 }).catch(function (err) {
36 logger_1.Logger.debug("[Webpack] loader: Encountered an unexpected error: " + err.message);
37 callback(err);
38 });
39}
40exports.webpackLoader = webpackLoader;
41function readFile(fileCache, filePath) {
42 return helpers_1.readAndCacheFile(filePath).then(function (fileContent) {
43 logger_1.Logger.debug("[Webpack] loader: Loaded " + filePath + " successfully from disk");
44 return fileCache.get(filePath);
45 }).catch(function (err) {
46 logger_1.Logger.debug("[Webpack] loader: Failed to load " + filePath + " from disk");
47 throw err;
48 });
49}