-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathbuild.js
executable file
·221 lines (202 loc) · 6.68 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
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
#!/usr/bin/env node
/*
Parchment build script
======================
Copyright (c) 2024 Dannii Willis
MIT licenced
https://github.com/curiousdannii/parchment
*/
import esbuild from 'esbuild'
import esbuildSvelte from 'esbuild-svelte'
import fs from 'fs/promises'
import minimist from 'minimist'
import {sveltePreprocess} from 'svelte-preprocess'
const argv = minimist(process.argv.slice(2))
const projects = argv._
if (projects.length === 0) {
projects.push('tools', 'web')
}
console.log(`Building project${projects.length > 1 ? 's' : ''}: ${projects.join(', ')}`)
const analyse = argv.analyse
const servemode = argv.serve
async function readdir(path) {
return (await fs.readdir(path)).map(file => `${path}/${file}`)
}
// To avoid breaking .min.js files, we'll copy jQuery by itself
function jquery_copier(outdir) {
return {
entryPoints: {
'jquery.min': 'node_modules/jquery/dist/jquery.min.js',
},
loader: {
'.min.js': 'copy',
},
outdir,
}
}
const projects_to_build = []
if (projects.includes('ifcomp')) {
projects_to_build.push({
entryPoints: {
bocfel: 'node_modules/emglken/build/bocfel.*',
glkaudio_bg: 'node_modules/glkaudio/glkaudio_bg.wasm',
glulxe: 'node_modules/emglken/build/glulxe.*',
ie: 'src/common/ie.js',
tads: 'node_modules/emglken/build/tads.*',
waiting: 'src/common/waiting.gif',
web: 'src/common/launcher.ts',
},
outdir: 'dist/ifcomp/interpreter',
sourcemap: true,
},
jquery_copier('dist/ifcomp/interpreter'),
{
entryPoints: ['src/fonts/iosevka/*.woff2'],
outdir: 'dist/fonts/iosevka',
})
}
if (projects.includes('inform7')) {
projects_to_build.push({
entryPoints: {
ie: 'src/common/ie.js',
parchment: 'src/inform7/index.ts',
waiting: 'src/common/waiting.gif',
},
format: 'iife',
logOverride: {'empty-import-meta': 'silent'},
outdir: 'dist/inform7/Parchment',
},
jquery_copier('dist/inform7/Parchment'),
{
entryPoints: ['src/upstream/quixe/media/resourcemap.js'],
loader: {'.js': 'copy'},
outdir: 'dist/inform7/Parchment',
})
}
if (projects.includes('lectrote')) {
projects_to_build.push({
entryPoints: {
git: 'node_modules/emglken/build/git.*',
glulxe: 'node_modules/emglken/build/glulxe.*',
hugo: 'node_modules/emglken/build/hugo.*',
scare: 'node_modules/emglken/build/scare.*',
tads: 'node_modules/emglken/build/tads.*',
},
format: 'cjs',
outdir: 'dist/lectrote',
})
}
if (projects.includes('tools')) {
projects_to_build.push({
entryPoints: {
'inform7-wasm': 'src/tools/inform7-wasm-cli.ts',
'make-single-file': 'src/tools/single-file-cli.ts',
},
minify: false,
outdir: 'tools',
platform: 'node',
treeShaking: true,
}, {
entryPoints: {
'file-exporter': 'src/tools/file-exporter.*',
},
outdir: 'dist/tools',
sourcemap: true,
})
}
if (projects.includes('web')) {
projects_to_build.push({
entryPoints: {
bocfel: 'node_modules/emglken/build/bocfel.*',
git: 'node_modules/emglken/build/git.*',
glkaudio_bg: 'node_modules/glkaudio/glkaudio_bg.wasm',
glulxe: 'node_modules/emglken/build/glulxe.*',
hugo: 'node_modules/emglken/build/hugo.*',
ie: 'src/common/ie.js',
//quixe: 'src/common/quixe.js',
scare: 'node_modules/emglken/build/scare.*',
tads: 'node_modules/emglken/build/tads.*',
waiting: 'src/common/waiting.gif',
web: 'src/common/launcher.ts',
//zvm: 'src/common/zvm.js',
},
outdir: 'dist/web',
sourcemap: true,
},
jquery_copier('dist/web'),
{
entryPoints: ['src/fonts/iosevka/*.woff2'],
outdir: 'dist/fonts/iosevka',
})
}
const common_options = {
bundle: true,
define: {
ENVIRONMENT_IS_NODE: 'false',
ENVIRONMENT_IS_WEB: 'true',
ENVIRONMENT_IS_WORKER: 'false',
},
external: [
'*.woff2',
],
format: 'esm',
loader: {
'.gif': 'copy',
'.html': 'copy',
'.wasm': 'copy',
'.woff2': 'copy',
},
minify: true,
metafile: analyse,
plugins: [
esbuildSvelte({
preprocess: sveltePreprocess(),
}),
{
// Removing the Emscripten ENVIRONMENT_IS_ variables so that the globals defined above will be used instead. Most Node code will be excluded, except that some variables will still be defined
name: 'EmglkenEnvironmentRemover',
setup(build) {
build.onLoad({filter: /emglken\/build\/\w+.js$/}, async (args) => {
const code = await fs.readFile(args.path, 'utf8')
return {
contents: code.replace('var ENVIRONMENT_IS_WEB = typeof window == "object"', '')
.replace('var ENVIRONMENT_IS_WORKER = typeof importScripts == "function"', '')
.replace('var ENVIRONMENT_IS_NODE = typeof process == "object" && typeof process.versions == "object" && typeof process.versions.node == "string" && process.type != "renderer"', '')
}
})
},
},
],
}
let have_given_emglken_warning
for (const project of projects_to_build) {
await fs.mkdir(project.outdir, {recursive: true})
if (project.entryPoints) {
// Warn if not using upstream Emglken
if (!have_given_emglken_warning && Object.values(project.entryPoints).filter(file => file.startsWith('node_modules/emglken')).length > 0) {
have_given_emglken_warning = 1
if (!(await fs.lstat('node_modules/emglken')).isSymbolicLink()) {
console.warn('⚠ Warning: Using npm rather than upstream version of Emglken')
}
}
const options = Object.assign({}, common_options, project)
if (servemode) {
const context = await esbuild.context(options)
await context.watch()
}
else {
const result = await esbuild.build(options)
if (analyse) {
console.log(await esbuild.analyzeMetafile(result.metafile, {verbose: true}))
}
}
}
}
if (servemode) {
const context = await esbuild.context({})
let {host, port} = await context.serve({
port: 8080,
servedir: '.',
})
console.log(`Serving on http://${host}:${port}`)
}