-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·71 lines (66 loc) · 2.38 KB
/
index.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
#!/usr/bin/env node
import path from "path";
import { promises as fs } from "fs";
import serve from "./src/serve.js";
import build from "./src/build.js";
import watch from "./src/watch.js";
import defaultVueTransformer from "./src/transformers/vue/vue.js";
import defaultTsxTransformer from "./src/transformers/ts-jsx-tsx/ts-jsx-tsx.js";
const esdevConfigPath = path.join(path.resolve(), "esdev.config.js");
fs.access(esdevConfigPath)
.catch(() => ({}))
.then(async (module) => module ?? (await import(esdevConfigPath)).default)
.then(
async ({
outputDir = path.join(path.resolve(), "./build/"),
inputDir = path.join(path.resolve(), "./"),
transformers: inputTransformers = {},
} = {}) => {
const transformers = {
...defaultTsxTransformer,
...defaultVueTransformer,
...inputTransformers,
};
console.log(`Available transformers: ${Object.keys(transformers)}`);
const [command] = process.argv.slice(2);
const actions = {
watch: () =>
watch({ inputDir, outputDir }, (event, file) => {
const fileExtension = path.extname(file).substring(1);
if (fileExtension in transformers) {
build({ outputDir, inputDir, transformers }).then(() =>
console.log("Build Succeed.")
);
}
}),
build: () =>
build({ outputDir, inputDir, transformers }).then(() =>
console.log(
"Succeed.\n\nDon't forget to add to your index.html file this line:" +
`\n<script type="importmap" src="${path.join(
path.relative(path.resolve(), outputDir),
"build-import-map.json"
)}"></script>`
)
),
serve: () => {
serve({ transformers });
watch({ inputDir, outputDir }, (event, file) => {
const fileExtension = path.extname(file).substring(1);
if (fileExtension in transformers) {
build({ outputDir, inputDir, transformers }).then(() =>
console.log("Build Succeed.")
);
}
});
},
};
if (command in actions) {
return actions[command]();
} else
throw Error(
`${command} ¬¬ Supported Commands: ${Object.keys(actions)}`
);
}
)
.catch((err) => console.error(err));