UNPKG

4.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const cli_utils_1 = require("@design-systems/cli-utils");
5const path_1 = tslib_1.__importDefault(require("path"));
6const babel = tslib_1.__importStar(require("@babel/core"));
7const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
8const utils_1 = require("./utils");
9exports.BABEL_CONFIG = path_1.default.join(__dirname, './configs/babel.config.js');
10const logger = cli_utils_1.createLogger({ scope: 'build' });
11/** Write a babel compilation result to the file system. */
12async function write(result, dest) {
13 if (!result) {
14 return;
15 }
16 let { code } = result;
17 if (result.map) {
18 await fs_extra_1.default.outputJson(`${dest}.map`, result.map);
19 code = `${code}\n//# sourceMappingURL=${path_1.default.basename(dest)}.map`;
20 }
21 await fs_extra_1.default.outputFile(dest, code);
22}
23/** Transpiled the file to all of the supported formats. */
24function getJSOutputFiles(inFile, inDir, outDir) {
25 const ext = path_1.default.extname(inFile);
26 const base = path_1.default.basename(inFile, ext);
27 const cjsFile = path_1.default.join(utils_1.getOutPath(inDir, inFile, path_1.default.join(outDir, 'cjs')), `${base}.js`);
28 const mjsFile = path_1.default.join(utils_1.getOutPath(inDir, inFile, path_1.default.join(outDir, 'esm')), `${base}.js`);
29 return {
30 cjsFile,
31 mjsFile
32 };
33}
34exports.getJSOutputFiles = getJSOutputFiles;
35/** Load @design-systems/cli's default babel.config.js */
36function getBabelConfig(filename, envName, configFile = exports.BABEL_CONFIG) {
37 const babelConfig = babel.loadPartialConfig({
38 configFile,
39 filename,
40 envName
41 });
42 if (!babelConfig) {
43 throw new Error('Could not find babel config!');
44 }
45 return babelConfig;
46}
47exports.getBabelConfig = getBabelConfig;
48/** Load the options (cli's + user) for babel. */
49function getBabelOptions(filename, envName, configFile = exports.BABEL_CONFIG) {
50 const { options } = getBabelConfig(filename, envName, configFile);
51 const customConfigPath = path_1.default.join(cli_utils_1.getMonorepoRoot(), 'babel.config.js');
52 const customConfig = fs_extra_1.default.existsSync(customConfigPath)
53 ? customConfigPath
54 : undefined;
55 return Object.assign(Object.assign({}, options), { configFile: customConfig, babelrc: true });
56}
57exports.getBabelOptions = getBabelOptions;
58/** Transpile code using babel for a specific environment. */
59async function transpileForEnv(filename, envName, configFile) {
60 const options = getBabelOptions(filename, envName, configFile);
61 return babel.transformFileAsync(filename, Object.assign(Object.assign({}, options), { envName }));
62}
63/**
64 * Transpile a JS file using babel.
65 * Write the CJS and ESM versions to disk.
66 */
67async function transpile(inFile, inDir, outDir, configFile) {
68 const { cjsFile, mjsFile } = getJSOutputFiles(inFile, inDir, outDir);
69 const filename = path_1.default.resolve(inFile);
70 try {
71 const [cjsOut, mjsOut] = await Promise.all([
72 transpileForEnv(filename, 'commonjs', configFile),
73 transpileForEnv(filename, 'module', configFile)
74 ]);
75 await Promise.all([write(cjsOut, cjsFile), write(mjsOut, mjsFile)]);
76 }
77 catch (error) {
78 const parts = error.message.match(/^(\S+): (.+) \((\d+):(\d+)\)([\S\s]*)/);
79 if (parts) {
80 logger.error('Transpilation failed');
81 // eslint-disable-next-line no-console
82 console.log(utils_1.formatError({
83 file: parts[1],
84 line: parts[3],
85 column: parts[4],
86 tool: 'BABEL',
87 message: parts[2],
88 code: parts[5]
89 }));
90 }
91 else {
92 logger.error(error.message);
93 }
94 logger.trace(error.stack);
95 return { success: false };
96 }
97 return { success: true };
98}
99exports.default = transpile;
100//# sourceMappingURL=babel.js.map
\No newline at end of file