UNPKG

4.59 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.array.concat");
4
5require("core-js/modules/es.array.map");
6
7require("core-js/modules/es.object.to-string");
8
9require("core-js/modules/es.promise");
10
11require("core-js/modules/es.regexp.exec");
12
13require("core-js/modules/es.string.split");
14
15Object.defineProperty(exports, "__esModule", {
16 value: true
17});
18exports.transform = transform;
19
20require("regenerator-runtime/runtime");
21
22var _sourceMap = require("source-map");
23
24var _readAsObject = require("./dependencies-lookup/readAsObject");
25
26function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
27
28function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
29
30function transform(_x, _x2) {
31 return _transform.apply(this, arguments);
32}
33
34function _transform() {
35 _transform = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(inputSource, inputSourceMap) {
36 var _this = this;
37
38 var callback, sourceObject, source, sourceJson, addsMap, sourceNode, preamble, _sourceNode$toStringW, code, map;
39
40 return regeneratorRuntime.wrap(function _callee$(_context) {
41 while (1) {
42 switch (_context.prev = _context.next) {
43 case 0:
44 callback = this.async();
45 _context.prev = 1;
46 _context.next = 4;
47 return (0, _readAsObject.readStory)(this, inputSource);
48
49 case 4:
50 sourceObject = _context.sent;
51
52 if (!(!sourceObject.source || sourceObject.source.length === 0)) {
53 _context.next = 8;
54 break;
55 }
56
57 callback(null, inputSource, inputSourceMap);
58 return _context.abrupt("return");
59
60 case 8:
61 source = sourceObject.source, sourceJson = sourceObject.sourceJson, addsMap = sourceObject.addsMap; // Use the SourceNode to produce the code. Given that the source mapping here is trivial it's easier to just
62 // always build a sourcemap rather than to have two different code paths.
63
64 if (!inputSourceMap) {
65 _context.next = 15;
66 break;
67 }
68
69 _context.next = 12;
70 return _sourceMap.SourceMapConsumer["with"](JSON.stringify(inputSourceMap), null, function (consumer) {
71 return _sourceMap.SourceNode.fromStringWithSourceMap(source, consumer);
72 });
73
74 case 12:
75 sourceNode = _context.sent;
76 _context.next = 18;
77 break;
78
79 case 15:
80 // Build an identity sourcemap. Note that "source" is already potentially differing from "inputSource"
81 // due to other loaders, so while we need to use "source" for the source node contents to generate the correct
82 // code, we still want to use "inputSource" as the source content.
83 sourceNode = new _sourceMap.SourceNode();
84 sourceNode.add(source.split(/\n/).map(function (line, index) {
85 return new _sourceMap.SourceNode(index + 1, 0, _this.resourcePath, "".concat(line, "\n"));
86 }));
87 sourceNode.setSourceContent(this.resourcePath, inputSource);
88
89 case 18:
90 // Prepend the preamble
91 preamble = "\n /* eslint-disable */\n // @ts-nocheck\n // @ts-ignore\n var __STORY__ = ".concat(sourceJson, ";\n // @ts-ignore\n var __LOCATIONS_MAP__ = ").concat(JSON.stringify(addsMap), ";");
92 sourceNode.prepend("".concat(preamble, "\n")); // Generate the code and the source map for the next loader
93
94 _sourceNode$toStringW = sourceNode.toStringWithSourceMap(), code = _sourceNode$toStringW.code, map = _sourceNode$toStringW.map;
95 callback(null, code, map);
96 _context.next = 27;
97 break;
98
99 case 24:
100 _context.prev = 24;
101 _context.t0 = _context["catch"](1);
102 callback(_context.t0);
103
104 case 27:
105 case "end":
106 return _context.stop();
107 }
108 }
109 }, _callee, this, [[1, 24]]);
110 }));
111 return _transform.apply(this, arguments);
112}
\No newline at end of file