UNPKG

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