1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.run = run;
|
7 |
|
8 | function _traverse() {
|
9 | const data = require("@babel/traverse");
|
10 |
|
11 | _traverse = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | var _pluginPass = require("./plugin-pass");
|
19 |
|
20 | var _blockHoistPlugin = require("./block-hoist-plugin");
|
21 |
|
22 | var _normalizeOpts = require("./normalize-opts");
|
23 |
|
24 | var _normalizeFile = require("./normalize-file");
|
25 |
|
26 | var _generate = require("./file/generate");
|
27 |
|
28 | var _deepArray = require("../config/helpers/deep-array");
|
29 |
|
30 | function* run(config, code, ast) {
|
31 | const file = yield* (0, _normalizeFile.default)(config.passes, (0, _normalizeOpts.default)(config), code, ast);
|
32 | const opts = file.opts;
|
33 |
|
34 | try {
|
35 | yield* transformFile(file, config.passes);
|
36 | } catch (e) {
|
37 | var _opts$filename;
|
38 |
|
39 | e.message = `${(_opts$filename = opts.filename) != null ? _opts$filename : "unknown"}: ${e.message}`;
|
40 |
|
41 | if (!e.code) {
|
42 | e.code = "BABEL_TRANSFORM_ERROR";
|
43 | }
|
44 |
|
45 | throw e;
|
46 | }
|
47 |
|
48 | let outputCode, outputMap;
|
49 |
|
50 | try {
|
51 | if (opts.code !== false) {
|
52 | ({
|
53 | outputCode,
|
54 | outputMap
|
55 | } = (0, _generate.default)(config.passes, file));
|
56 | }
|
57 | } catch (e) {
|
58 | var _opts$filename2;
|
59 |
|
60 | e.message = `${(_opts$filename2 = opts.filename) != null ? _opts$filename2 : "unknown"}: ${e.message}`;
|
61 |
|
62 | if (!e.code) {
|
63 | e.code = "BABEL_GENERATE_ERROR";
|
64 | }
|
65 |
|
66 | throw e;
|
67 | }
|
68 |
|
69 | return {
|
70 | metadata: file.metadata,
|
71 | options: opts,
|
72 | ast: opts.ast === true ? file.ast : null,
|
73 | code: outputCode === undefined ? null : outputCode,
|
74 | map: outputMap === undefined ? null : outputMap,
|
75 | sourceType: file.ast.program.sourceType,
|
76 | externalDependencies: (0, _deepArray.flattenToSet)(config.externalDependencies)
|
77 | };
|
78 | }
|
79 |
|
80 | function* transformFile(file, pluginPasses) {
|
81 | for (const pluginPairs of pluginPasses) {
|
82 | const passPairs = [];
|
83 | const passes = [];
|
84 | const visitors = [];
|
85 |
|
86 | for (const plugin of pluginPairs.concat([(0, _blockHoistPlugin.default)()])) {
|
87 | const pass = new _pluginPass.default(file, plugin.key, plugin.options);
|
88 | passPairs.push([plugin, pass]);
|
89 | passes.push(pass);
|
90 | visitors.push(plugin.visitor);
|
91 | }
|
92 |
|
93 | for (const [plugin, pass] of passPairs) {
|
94 | const fn = plugin.pre;
|
95 |
|
96 | if (fn) {
|
97 | const result = fn.call(pass, file);
|
98 | yield* [];
|
99 |
|
100 | if (isThenable(result)) {
|
101 | throw new Error(`You appear to be using an plugin with an async .pre, ` + `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.`);
|
102 | }
|
103 | }
|
104 | }
|
105 |
|
106 | const visitor = _traverse().default.visitors.merge(visitors, passes, file.opts.wrapPluginVisitorMethod);
|
107 |
|
108 | (0, _traverse().default)(file.ast, visitor, file.scope);
|
109 |
|
110 | for (const [plugin, pass] of passPairs) {
|
111 | const fn = plugin.post;
|
112 |
|
113 | if (fn) {
|
114 | const result = fn.call(pass, file);
|
115 | yield* [];
|
116 |
|
117 | if (isThenable(result)) {
|
118 | throw new Error(`You appear to be using an plugin with an async .post, ` + `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.`);
|
119 | }
|
120 | }
|
121 | }
|
122 | }
|
123 | }
|
124 |
|
125 | function isThenable(val) {
|
126 | return !!val && (typeof val === "object" || typeof val === "function") && !!val.then && typeof val.then === "function";
|
127 | } |
\ | No newline at end of file |