forked from cefiti/cefiti
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.mjs
More file actions
102 lines (92 loc) · 2.44 KB
/
dev.mjs
File metadata and controls
102 lines (92 loc) · 2.44 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
import fs from 'node:fs'
import process from 'node:process'
import esbuild from 'esbuild'
const project =
process.argv.slice(2).find((arg) => !arg.startsWith('--')) || 'cefiti'
const baseDir = `./${project}`
const isBuild = process.argv.includes('--build')
if (!fs.existsSync(baseDir)) {
console.error(`Project directory ${baseDir} does not exist.`)
process.exit(1)
}
const externalDbPlugin = {
name: 'external-db',
setup(build) {
build.onResolve({ filter: /^#db$/ }, () => {
return {
path: 'https://cefiti.web.app/db.js',
external: true,
}
})
build.onResolve({ filter: /^#municipios$/ }, () => {
return {
path: 'https://cefiti.web.app/municipios.js',
external: true,
}
})
build.onResolve({ filter: /^#db-next$/ }, () => {
return {
path: 'https://cefiti.web.app/db-next.js',
external: true,
}
})
build.onResolve({ filter: /^#legislacao$/ }, () => {
return {
path: 'https://cefiti.web.app/legislacao.js',
external: true,
}
})
},
}
const commonConfig = {
bundle: true,
outdir: `${baseDir}/public`,
sourcemap: true,
format: 'esm',
jsx: 'automatic',
target: ['es2017'],
define: {
'process.env.NODE_ENV': isBuild ? '"production"' : '"development"',
},
metafile: true,
charset: 'utf8',
}
const entryPoints = [`${baseDir}/src/index.tsx`]
if (fs.existsSync(`${baseDir}/src/leg.tsx`)) {
entryPoints.push(`${baseDir}/src/leg.tsx`)
}
if (fs.existsSync(`${baseDir}/src/sw.js`)) {
entryPoints.push(`${baseDir}/src/sw.js`)
}
/** @type {esbuild.BuildOptions} */
const appConfig = {
...commonConfig,
entryPoints,
minify: true,
plugins:
['cefiti', 'cefiti-new'].includes(project) && isBuild
? [externalDbPlugin]
: [],
}
if (isBuild) {
try {
await esbuild.build(appConfig)
console.log(`Build for ${project} complete successfully`)
} catch (error) {
console.error(`Build for ${project} failed:`, error)
process.exit(1)
}
} else {
const appContext = await esbuild.context(appConfig).catch((err) => {
console.error(`App context error for ${project}:`, err)
process.exit(1)
})
await appContext.rebuild()
await appContext.watch()
const serveResult = await appContext.serve({
servedir: `${baseDir}/public`,
host: 'localhost',
port: project === 'cefiti' ? 3001 : 3002,
})
console.log(`${project} listening on http://localhost:${serveResult.port}`)
}