1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getBabelOptions = exports.getBabelConfig = exports.getJSOutputFiles = exports.BABEL_CONFIG = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const cli_utils_1 = require("@design-systems/cli-utils");
|
6 | const path_1 = tslib_1.__importDefault(require("path"));
|
7 | const babel = tslib_1.__importStar(require("@babel/core"));
|
8 | const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
9 | const utils_1 = require("./utils");
|
10 | exports.BABEL_CONFIG = path_1.default.join(__dirname, './configs/babel.config.js');
|
11 | const logger = cli_utils_1.createLogger({ scope: 'build' });
|
12 |
|
13 | async 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 |
|
25 | function 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 | }
|
35 | exports.getJSOutputFiles = getJSOutputFiles;
|
36 |
|
37 | function 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 | }
|
48 | exports.getBabelConfig = getBabelConfig;
|
49 |
|
50 | function 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 | }
|
58 | exports.getBabelOptions = getBabelOptions;
|
59 |
|
60 | async 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 |
|
66 |
|
67 |
|
68 | async 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 |
|
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 | }
|
100 | exports.default = transpile;
|
101 |
|
\ | No newline at end of file |