MCPcopy Create free account
hub / github.com/babel/babel / write

Function write

packages/babel-cli/src/babel/dir.ts:25–86  ·  view source on GitHub ↗
(
    src: string,
    base: string,
  )

Source from the content-addressed store, hash-verified

23 babelOptions,
24}: CmdOptions): Promise<void> {
25 async function write(
26 src: string,
27 base: string,
28 ): Promise<keyof typeof FILE_TYPE> {
29 let relative = path.relative(base, src);
30
31 if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
32 return FILE_TYPE.NON_COMPILABLE;
33 }
34
35 relative = util.withExtension(
36 relative,
37 cliOptions.keepFileExtension
38 ? path.extname(relative)
39 : cliOptions.outFileExtension,
40 );
41
42 const dest = getDest(relative, base);
43
44 try {
45 const res = await util.compile(src, {
46 ...babelOptions,
47 sourceFileName: slash(path.relative(dest + "/..", src)),
48 });
49
50 if (!res) return FILE_TYPE.IGNORED;
51
52 if (res.map) {
53 let outputMap: "both" | "external" | false = false;
54 if (babelOptions.sourceMaps && babelOptions.sourceMaps !== "inline") {
55 outputMap = "external";
56 } else if (babelOptions.sourceMaps == null) {
57 outputMap = util.hasDataSourcemap(res.code!) ? "external" : "both";
58 }
59
60 if (outputMap) {
61 const mapLoc = dest + ".map";
62 if (outputMap === "external") {
63 res.code = util.addSourceMappingUrl(res.code!, mapLoc);
64 }
65 res.map.file = path.basename(relative);
66 outputFileSync(mapLoc, JSON.stringify(res.map));
67 }
68 }
69
70 outputFileSync(dest, res.code!);
71 util.chmod(src, dest);
72
73 if (cliOptions.verbose) {
74 console.log(path.relative(process.cwd(), src) + " -> " + dest);
75 }
76
77 return FILE_TYPE.COMPILED;
78 } catch (err) {
79 if (cliOptions.watch) {
80 console.error(err);
81 return FILE_TYPE.ERR_COMPILATION;
82 }

Callers 3

handleFileFunction · 0.70
exec.jsFile · 0.50
exec.jsFile · 0.50

Calls 2

getDestFunction · 0.85
outputFileSyncFunction · 0.70

Tested by

no test coverage detected