added flow to babel cli (#10244)
* added flow to babel cli * added 'SourceMapGenerator' as a argument to 'fromObject'
This commit is contained in:
parent
4506590557
commit
4d12c8971b
@ -165,7 +165,7 @@ declare module "source-map" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
declare module "convert-source-map" {
|
declare module "convert-source-map" {
|
||||||
import type { SourceMap } from "source-map";
|
import type { SourceMap, SourceMapGenerator } from "source-map";
|
||||||
|
|
||||||
declare class Converter {
|
declare class Converter {
|
||||||
toJSON(): string;
|
toJSON(): string;
|
||||||
@ -177,7 +177,7 @@ declare module "convert-source-map" {
|
|||||||
declare module.exports: {
|
declare module.exports: {
|
||||||
SourceMap: SourceMap,
|
SourceMap: SourceMap,
|
||||||
Converter: Converter,
|
Converter: Converter,
|
||||||
fromObject(obj: SourceMap): Converter,
|
fromObject(obj: SourceMap | SourceMapGenerator): Converter,
|
||||||
fromJSON(str: string): Converter,
|
fromJSON(str: string): Converter,
|
||||||
fromBase64(str: string): Converter,
|
fromBase64(str: string): Converter,
|
||||||
fromComment(str: string): Converter,
|
fromComment(str: string): Converter,
|
||||||
|
|||||||
@ -1,7 +1,12 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
import commander from "commander";
|
import commander from "commander";
|
||||||
import { buildExternalHelpers } from "@babel/core";
|
import { buildExternalHelpers } from "@babel/core";
|
||||||
|
|
||||||
function collect(value, previousValue): Array<string> {
|
function collect(
|
||||||
|
value: string | any,
|
||||||
|
previousValue: Array<string>,
|
||||||
|
): Array<string> {
|
||||||
// If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing.
|
// If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing.
|
||||||
if (typeof value !== "string") return previousValue;
|
if (typeof value !== "string") return previousValue;
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
import defaults from "lodash/defaults";
|
import defaults from "lodash/defaults";
|
||||||
import outputFileSync from "output-file-sync";
|
import outputFileSync from "output-file-sync";
|
||||||
import { sync as mkdirpSync } from "mkdirp";
|
import { sync as mkdirpSync } from "mkdirp";
|
||||||
@ -6,11 +8,15 @@ import path from "path";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
import * as util from "./util";
|
import * as util from "./util";
|
||||||
|
import { type CmdOptions } from "./options";
|
||||||
|
|
||||||
export default async function({ cliOptions, babelOptions }) {
|
export default async function({
|
||||||
|
cliOptions,
|
||||||
|
babelOptions,
|
||||||
|
}: CmdOptions): Promise<void> {
|
||||||
const filenames = cliOptions.filenames;
|
const filenames = cliOptions.filenames;
|
||||||
|
|
||||||
async function write(src, base) {
|
async function write(src: string, base: string): Promise<boolean> {
|
||||||
let relative = path.relative(base, src);
|
let relative = path.relative(base, src);
|
||||||
|
|
||||||
if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
|
if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
|
||||||
@ -65,14 +71,14 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDest(filename, base) {
|
function getDest(filename: string, base: string): string {
|
||||||
if (cliOptions.relative) {
|
if (cliOptions.relative) {
|
||||||
return path.join(base, cliOptions.outDir, filename);
|
return path.join(base, cliOptions.outDir, filename);
|
||||||
}
|
}
|
||||||
return path.join(cliOptions.outDir, filename);
|
return path.join(cliOptions.outDir, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleFile(src, base) {
|
async function handleFile(src: string, base: string): Promise<boolean> {
|
||||||
const written = await write(src, base);
|
const written = await write(src, base);
|
||||||
|
|
||||||
if (!written && cliOptions.copyFiles) {
|
if (!written && cliOptions.copyFiles) {
|
||||||
@ -84,7 +90,7 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handle(filenameOrDir) {
|
async function handle(filenameOrDir: string): Promise<number> {
|
||||||
if (!fs.existsSync(filenameOrDir)) return 0;
|
if (!fs.existsSync(filenameOrDir)) return 0;
|
||||||
|
|
||||||
const stat = fs.statSync(filenameOrDir);
|
const stat = fs.statSync(filenameOrDir);
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
import convertSourceMap from "convert-source-map";
|
import convertSourceMap from "convert-source-map";
|
||||||
import defaults from "lodash/defaults";
|
import defaults from "lodash/defaults";
|
||||||
import sourceMap from "source-map";
|
import sourceMap from "source-map";
|
||||||
@ -6,9 +8,18 @@ import path from "path";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
import * as util from "./util";
|
import * as util from "./util";
|
||||||
|
import { type CmdOptions } from "./options";
|
||||||
|
|
||||||
export default async function({ cliOptions, babelOptions }) {
|
type CompilationOutput = {
|
||||||
function buildResult(fileResults) {
|
code: string,
|
||||||
|
map: Object,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function({
|
||||||
|
cliOptions,
|
||||||
|
babelOptions,
|
||||||
|
}: CmdOptions): Promise<void> {
|
||||||
|
function buildResult(fileResults: Array<Object>): CompilationOutput {
|
||||||
const map = new sourceMap.SourceMapGenerator({
|
const map = new sourceMap.SourceMapGenerator({
|
||||||
file:
|
file:
|
||||||
cliOptions.sourceMapTarget ||
|
cliOptions.sourceMapTarget ||
|
||||||
@ -74,7 +85,7 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function output(fileResults) {
|
function output(fileResults: Array<string>): void {
|
||||||
const result = buildResult(fileResults);
|
const result = buildResult(fileResults);
|
||||||
|
|
||||||
if (cliOptions.outFile) {
|
if (cliOptions.outFile) {
|
||||||
@ -91,25 +102,28 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function readStdin() {
|
function readStdin(): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise(
|
||||||
let code = "";
|
(resolve: Function, reject: Function): void => {
|
||||||
|
let code = "";
|
||||||
|
|
||||||
process.stdin.setEncoding("utf8");
|
process.stdin.setEncoding("utf8");
|
||||||
|
|
||||||
process.stdin.on("readable", function() {
|
process.stdin.on("readable", function() {
|
||||||
const chunk = process.stdin.read();
|
const chunk = process.stdin.read();
|
||||||
if (chunk !== null) code += chunk;
|
// $FlowIgnore
|
||||||
});
|
if (chunk !== null) code += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
process.stdin.on("end", function() {
|
process.stdin.on("end", function() {
|
||||||
resolve(code);
|
resolve(code);
|
||||||
});
|
});
|
||||||
process.stdin.on("error", reject);
|
process.stdin.on("error", reject);
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function stdin() {
|
async function stdin(): Promise<void> {
|
||||||
const code = await readStdin();
|
const code = await readStdin();
|
||||||
|
|
||||||
const res = await util.transform(
|
const res = await util.transform(
|
||||||
@ -126,7 +140,7 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
output([res]);
|
output([res]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function walk(filenames) {
|
async function walk(filenames: Array<string>): Promise<void> {
|
||||||
const _filenames = [];
|
const _filenames = [];
|
||||||
|
|
||||||
filenames.forEach(function(filename) {
|
filenames.forEach(function(filename) {
|
||||||
@ -151,7 +165,7 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
_filenames.map(async function(filename) {
|
_filenames.map(async function(filename: string): Promise<Object> {
|
||||||
let sourceFilename = filename;
|
let sourceFilename = filename;
|
||||||
if (cliOptions.outFile) {
|
if (cliOptions.outFile) {
|
||||||
sourceFilename = path.relative(
|
sourceFilename = path.relative(
|
||||||
@ -168,7 +182,7 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
{
|
{
|
||||||
sourceFileName: sourceFilename,
|
sourceFileName: sourceFilename,
|
||||||
// Since we're compiling everything to be merged together,
|
// Since we're compiling everything to be merged together,
|
||||||
// "inline" applies to the final output file, but to the individual
|
// "inline" applies to the final output file, but not to the individual
|
||||||
// files being concatenated.
|
// files being concatenated.
|
||||||
sourceMaps:
|
sourceMaps:
|
||||||
babelOptions.sourceMaps === "inline"
|
babelOptions.sourceMaps === "inline"
|
||||||
@ -192,7 +206,7 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
output(results);
|
output(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function files(filenames) {
|
async function files(filenames: Array<string>): Promise<void> {
|
||||||
if (!cliOptions.skipInitialBuild) {
|
if (!cliOptions.skipInitialBuild) {
|
||||||
await walk(filenames);
|
await walk(filenames);
|
||||||
}
|
}
|
||||||
@ -208,7 +222,7 @@ export default async function({ cliOptions, babelOptions }) {
|
|||||||
pollInterval: 10,
|
pollInterval: 10,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.on("all", function(type, filename) {
|
.on("all", function(type: string, filename: string) {
|
||||||
if (!util.isCompilableExtension(filename, cliOptions.extensions)) {
|
if (!util.isCompilableExtension(filename, cliOptions.extensions)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
import commander from "commander";
|
import commander from "commander";
|
||||||
@ -151,7 +153,12 @@ commander.option(
|
|||||||
commander.version(pkg.version + " (@babel/core " + version + ")");
|
commander.version(pkg.version + " (@babel/core " + version + ")");
|
||||||
commander.usage("[options] <files ...>");
|
commander.usage("[options] <files ...>");
|
||||||
|
|
||||||
export default function parseArgv(args: Array<string>) {
|
export type CmdOptions = {
|
||||||
|
babelOptions: Object,
|
||||||
|
cliOptions: Object,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function parseArgv(args: Array<string>): CmdOptions {
|
||||||
//
|
//
|
||||||
commander.parse(args);
|
commander.parse(args);
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
import readdirRecursive from "fs-readdir-recursive";
|
import readdirRecursive from "fs-readdir-recursive";
|
||||||
import * as babel from "@babel/core";
|
import * as babel from "@babel/core";
|
||||||
import includes from "lodash/includes";
|
import includes from "lodash/includes";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
export function chmod(src, dest) {
|
export function chmod(src: string, dest: string): void {
|
||||||
fs.chmodSync(dest, fs.statSync(src).mode);
|
fs.chmodSync(dest, fs.statSync(src).mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,8 +15,8 @@ type ReaddirFilter = (filename: string) => boolean;
|
|||||||
export function readdir(
|
export function readdir(
|
||||||
dirname: string,
|
dirname: string,
|
||||||
includeDotfiles: boolean,
|
includeDotfiles: boolean,
|
||||||
filter: ReaddirFilter,
|
filter?: ReaddirFilter,
|
||||||
) {
|
): Array<string> {
|
||||||
return readdirRecursive(dirname, (filename, _index, currentDirectory) => {
|
return readdirRecursive(dirname, (filename, _index, currentDirectory) => {
|
||||||
const stat = fs.statSync(path.join(currentDirectory, filename));
|
const stat = fs.statSync(path.join(currentDirectory, filename));
|
||||||
|
|
||||||
@ -30,7 +32,7 @@ export function readdirForCompilable(
|
|||||||
dirname: string,
|
dirname: string,
|
||||||
includeDotfiles: boolean,
|
includeDotfiles: boolean,
|
||||||
altExts?: Array<string>,
|
altExts?: Array<string>,
|
||||||
) {
|
): Array<string> {
|
||||||
return readdir(dirname, includeDotfiles, function(filename) {
|
return readdir(dirname, includeDotfiles, function(filename) {
|
||||||
return isCompilableExtension(filename, altExts);
|
return isCompilableExtension(filename, altExts);
|
||||||
});
|
});
|
||||||
@ -48,7 +50,7 @@ export function isCompilableExtension(
|
|||||||
return includes(exts, ext);
|
return includes(exts, ext);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addSourceMappingUrl(code, loc) {
|
export function addSourceMappingUrl(code: string, loc: string): string {
|
||||||
return code + "\n//# sourceMappingURL=" + path.basename(loc);
|
return code + "\n//# sourceMappingURL=" + path.basename(loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +58,11 @@ const CALLER = {
|
|||||||
name: "@babel/cli",
|
name: "@babel/cli",
|
||||||
};
|
};
|
||||||
|
|
||||||
export function transform(filename, code, opts) {
|
export function transform(
|
||||||
|
filename: string,
|
||||||
|
code: string,
|
||||||
|
opts: Object,
|
||||||
|
): Promise<Object> {
|
||||||
opts = {
|
opts = {
|
||||||
...opts,
|
...opts,
|
||||||
caller: CALLER,
|
caller: CALLER,
|
||||||
@ -71,7 +77,10 @@ export function transform(filename, code, opts) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function compile(filename, opts) {
|
export function compile(
|
||||||
|
filename: string,
|
||||||
|
opts: Object | Function,
|
||||||
|
): Promise<Object> {
|
||||||
opts = {
|
opts = {
|
||||||
...opts,
|
...opts,
|
||||||
caller: CALLER,
|
caller: CALLER,
|
||||||
@ -85,7 +94,7 @@ export function compile(filename, opts) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deleteDir(path) {
|
export function deleteDir(path: string): void {
|
||||||
if (fs.existsSync(path)) {
|
if (fs.existsSync(path)) {
|
||||||
fs.readdirSync(path).forEach(function(file) {
|
fs.readdirSync(path).forEach(function(file) {
|
||||||
const curPath = path + "/" + file;
|
const curPath = path + "/" + file;
|
||||||
@ -106,7 +115,7 @@ process.on("uncaughtException", function(err) {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
export function requireChokidar() {
|
export function requireChokidar(): Object {
|
||||||
try {
|
try {
|
||||||
return require("chokidar");
|
return require("chokidar");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -118,7 +127,10 @@ export function requireChokidar() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function adjustRelative(relative, keepFileExtension) {
|
export function adjustRelative(
|
||||||
|
relative: string,
|
||||||
|
keepFileExtension: boolean,
|
||||||
|
): string {
|
||||||
if (keepFileExtension) {
|
if (keepFileExtension) {
|
||||||
return relative;
|
return relative;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user