-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
91 lines (85 loc) · 1.87 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import fs from 'fs';
import CFG from './project.cfg.js';
import { findFiles } from '@jam-do/jam-tools/node/index.js';
import esbuild from 'esbuild';
/**
*
* @param {String} dirPath
*/
function checkDir(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, {
recursive: true,
});
}
}
/**
*
* @param {String} path
*/
function fmtPath(path) {
if (path && !path.startsWith('.')) {
path = './' + path;
}
return path;
}
/**
*
* @param {String} path
* @returns {Promise<String>}
*/
async function impWa(path) {
let result = null;
path = fmtPath(path);
if (path.includes('/index.js')) {
let buildResult = esbuild.buildSync({
entryPoints: [path],
format: 'esm',
bundle: true,
minify: true,
sourcemap: false,
// outfile: buildItem.out,
target: 'es2019',
write: false,
});
// console.log('BUILD RESULT:');
// console.log(buildResult.outputFiles[0].text);
result = buildResult.outputFiles[0].text;
} else {
try {
let str = (await import(path)).default;
if (str.constructor === Function) {
str = str();
}
result = str;
} catch (e) {
console.log('WRONG WA PATH: ' + path);
}
}
return result;
}
/**
*
* @param {String} indexPath
*/
async function processIndex(indexPath) {
let indexSrc = await impWa(indexPath);
if (!indexSrc) {
return;
}
let outPath = fmtPath(indexPath);
if (!outPath.includes('index.js')) {
outPath = outPath.replace('.js', '');
}
outPath = outPath.replace(fmtPath(CFG.sourceFolder), fmtPath(CFG.outputFolder));
checkDir(outPath.split('index.')[0]);
fs.writeFileSync(outPath, indexSrc);
}
export function build() {
let indexArr = findFiles(CFG.sourceFolder, ['index.', '.js'], []);
console.log(indexArr);
indexArr.forEach((indexPath) => {
processIndex(indexPath);
});
}
build();