Merge pull request #8023 from loganfsmyth/sync-async
Add a promise-returning *Async version of the transform and parse fns
This commit is contained in:
commit
98ff2ce877
@ -27,8 +27,7 @@
|
||||
},
|
||||
"browser": {
|
||||
"./lib/config/files/index.js": "./lib/config/files/index-browser.js",
|
||||
"./lib/transform-file.js": "./lib/transform-file-browser.js",
|
||||
"./lib/transform-file-sync.js": "./lib/transform-file-sync-browser.js"
|
||||
"./lib/transform-file.js": "./lib/transform-file-browser.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/code-frame": "7.0.0-beta.48",
|
||||
|
||||
@ -17,10 +17,3 @@ export function loadOptions(opts: {}): Object | null {
|
||||
|
||||
return config ? config.options : null;
|
||||
}
|
||||
|
||||
// For easier backward-compatibility, provide an API like the one we exposed in Babel 6.
|
||||
export class OptionManager {
|
||||
init(opts: {}) {
|
||||
return loadOptions(opts);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,26 +13,22 @@ export * as types from "@babel/types";
|
||||
export { default as traverse } from "@babel/traverse";
|
||||
export { default as template } from "@babel/template";
|
||||
|
||||
export { loadPartialConfig, loadOptions, OptionManager } from "./config";
|
||||
|
||||
export { createConfigItem } from "./config/item";
|
||||
|
||||
export function Plugin(alias: string) {
|
||||
throw new Error(
|
||||
`The (${alias}) Babel 5 plugin is being run with an unsupported Babel version.`,
|
||||
);
|
||||
}
|
||||
export { loadPartialConfig, loadOptions } from "./config";
|
||||
|
||||
export { default as transform } from "./transform";
|
||||
export { default as transformSync } from "./transform-sync";
|
||||
|
||||
export { default as transformFile } from "./transform-file";
|
||||
export { default as transformFileSync } from "./transform-file-sync";
|
||||
|
||||
export { default as transformFromAst } from "./transform-ast";
|
||||
export { default as transformFromAstSync } from "./transform-ast-sync";
|
||||
|
||||
export { default as parse } from "./parse";
|
||||
export { transform, transformSync, transformAsync } from "./transform";
|
||||
export {
|
||||
transformFile,
|
||||
transformFileSync,
|
||||
transformFileAsync,
|
||||
} from "./transform-file";
|
||||
export {
|
||||
transformFromAst,
|
||||
transformFromAstSync,
|
||||
transformFromAstAsync,
|
||||
} from "./transform-ast";
|
||||
export { parse, parseSync, parseAsync } from "./parse";
|
||||
|
||||
/**
|
||||
* Recommended set of compilable extensions. Not used in @babel/core directly, but meant as
|
||||
@ -45,3 +41,17 @@ export const DEFAULT_EXTENSIONS = Object.freeze([
|
||||
".es",
|
||||
".mjs",
|
||||
]);
|
||||
|
||||
// For easier backward-compatibility, provide an API like the one we exposed in Babel 6.
|
||||
import { loadOptions } from "./config";
|
||||
export class OptionManager {
|
||||
init(opts: {}) {
|
||||
return loadOptions(opts);
|
||||
}
|
||||
}
|
||||
|
||||
export function Plugin(alias: string) {
|
||||
throw new Error(
|
||||
`The (${alias}) Babel 5 plugin is being run with an unsupported Babel version.`,
|
||||
);
|
||||
}
|
||||
|
||||
@ -6,17 +6,79 @@ import normalizeOptions from "./transformation/normalize-opts";
|
||||
|
||||
type AstRoot = BabelNodeFile | BabelNodeProgram;
|
||||
|
||||
export default function parse(
|
||||
code: string,
|
||||
opts: InputOptions,
|
||||
): AstRoot | null {
|
||||
export type ParseResult = AstRoot;
|
||||
|
||||
export type FileParseCallback = {
|
||||
(Error, null): any,
|
||||
(null, ParseResult | null): any,
|
||||
};
|
||||
|
||||
type Parse = {
|
||||
(code: string, callback: FileParseCallback): void,
|
||||
(code: string, opts: ?InputOptions, callback: FileParseCallback): void,
|
||||
|
||||
// Here for backward-compatibility. Ideally use ".parseSync" if you want
|
||||
// a synchronous API.
|
||||
(code: string, opts: ?InputOptions): ParseResult | null,
|
||||
};
|
||||
|
||||
export const parse: Parse = (function parse(code, opts, callback) {
|
||||
if (typeof opts === "function") {
|
||||
opts = undefined;
|
||||
callback = opts;
|
||||
}
|
||||
|
||||
// For backward-compat with Babel 7's early betas, we allow sync parsing when
|
||||
// no callback is given. Will be dropped in some future Babel major version.
|
||||
if (callback === undefined) return parseSync(code, opts);
|
||||
|
||||
const config = loadConfig(opts);
|
||||
|
||||
if (config === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const file = normalizeFile(config.passes, normalizeOptions(config), code);
|
||||
// Reassign to keep Flowtype happy.
|
||||
const cb = callback;
|
||||
|
||||
return file.ast;
|
||||
// Just delaying the transform one tick for now to simulate async behavior
|
||||
// but more async logic may land here eventually.
|
||||
process.nextTick(() => {
|
||||
let ast = null;
|
||||
try {
|
||||
const cfg = loadConfig(opts);
|
||||
if (cfg === null) return cb(null, null);
|
||||
|
||||
ast = normalizeFile(cfg.passes, normalizeOptions(cfg), code).ast;
|
||||
} catch (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
cb(null, ast);
|
||||
});
|
||||
}: Function);
|
||||
|
||||
export function parseSync(
|
||||
code: string,
|
||||
opts?: InputOptions,
|
||||
): ParseResult | null {
|
||||
const config = loadConfig(opts);
|
||||
|
||||
if (config === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return normalizeFile(config.passes, normalizeOptions(config), code).ast;
|
||||
}
|
||||
|
||||
export function parseAsync(
|
||||
code: string,
|
||||
opts?: InputOptions,
|
||||
): Promise<ParseResult | null> {
|
||||
return new Promise((res, rej) => {
|
||||
parse(code, opts, (err, result) => {
|
||||
if (err == null) res(result);
|
||||
else rej(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
// @flow
|
||||
import loadConfig, { type InputOptions } from "./config";
|
||||
import { runSync, type FileResult } from "./transformation";
|
||||
|
||||
type AstRoot = BabelNodeFile | BabelNodeProgram;
|
||||
|
||||
export default function transformFromAstSync(
|
||||
ast: AstRoot,
|
||||
code: string,
|
||||
opts: ?InputOptions,
|
||||
): FileResult | null {
|
||||
const config = loadConfig(opts);
|
||||
if (config === null) return null;
|
||||
|
||||
if (!ast) throw new Error("No AST given");
|
||||
|
||||
return runSync(config, code, ast);
|
||||
}
|
||||
@ -2,16 +2,15 @@
|
||||
|
||||
import loadConfig, { type InputOptions } from "./config";
|
||||
import {
|
||||
runSync,
|
||||
runAsync,
|
||||
type FileResult,
|
||||
type FileResultCallback,
|
||||
} from "./transformation";
|
||||
|
||||
import transformAstSync from "./transform-ast-sync";
|
||||
|
||||
type AstRoot = BabelNodeFile | BabelNodeProgram;
|
||||
|
||||
type TransformAst = {
|
||||
type TransformFromAst = {
|
||||
(ast: AstRoot, code: string, callback: FileResultCallback): void,
|
||||
(
|
||||
ast: AstRoot,
|
||||
@ -25,7 +24,12 @@ type TransformAst = {
|
||||
(ast: AstRoot, code: string, opts: ?InputOptions): FileResult | null,
|
||||
};
|
||||
|
||||
export default ((function transformFromAst(ast, code, opts, callback) {
|
||||
export const transformFromAst: TransformFromAst = (function transformFromAst(
|
||||
ast,
|
||||
code,
|
||||
opts,
|
||||
callback,
|
||||
) {
|
||||
if (typeof opts === "function") {
|
||||
opts = undefined;
|
||||
callback = opts;
|
||||
@ -33,7 +37,7 @@ export default ((function transformFromAst(ast, code, opts, callback) {
|
||||
|
||||
// For backward-compat with Babel 6, we allow sync transformation when
|
||||
// no callback is given. Will be dropped in some future Babel major version.
|
||||
if (callback === undefined) return transformAstSync(ast, code, opts);
|
||||
if (callback === undefined) return transformFromAstSync(ast, code, opts);
|
||||
|
||||
// Reassign to keep Flowtype happy.
|
||||
const cb = callback;
|
||||
@ -53,4 +57,30 @@ export default ((function transformFromAst(ast, code, opts, callback) {
|
||||
|
||||
runAsync(cfg, code, ast, cb);
|
||||
});
|
||||
}: Function): TransformAst);
|
||||
}: Function);
|
||||
|
||||
export function transformFromAstSync(
|
||||
ast: AstRoot,
|
||||
code: string,
|
||||
opts: ?InputOptions,
|
||||
): FileResult | null {
|
||||
const config = loadConfig(opts);
|
||||
if (config === null) return null;
|
||||
|
||||
if (!ast) throw new Error("No AST given");
|
||||
|
||||
return runSync(config, code, ast);
|
||||
}
|
||||
|
||||
export function transformFromAstAsync(
|
||||
ast: AstRoot,
|
||||
code: string,
|
||||
opts: ?InputOptions,
|
||||
): Promise<FileResult | null> {
|
||||
return new Promise((res, rej) => {
|
||||
transformFromAst(ast, code, opts, (err, result) => {
|
||||
if (err == null) res(result);
|
||||
else rej(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
// @flow
|
||||
import fs from "fs";
|
||||
|
||||
import loadConfig, { type InputOptions } from "./config";
|
||||
import { runSync, type FileResult } from "./transformation";
|
||||
|
||||
export default function transformFileSync(
|
||||
filename: string,
|
||||
opts: ?InputOptions,
|
||||
): FileResult | null {
|
||||
let options;
|
||||
if (opts == null) {
|
||||
options = { filename };
|
||||
} else if (opts && typeof opts === "object") {
|
||||
options = {
|
||||
...opts,
|
||||
filename,
|
||||
};
|
||||
}
|
||||
|
||||
const config = loadConfig(options);
|
||||
if (config === null) return null;
|
||||
|
||||
return runSync(config, fs.readFileSync(filename, "utf8"));
|
||||
}
|
||||
@ -2,14 +2,23 @@
|
||||
import fs from "fs";
|
||||
|
||||
import loadConfig, { type InputOptions } from "./config";
|
||||
import { runAsync, type FileResultCallback } from "./transformation";
|
||||
import {
|
||||
runSync,
|
||||
runAsync,
|
||||
type FileResult,
|
||||
type FileResultCallback,
|
||||
} from "./transformation";
|
||||
|
||||
type TransformFile = {
|
||||
(filename: string, callback: FileResultCallback): void,
|
||||
(filename: string, opts: ?InputOptions, callback: FileResultCallback): void,
|
||||
};
|
||||
|
||||
export default ((function transformFile(filename, opts, callback) {
|
||||
export const transformFile: TransformFile = (function transformFile(
|
||||
filename,
|
||||
opts,
|
||||
callback,
|
||||
) {
|
||||
let options;
|
||||
if (typeof opts === "function") {
|
||||
callback = opts;
|
||||
@ -43,4 +52,36 @@ export default ((function transformFile(filename, opts, callback) {
|
||||
runAsync(config, code, null, callback);
|
||||
});
|
||||
});
|
||||
}: Function): TransformFile);
|
||||
}: Function);
|
||||
|
||||
export function transformFileSync(
|
||||
filename: string,
|
||||
opts: ?InputOptions,
|
||||
): FileResult | null {
|
||||
let options;
|
||||
if (opts == null) {
|
||||
options = { filename };
|
||||
} else if (opts && typeof opts === "object") {
|
||||
options = {
|
||||
...opts,
|
||||
filename,
|
||||
};
|
||||
}
|
||||
|
||||
const config = loadConfig(options);
|
||||
if (config === null) return null;
|
||||
|
||||
return runSync(config, fs.readFileSync(filename, "utf8"));
|
||||
}
|
||||
|
||||
export function transformFileAsync(
|
||||
filename: string,
|
||||
opts: ?InputOptions,
|
||||
): Promise<FileResult | null> {
|
||||
return new Promise((res, rej) => {
|
||||
transformFile(filename, opts, (err, result) => {
|
||||
if (err == null) res(result);
|
||||
else rej(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
// @flow
|
||||
import loadConfig, { type InputOptions } from "./config";
|
||||
import { runSync, type FileResult } from "./transformation";
|
||||
|
||||
export default function transformSync(
|
||||
code: string,
|
||||
opts: ?InputOptions,
|
||||
): FileResult | null {
|
||||
const config = loadConfig(opts);
|
||||
if (config === null) return null;
|
||||
|
||||
return runSync(config, code);
|
||||
}
|
||||
@ -1,13 +1,12 @@
|
||||
// @flow
|
||||
import loadConfig, { type InputOptions } from "./config";
|
||||
import {
|
||||
runSync,
|
||||
runAsync,
|
||||
type FileResult,
|
||||
type FileResultCallback,
|
||||
} from "./transformation";
|
||||
|
||||
import transformSync from "./transform-sync";
|
||||
|
||||
type Transform = {
|
||||
(code: string, callback: FileResultCallback): void,
|
||||
(code: string, opts: ?InputOptions, callback: FileResultCallback): void,
|
||||
@ -17,7 +16,7 @@ type Transform = {
|
||||
(code: string, opts: ?InputOptions): FileResult | null,
|
||||
};
|
||||
|
||||
export default ((function transform(code, opts, callback) {
|
||||
export const transform: Transform = (function transform(code, opts, callback) {
|
||||
if (typeof opts === "function") {
|
||||
opts = undefined;
|
||||
callback = opts;
|
||||
@ -43,4 +42,26 @@ export default ((function transform(code, opts, callback) {
|
||||
|
||||
runAsync(cfg, code, null, cb);
|
||||
});
|
||||
}: Function): Transform);
|
||||
}: Function);
|
||||
|
||||
export function transformSync(
|
||||
code: string,
|
||||
opts: ?InputOptions,
|
||||
): FileResult | null {
|
||||
const config = loadConfig(opts);
|
||||
if (config === null) return null;
|
||||
|
||||
return runSync(config, code);
|
||||
}
|
||||
|
||||
export function transformAsync(
|
||||
code: string,
|
||||
opts: ?InputOptions,
|
||||
): Promise<FileResult | null> {
|
||||
return new Promise((res, rej) => {
|
||||
transform(code, opts, (err, result) => {
|
||||
if (err == null) res(result);
|
||||
else rej(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user