forked from WatWowMap/ReactMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.mjs
155 lines (146 loc) · 4.57 KB
/
esbuild.config.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
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
/* eslint-disable no-await-in-loop */
/* eslint-disable no-continue */
/* eslint-disable no-restricted-syntax */
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-console */
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
import dotenv from 'dotenv'
import { build as compile } from 'esbuild'
import { htmlPlugin } from '@craftamap/esbuild-plugin-html'
import esbuildMxnCopy from 'esbuild-plugin-mxn-copy'
import aliasPlugin from 'esbuild-plugin-path-alias'
import { eslintPlugin } from 'esbuild-plugin-eslinter'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const env = fs.existsSync(`${__dirname}/.env`) ? dotenv.config() : { parsed: {} }
const { version } = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json')))
const isDevelopment = Boolean(process.argv.includes('--dev'))
const isRelease = Boolean(process.argv.includes('--release'))
const hasCustom = await (async function checkFolders(folder, isCustom = false) {
for (const file of await fs.promises.readdir(folder)) {
if (isCustom) return true
if (file.startsWith('.')) continue
if (!file.includes('.')) isCustom = await checkFolders(`${folder}/${file}`, isCustom)
if (/\.custom.(jsx?|css)$/.test(file)) return true
}
return isCustom
}(path.resolve(__dirname, 'src')))
if (fs.existsSync(path.resolve(__dirname, 'dist'))) {
console.log('[BUILD] Cleaning up old build')
fs.rm(path.resolve(__dirname, 'dist'), { recursive: true }, (err) => {
if (err) console.log(err)
})
}
const plugins = [
htmlPlugin({
files: [
{
entryPoints: ['src/index.jsx'],
filename: 'index.html',
htmlTemplate: fs.readFileSync('./public/index.template.html'),
scriptLoading: 'defer',
favicon: './public/favicon/favicon.ico',
},
],
}),
esbuildMxnCopy({
copy: [
{ from: 'public/images', to: 'dist/' },
{ from: 'public/locales', to: 'dist/' },
],
}),
aliasPlugin({
'@components': path.resolve(__dirname, './src/components'),
'@assets': path.resolve(__dirname, './src/assets'),
'@hooks': path.resolve(__dirname, './src/hooks'),
'@services': path.resolve(__dirname, './src/services'),
}),
]
if (isDevelopment) {
plugins.push(
eslintPlugin(),
)
} else {
if (hasCustom) {
plugins.push(
{
name: 'Custom Loader',
setup(build) {
const customPaths = []
build.onLoad({ filter: /\.(jsx?|css)$/ }, async (args) => {
const isNodeModule = /node_modules/.test(args.path)
if (!isNodeModule) {
const [base, suffix] = args.path.split('.')
const newPath = `${base}.custom.${suffix}`
if (fs.existsSync(newPath)) {
customPaths.push(newPath)
return {
contents: fs.readFileSync(newPath, 'utf8'),
loader: suffix,
watchFiles: isDevelopment ? [newPath] : undefined,
}
}
}
})
build.onEnd(() => {
if (customPaths.length && !isDevelopment) {
console.log(`
======================================================
WARNING:
Custom files aren't officially supported
Be sure to watch for breaking changes!
${customPaths.map((x, i) => ` ${i + 1}. src/${x.split('src/')[1]}`).join('\n')}
======================================================
`)
}
})
},
},
)
}
console.log(`[BUILD] Building production version: ${version}`)
}
try {
await compile({
entryPoints: ['src/index.jsx'],
legalComments: 'none',
bundle: true,
outdir: 'dist/',
publicPath: '/',
entryNames: isDevelopment ? undefined : '[name].[hash]',
metafile: true,
minify: isRelease || !isDevelopment,
logLevel: isDevelopment ? 'info' : 'error',
target: [
'safari11',
'chrome64',
'firefox58',
'edge88',
],
watch: isDevelopment
? {
onRebuild(error) {
if (error) console.error('Recompiling failed:', error)
else console.log('Recompiled successfully')
},
}
: false,
sourcemap: isRelease || isDevelopment,
define: {
inject: JSON.stringify({
...env.parsed,
VERSION: version,
DEVELOPMENT: isDevelopment,
CUSTOM: hasCustom,
LOCALES: await fs.promises.readdir(`${__dirname}/public/locales`),
}),
},
plugins,
})
} catch (e) {
console.error(e)
process.exit(1)
} finally {
console.log('[BUILD] React Map Compiled')
}