-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.js
100 lines (96 loc) · 2.71 KB
/
esbuild.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
import esbuild from 'esbuild'
import { open, readdir, mkdir } from 'node:fs/promises'
import { existsSync } from 'node:fs'
// Variables
const srcDir = 'src/test'
const outDir = 'test'
const styleDir = 'styles'
const frameworks = [
{
name: 'ml',
projections: ['3857'] // Only create test files for the given projection.
},
{
name: 'ol' // If no projections array, create test files for all projections.
}
]
// Helper functions
async function readHTML(file) {
let filehandle
let html = ''
try {
filehandle = await open(file, 'r+')
filehandle.readFile('utf8').then(function(contents) {
html += contents
})
} catch (error) {
console.error('there was an error:', error.message)
} finally {
await filehandle?.close()
return html
}
}
async function writeHTML(file, html) {
let filehandle
try {
filehandle = await open(file, 'w')
filehandle.writeFile(html, 'utf8')
} catch (error) {
console.error('there was an error:', error.message)
} finally {
await filehandle?.close()
}
}
// Start building html files
console.log('---------------------')
console.log('Building test HTML')
for(const framework of frameworks) {
const templateHtml = await readHTML(`${srcDir}/${framework.name}.html`)
const topFolders = await readdir(styleDir)
for(const topFolder of topFolders) {
const files = await readdir(`${styleDir}/${topFolder}/`)
for(const file of files) {
const projection = file.match(/^[^_]+/)[0]
if (!framework.projections || framework.projections.includes(projection)) {
try {
const filePath = `/${styleDir}/${topFolder}//${file}`
const title = `${framework.name}_${file}`
const dir = `${outDir}/${framework.name}/${topFolder}`
const content = templateHtml
.replace('InsertYourStylefileHere', filePath)
.replace('InsertYourTitleHere', title)
.replace('InsertYourProjectionHere', projection)
if (!await existsSync(dir)){
await mkdir(dir, { recursive: true })
}
await writeHTML(`${dir}/${file}.html`, content)
} catch (err) {
console.error(err)
}
}
}
}
}
// TODO: copy styles, glyphs and sprites to the test folder.
// Serve the test pages
console.log('---------------------')
console.log('Running test server')
esbuild.context({
entryPoints: {
'ol/main': `${srcDir}/ol.js`,
'ol/style': `${srcDir}/ol.css`,
'ml/main': `${srcDir}/ml.js`,
'ml/style': `${srcDir}/ml.css`,
},
outdir: outDir,
bundle: true,
splitting: true,
format: 'esm'
})
.then((result) => {
result.serve({
servedir: './',
}).then(({ host, port }) => {
console.log('Serving at localhost:' + port)
})
})