-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
93 lines (77 loc) · 1.89 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
92
93
import esbuild from 'esbuild';
import fs from "fs";
const addToTree = (word, branch) => {
if (word.length <= 1) {
return branch;
}
const char = word[0];
if (!branch[char]) {
branch[char] = {}
}
if (word.length === 2) {
if (!Array.isArray(branch[char])) {
branch[char] = [];
}
branch[char].push(word[1]);
return branch
}
return addToTree(word.substring(1), branch[char]);
}
const convertToTree = (wordlist) => {
const result = {};
wordlist.forEach((word) => {
addToTree(word, result)
})
return result;
}
const exportAsString = (tree) => {
return `let A='A',B='B',C='C',D='D',E='E',F='F',G='G',H='H',I='I',J='J',K='K',L='L',M='M',N='N',O='O',P='P',Q='Q',R='R',S='S',T='T',U='U',V='V',W='W',X='X',Y='Y',Z='Z';
export default ${JSON.stringify(tree).replaceAll('"', '')};`
}
export const wordsLoader = {
name: 'words-loader',
setup(build) {
build.onLoad({ filter: /.txt$/ }, async (args) => {
const allWords = fs.readFileSync(args.path, 'utf-8');
const allTree = convertToTree(allWords.replaceAll(/\r/g,'').split('\n'));
const all = exportAsString(allTree);
return {
contents: all,
loader: 'js',
};
})
}
}
esbuild.build({
entryPoints: ['src/index.js'],
bundle: true,
minify: true,
outfile: 'dist/index.js',
format: 'esm',
plugins: [wordsLoader]
});
esbuild.build({
entryPoints: ['src/processWords.js'],
bundle: true,
minify: true,
outfile: 'build/processWords.js',
format: 'esm',
platform: 'node',
plugins: [wordsLoader]
});
esbuild.build({
entryPoints: ['src/processApprovedWords.js'],
bundle: true,
minify: true,
outfile: 'wordlists/approved.js',
format: 'esm',
plugins: [wordsLoader]
});
esbuild.build({
entryPoints: ['src/processAllWords.js'],
bundle: true,
minify: true,
outfile: 'wordlists/words.js',
format: 'esm',
plugins: [wordsLoader]
});