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