| 1 |
import traverse from "@babel/traverse"; |
| 2 |
import type * as t from "@babel/types"; |
| 3 |
type SourceMap = any; |
| 4 |
import type { Handler } from "gensync"; |
| 5 |
|
| 6 |
import type { ResolvedConfig, Plugin, PluginPasses } from "../config"; |
| 7 |
|
| 8 |
import PluginPass from "./plugin-pass"; |
| 9 |
import loadBlockHoistPlugin from "./block-hoist-plugin"; |
| 10 |
import normalizeOptions from "./normalize-opts"; |
| 11 |
import normalizeFile from "./normalize-file"; |
| 12 |
|
| 13 |
import generateCode from "./file/generate"; |
| 14 |
import type File from "./file/file"; |
| 15 |
|
| 16 |
import { flattenToSet } from "../config/helpers/deep-array"; |
| 17 |
|
| 18 |
export type FileResultCallback = { |
| 19 |
(err: Error, file: null): void; |
| 20 |
(err: null, file: FileResult | null): void; |
| 21 |
}; |
| 22 |
|
| 23 |
export type FileResult = { |
| 24 |
metadata: { [key: string]: any }; |
| 25 |
options: { [key: string]: any }; |
| 26 |
ast: t.File | null; |
| 27 |
code: string | null; |
| 28 |
map: SourceMap | null; |
| 29 |
sourceType: "script" | "module"; |
| 30 |
externalDependencies: Set<string>; |
| 31 |
}; |
| 32 |
|
| 33 |
export function* run( |
| 34 |
config: ResolvedConfig, |
| 35 |
code: string, |
| 36 |
ast?: t.File | t.Program | null, |
| 37 |
): Handler<FileResult> { |
| 38 |
const file = yield* normalizeFile( |
| 39 |
config.passes, |
| 40 |
normalizeOptions(config), |
| 41 |
code, |
| 42 |
ast, |
| 43 |
); |
| 44 |
|
| 45 |
const opts = file.opts; |
| 46 |
try { |
| 47 |
yield* transformFile(file, config.passes); |
| 48 |
} catch (e) { |
| 49 |
e.message = `${opts.filename ?? "unknown file"}: ${e.message}`; |
| 50 |
if (!e.code) { |
| 51 |
e.code = "BABEL_TRANSFORM_ERROR"; |
| 52 |
} |
| 53 |
throw e; |
| 54 |
} |
| 55 |
|
| 56 |
let outputCode, outputMap; |
| 57 |
try { |
| 58 |
if (opts.code !== false) { |
| 59 |
({ outputCode, outputMap } = generateCode(config.passes, file)); |
| 60 |
} |
| 61 |
} catch (e) { |
| 62 |
e.message = `${opts.filename ?? "unknown file"}: ${e.message}`; |
| 63 |
if (!e.code) { |
| 64 |
e.code = "BABEL_GENERATE_ERROR"; |
| 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: flattenToSet(config.externalDependencies), |
| 77 |
}; |
| 78 |
} |
| 79 |
|
| 80 |
function* transformFile(file: File, pluginPasses: PluginPasses): Handler<void> { |
| 81 |
for (const pluginPairs of pluginPasses) { |
| 82 |
const passPairs: [Plugin, PluginPass][] = []; |
| 83 |
const passes = []; |
| 84 |
const visitors = []; |
| 85 |
|
| 86 |
for (const plugin of pluginPairs.concat([loadBlockHoistPlugin()])) { |
| 87 |
const pass = new PluginPass(file, plugin.key, plugin.options); |
| 88 |
|
| 89 |
passPairs.push([plugin, pass]); |
| 90 |
passes.push(pass); |
| 91 |
visitors.push(plugin.visitor); |
| 92 |
} |
| 93 |
|
| 94 |
for (const [plugin, pass] of passPairs) { |
| 95 |
const fn = plugin.pre; |
| 96 |
if (fn) { |
| 97 |
const result = fn.call(pass, file); |
| 98 |
|
| 99 |
// @ts-expect-error - If we want to support async .pre |
| 100 |
yield* []; |
| 101 |
|
| 102 |
if (isThenable(result)) { |
| 103 |
throw new Error( |
| 104 |
`You appear to be using an plugin with an async .pre, ` + |
| 105 |
`which your current version of Babel does not support. ` + |
| 106 |
`If you're using a published plugin, you may need to upgrade ` + |
| 107 |
`your @babel/core version.`, |
| 108 |
); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// merge all plugin visitors into a single visitor |
| 114 |
const visitor = traverse.visitors.merge( |
| 115 |
visitors, |
| 116 |
passes, |
| 117 |
file.opts.wrapPluginVisitorMethod, |
| 118 |
); |
| 119 |
traverse(file.ast, visitor, file.scope); |
| 120 |
|
| 121 |
for (const [plugin, pass] of passPairs) { |
| 122 |
const fn = plugin.post; |
| 123 |
if (fn) { |
| 124 |
const result = fn.call(pass, file); |
| 125 |
|
| 126 |
// @ts-expect-error - If we want to support async .post |
| 127 |
yield* []; |
| 128 |
|
| 129 |
if (isThenable(result)) { |
| 130 |
throw new Error( |
| 131 |
`You appear to be using an plugin with an async .post, ` + |
| 132 |
`which your current version of Babel does not support. ` + |
| 133 |
`If you're using a published plugin, you may need to upgrade ` + |
| 134 |
`your @babel/core version.`, |
| 135 |
); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
function isThenable<T extends PromiseLike<any>>(val: any): val is T { |
| 143 |
return ( |
| 144 |
!!val && |
| 145 |
(typeof val === "object" || typeof val === "function") && |
| 146 |
!!val.then && |
| 147 |
typeof val.then === "function" |
| 148 |
); |
| 149 |
} |