-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
131 lines (115 loc) · 2.99 KB
/
build.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
import {env} from 'node:process';
import {format, parse} from 'node:path';
import {promises} from 'node:fs';
import esbuildSvelte from 'esbuild-svelte';
import sveltePreprocess from 'svelte-preprocess';
import esbuild from 'esbuild';
const inlineStylePlugin = {
name: 'inlineStyle',
setup(build) {
const options = build.initialOptions;
build.onLoad({filter: /\.html$/}, async args => {
const styleTagRegex = /<\/style>/gi;
const headTagRegex = /<\/head>/gi;
const bodyTagRegex = /<\/body>/gi;
const htmlPathObject = parse(args.path);
const cssPath = format(Object.assign({},
htmlPathObject,
{ext: '.css', base: undefined},
));
const jsPath = format(Object.assign({},
htmlPathObject,
{ext: '.js', base: undefined},
));
let fileContents = await promises.readFile(args.path);
const htmlSource = fileContents.toString();
let jsSource = '';
let cssSource = '';
try {
try {
fileContents = await promises.readFile(cssPath);
cssSource = fileContents.toString();
} catch {
const fallbackCssPath = format({
dir: options.outdir,
name: htmlPathObject.name,
ext: '.css',
});
fileContents = await promises.readFile(fallbackCssPath);
cssSource = fileContents.toString();
}
let newHtmlSource = htmlSource.replace(
styleTagRegex,
`${cssSource}\n</style>`,
);
if (htmlSource.length === newHtmlSource.length) {
newHtmlSource = htmlSource.replace(
headTagRegex,
`<style>\n${cssSource}\n</style>\n</head>`,
);
}
try {
fileContents = await promises.readFile(jsPath);
jsSource = fileContents.toString();
} catch (error) { // eslint-disable-line no-unused-vars, unicorn/prefer-optional-catch-binding
const fallbackJsPath = format({
dir: options.outdir,
name: htmlPathObject.name,
ext: '.js',
});
try {
fileContents = await promises.readFile(fallbackJsPath);
jsSource = fileContents.toString();
} catch {}
}
if (jsSource.length > 0) {
newHtmlSource = newHtmlSource.replace(
bodyTagRegex,
`<script>\n${jsSource}\n</script>\n</body>`,
);
}
return {contents: newHtmlSource, loader: 'text'};
} catch {}
});
},
};
const defaults = {
target: ['es6'],
logLevel: 'info',
bundle: true,
outdir: 'dist/',
minify: env.NODE_ENV !== 'dev',
sourcemap: env.NODE_ENV === 'dev',
};
esbuild
.build(Object.assign({}, defaults, {
entryPoints: ['lib/ui.js'],
platform: 'browser',
plugins: [
esbuildSvelte({
preprocess: sveltePreprocess,
compilerOptions: {
preserveComments: false,
preserveWhitespace: false,
dev: env.NODE_ENV === 'dev',
},
}),
],
}))
.catch(() => {
throw new Error('Building ui.js failed');
});
esbuild
.build(Object.assign({}, defaults, {
entryPoints: ['code.ts'],
platform: 'neutral',
loader: {
'.html': 'text',
},
plugins: [
inlineStylePlugin,
],
}))
.catch(() => {
throw new Error('Building code.js failed');
});