-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
esbuild.config.mjs
244 lines (228 loc) · 7.26 KB
/
esbuild.config.mjs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import esbuild from "esbuild";
import process from "process";
import builtins from 'builtin-modules';
import { copy } from 'esbuild-plugin-copy';
import extra from "fs-extra";
import fs from "fs";
import path from "path";
import { sassPlugin } from 'esbuild-sass-plugin';
import { readdir } from "fs";
import { basename } from "path";
const staticAssetsPlugin = {
name: 'static-assets-plugin',
setup(build) {
build.onLoad({ filter: /.+/ }, (args) => {
return {
watchFiles: ['styles.css', 'esbuild.config.mjs'],
};
});
},
};
const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;
const prod = (process.argv[2] === 'production');
const TEST_VAULT = 'test-vault/.obsidian/plugins/obsidian-advanced-slides';
function build() {
esbuild.build({
banner: {
js: banner,
},
entryPoints: ['src/main.ts'],
bundle: true,
external: ['obsidian', 'electron', ...builtins],
format: 'cjs',
minify: prod,
watch: !prod,
target: 'es2020',
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: TEST_VAULT + '/main.js',
plugins: [
staticAssetsPlugin,
copy({
assets: {
from: ['manifest.json', 'styles.css', 'distVersion.json'],
to: ['.'],
}
}),
copy({
assets: {
from: ['src/template/**/*'],
to: ['./template/*']
}
}),
copy({
assets: {
from: ['node_modules/reveal.js/dist/*'],
to: ['./dist/*'],
}
}),
copy({
assets: {
from: ['src/scss/theme/source/fonts/league-gothic/*'],
to: ['./dist/theme/fonts/league-gothic/*'],
}
}),
copy({
assets: {
from: ['src/scss/theme/source/fonts/source-sans-pro/*'],
to: ['./dist/theme/fonts/source-sans-pro/*'],
}
}),
copy({
assets: {
from: ['src/scss/theme/source/fonts/lato/*'],
to: ['./dist/theme/fonts/lato/*'],
}
}),
copy({
assets: {
from: ['node_modules/reveal.js/plugin/**/*'],
to: ['./plugin'],
keepStructure: true
}
}),
copy({
assets: {
from: ['node_modules/mathjax/es5/**/*'],
to: ['./plugin/math/mathjax'],
keepStructure: true
}
}),
copy({
assets: {
from: ['node_modules/reveal.js-mermaid-plugin/plugin/mermaid/*'],
to: ['./plugin/mermaid/*'],
}
}),
copy({
assets: {
from: ['node_modules/highlight.js/styles/vs2015.css'],
to: ['./css/*'],
}
}),
copy({
assets: {
from: ['node_modules/@fortawesome/fontawesome-free/js/all.min.js'],
to: ['./dist/fontawesome/*'],
}
}),
copy({
assets: {
from: ['node_modules/reveal.js-plugins/chalkboard/**/*'],
to: ['./plugin/chalkboard'],
keepStructure: true
}
}),
copy({
assets: {
from: ['node_modules/reveal.js-plugins/customcontrols/*'],
to: ['./plugin/customcontrols/*'],
}
}),
copy({
assets: {
from: ['node_modules/reveal.js-menu/*'],
to: ['./plugin/menu/*'],
}
}),
copy({
assets: {
from: ['node_modules/reveal.js-plugins/chart/**/*'],
to: ['./plugin/chart'],
keepStructure: true
}
}),
copy({
assets: {
from: ['node_modules/chart.js/dist/chart.min.js'],
to: ['./plugin/chart/*'],
}
}),
copy({
assets: {
from: ['node_modules/reveal.js-elapsed-time-bar/plugin/elapsed-time-bar/elapsed-time-bar.js'],
to: ['./plugin/elapsed-time-bar/*'],
}
}),
copy({
assets: {
from: ['node_modules/reveal-pointer/dist/pointer.js'],
to: ['./plugin/reveal-pointer/*'],
}
}),
copy({
assets: {
from: ['node_modules/reveal-pointer/dist/pointer.css'],
to: ['./plugin/reveal-pointer/*'],
}
}),
copy({
assets: prod ? {} : {
from: ['.hotreload'],
to: ['.'],
},
}),
],
}).then(buildScss('src/scss/layout/main.scss', TEST_VAULT + '/css/layout.css'))
.then(buildScss('src/scss/theme/source/mattropolis.scss', TEST_VAULT + '/css/mattropolis.css'))
.then(buildAllThemes('src/scss/theme/source/'))
.catch(() => process.exit(1));
}
function buildAllThemes(themeDir) {
readdir(themeDir, function (err, files) {
files.forEach(function (file) {
if (file.endsWith('.scss')) {
const source = themeDir + file;
const target = TEST_VAULT + '/dist/theme/' + basename(file).replaceAll('.scss', '.css');
buildScss(source, target);
}
});
});
}
function buildScss(source, target) {
return esbuild.build({
entryPoints: [
source,
],
outfile: target,
plugins: [
sassPlugin(),
staticAssetsPlugin
]
});
}
build();
function flatten(lists) {
return lists.reduce((a, b) => a.concat(b), []);
}
function getDirectories(srcpath) {
return fs.readdirSync(srcpath)
.map(file => path.join(srcpath, file))
.filter(path => fs.statSync(path).isDirectory());
}
function getDirectoriesRecursive(srcpath) {
return [srcpath, ...flatten(getDirectories(srcpath).map(getDirectoriesRecursive))];
}
const scssDirs = getDirectoriesRecursive('src/scss');
const templateDirs = getDirectoriesRecursive('src/template');
if (!prod) {
let fsTimeout;
const watchDirs = [...scssDirs, ...templateDirs];
watchDirs.forEach((dirName) => {
extra.watch(dirName, (event, fileName) => {
if (!fsTimeout) {
console.log(`Changes detected in ${dirName}`);
fsTimeout = setTimeout(function () {
fsTimeout = null;
}, 100);
build();
}
});
});
}