Repository navigation
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
125 lines (112 loc) · 2.72 KB
/
Copy pathesbuild.config.mjs
File metadata and controls
125 lines (112 loc) · 2.72 KB
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
import { build } from 'esbuild'
import { readFileSync, rmSync, mkdirSync, copyFileSync, existsSync } from 'node:fs'
/** @type {import('esbuild').BuildOptions} */
const common = {
bundle: true,
sourcemap: false,
logLevel: 'info',
}
const pkg = JSON.parse(readFileSync('./package.json', 'utf8'))
const version = pkg.version || '0.0.0'
const banner = {
js: `/*
* zumly
* v.${version}
* Author Juan Martin Muda - Zumerlab
* License MIT
*/`,
}
/**
* 1) LEGACY build for <script> tag:
* dist/zumly.js
*
* format:'iife' is REQUIRED here: without it the minified top-level vars
* (Xt, kt, S, P, …) leak into global scope and collide with any classic
* <script> on the page that declares a one-letter identifier
* (SyntaxError: Identifier already declared). entry.js assigns
* window.Zumly itself, so no globalName is needed.
*/
async function buildLegacy () {
await build({
...common,
entryPoints: ['src/entry.js'],
outfile: 'dist/zumly.js',
format: 'iife',
platform: 'browser',
minify: true,
target: ['es2020'],
banner,
})
}
/**
* 2) ESM build:
* dist/zumly.mjs
*/
async function buildESM () {
await build({
...common,
entryPoints: ['src/entry-esm.js'],
outfile: 'dist/zumly.mjs',
format: 'esm',
minify: true,
splitting: false,
banner,
})
}
/**
* 2b) CJS build for require('zumly') in Node:
* dist/zumly.cjs
*/
async function buildCJS () {
await build({
...common,
entryPoints: ['src/entry-esm.js'],
outfile: 'dist/zumly.cjs',
format: 'cjs',
platform: 'browser',
minify: true,
banner,
})
}
/**
* 3) CSS build:
* dist/zumly.css (+ dist/zumly.min.css)
*/
async function buildCSS () {
await build({
bundle: true,
sourcemap: false,
logLevel: 'info',
entryPoints: ['src/style.css'],
outfile: 'dist/zumly.css',
minify: false,
})
await build({
bundle: true,
sourcemap: false,
logLevel: 'info',
entryPoints: ['src/style.css'],
outfile: 'dist/zumly.min.css',
minify: true,
})
}
async function copyToPublicDist () {
const publicDist = 'public/dist'
try { rmSync(publicDist, { recursive: true, force: true }) } catch { /* ok */ }
mkdirSync(publicDist, { recursive: true })
const files = ['zumly.js', 'zumly.mjs', 'zumly.css', 'zumly.min.css']
for (const f of files) {
const src = `dist/${f}`
if (existsSync(src)) copyFileSync(src, `${publicDist}/${f}`)
}
}
async function main () {
try { rmSync('dist', { recursive: true, force: true }) } catch { /* ok */ }
await Promise.all([buildLegacy(), buildESM(), buildCJS(), buildCSS()])
await copyToPublicDist()
}
main().catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})