1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = generateCode;
|
7 |
|
8 | function _generator() {
|
9 | const data = _interopRequireDefault(require("@babel/generator"));
|
10 |
|
11 | _generator = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | var _mergeMap = _interopRequireDefault(require("./merge-map"));
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 | const convertSourceMap = require("convert-source-map");
|
23 |
|
24 | function generateCode(pluginPasses, file) {
|
25 | const {
|
26 | opts,
|
27 | ast,
|
28 | code,
|
29 | inputMap
|
30 | } = file;
|
31 | const results = [];
|
32 |
|
33 | for (const plugins of pluginPasses) {
|
34 | for (const plugin of plugins) {
|
35 | const {
|
36 | generatorOverride
|
37 | } = plugin;
|
38 |
|
39 | if (generatorOverride) {
|
40 | const result = generatorOverride(ast, opts.generatorOpts, code, _generator().default);
|
41 | if (result !== undefined) results.push(result);
|
42 | }
|
43 | }
|
44 | }
|
45 |
|
46 | let result;
|
47 |
|
48 | if (results.length === 0) {
|
49 | result = (0, _generator().default)(ast, opts.generatorOpts, code);
|
50 | } else if (results.length === 1) {
|
51 | result = results[0];
|
52 |
|
53 | if (typeof result.then === "function") {
|
54 | throw new Error(`You appear to be using an async codegen plugin, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, ` + `you may need to upgrade your @babel/core version.`);
|
55 | }
|
56 | } else {
|
57 | throw new Error("More than one plugin attempted to override codegen.");
|
58 | }
|
59 |
|
60 | let {
|
61 | code: outputCode,
|
62 | map: outputMap
|
63 | } = result;
|
64 |
|
65 | if (outputMap && inputMap) {
|
66 | outputMap = (0, _mergeMap.default)(inputMap.toObject(), outputMap);
|
67 | }
|
68 |
|
69 | if (opts.sourceMaps === "inline" || opts.sourceMaps === "both") {
|
70 | outputCode += "\n" + convertSourceMap.fromObject(outputMap).toComment();
|
71 | }
|
72 |
|
73 | if (opts.sourceMaps === "inline") {
|
74 | outputMap = null;
|
75 | }
|
76 |
|
77 | return {
|
78 | outputCode,
|
79 | outputMap
|
80 | };
|
81 | } |
\ | No newline at end of file |