1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = loader;
|
7 |
|
8 | var _sourceMap = require("source-map");
|
9 |
|
10 | var _options = _interopRequireDefault(require("./options.json"));
|
11 |
|
12 | var _utils = require("./utils");
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const HEADER = "/*** IMPORTS FROM imports-loader ***/\n";
|
21 |
|
22 | function loader(content, sourceMap) {
|
23 | const options = this.getOptions(_options.default);
|
24 | const type = options.type || "module";
|
25 | const callback = this.async();
|
26 | let importsCode = HEADER;
|
27 | let imports;
|
28 |
|
29 | if (typeof options.imports !== "undefined") {
|
30 | try {
|
31 | imports = (0, _utils.getImports)(type, options.imports);
|
32 | } catch (error) {
|
33 | callback(error);
|
34 | return;
|
35 | }
|
36 |
|
37 |
|
38 | const directive = (0, _utils.sourceHasUseStrict)(content);
|
39 | importsCode += Object.entries(imports).reduce((accumulator, item) => `${accumulator}${(0, _utils.renderImports)(this, type, item[0], item[1])}\n`, directive ? "'use strict';\n" : "");
|
40 | }
|
41 |
|
42 | if (typeof options.additionalCode !== "undefined") {
|
43 | importsCode += `\n${options.additionalCode}\n`;
|
44 | }
|
45 |
|
46 | let codeAfterModule = "";
|
47 |
|
48 | if (typeof options.wrapper !== "undefined") {
|
49 | let thisArg;
|
50 | let args;
|
51 | let params;
|
52 |
|
53 | if (typeof options.wrapper === "boolean") {
|
54 | thisArg = "";
|
55 | params = "";
|
56 | args = "";
|
57 | } else if (typeof options.wrapper === "string") {
|
58 | thisArg = options.wrapper;
|
59 | params = "";
|
60 | args = "";
|
61 | } else {
|
62 | ({
|
63 | thisArg,
|
64 | args
|
65 | } = options.wrapper);
|
66 |
|
67 | if (Array.isArray(args)) {
|
68 | params = args.join(", ");
|
69 | args = params;
|
70 | } else {
|
71 | params = Object.keys(args).join(", ");
|
72 | args = Object.values(args).join(", ");
|
73 | }
|
74 | }
|
75 |
|
76 | importsCode += `\n(function(${params}) {`;
|
77 | codeAfterModule += `\n}.call(${thisArg}${args ? `, ${args}` : ""}));\n`;
|
78 | }
|
79 |
|
80 | if (this.sourceMap) {
|
81 | if (sourceMap) {
|
82 | const node = _sourceMap.SourceNode.fromStringWithSourceMap(content, new _sourceMap.SourceMapConsumer(sourceMap));
|
83 |
|
84 | node.prepend(`${importsCode}\n`);
|
85 | node.add(codeAfterModule);
|
86 | const result = node.toStringWithSourceMap({
|
87 | file: this.resourcePath
|
88 | });
|
89 | callback(null, result.code, result.map.toJSON());
|
90 | return;
|
91 | }
|
92 |
|
93 | const generator = new _sourceMap.SourceMapGenerator();
|
94 | generator.setSourceContent(this.resourcePath, content);
|
95 | generator.addMapping({
|
96 | generated: {
|
97 | line: importsCode.split("\n").length + 1,
|
98 | column: 0
|
99 | },
|
100 | original: {
|
101 | line: 1,
|
102 | column: 0
|
103 | },
|
104 | source: this.resourcePath
|
105 | });
|
106 | callback(null, `${importsCode}\n${content}\n${codeAfterModule}`, generator.toString());
|
107 | return;
|
108 | }
|
109 |
|
110 | callback(null, `${importsCode}\n${content}${codeAfterModule}`, sourceMap);
|
111 | } |
\ | No newline at end of file |