|
| 1 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 2 | +/* eslint-disable no-console */ |
| 3 | +import path from 'path' |
| 4 | +import fs from 'fs' |
| 5 | +import { fileURLToPath } from 'url' |
| 6 | +import dotenv from 'dotenv' |
| 7 | + |
| 8 | +import { build } from 'esbuild' |
| 9 | +import { htmlPlugin } from '@craftamap/esbuild-plugin-html' |
| 10 | +import esbuildMxnCopy from 'esbuild-plugin-mxn-copy' |
| 11 | +import aliasPlugin from 'esbuild-plugin-path-alias' |
| 12 | +import { eslintPlugin } from 'esbuild-plugin-eslinter' |
| 13 | + |
| 14 | +const env = fs.existsSync('.env') ? dotenv.config() : { parsed: {} } |
| 15 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 16 | +const { version } = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'))) |
| 17 | +const isDevelopment = Boolean(process.argv.includes('--dev')) |
| 18 | +const isRelease = Boolean(process.argv.includes('--release')) |
| 19 | + |
| 20 | +if (fs.existsSync(path.resolve(__dirname, 'dist'))) { |
| 21 | + console.log('Cleaning up old build') |
| 22 | + fs.rm(path.resolve(__dirname, 'dist'), { recursive: true }, (err) => { |
| 23 | + if (err) console.log(err) |
| 24 | + }) |
| 25 | +} |
| 26 | + |
| 27 | +const plugins = [ |
| 28 | + htmlPlugin({ |
| 29 | + files: [ |
| 30 | + { |
| 31 | + entryPoints: ['src/index.jsx'], |
| 32 | + filename: 'index.html', |
| 33 | + htmlTemplate: fs.readFileSync('./public/index.template.html'), |
| 34 | + scriptLoading: 'defer', |
| 35 | + favicon: './public/favicon/favicon.ico', |
| 36 | + }, |
| 37 | + ], |
| 38 | + }), |
| 39 | + esbuildMxnCopy({ |
| 40 | + copy: [ |
| 41 | + { from: 'public/images', to: 'dist/' }, |
| 42 | + { from: 'public/locales', to: 'dist/' }, |
| 43 | + ], |
| 44 | + }), |
| 45 | + aliasPlugin({ |
| 46 | + '@components': path.resolve(__dirname, './src/components'), |
| 47 | + '@assets': path.resolve(__dirname, './src/assets'), |
| 48 | + '@hooks': path.resolve(__dirname, './src/hooks'), |
| 49 | + '@services': path.resolve(__dirname, './src/services'), |
| 50 | + }), |
| 51 | +] |
| 52 | + |
| 53 | +if (isDevelopment) { |
| 54 | + plugins.push( |
| 55 | + eslintPlugin(), |
| 56 | + ) |
| 57 | +} else { |
| 58 | + console.log(`Building production version: ${version}`) |
| 59 | +} |
| 60 | + |
| 61 | +try { |
| 62 | + await build({ |
| 63 | + entryPoints: ['src/index.jsx'], |
| 64 | + legalComments: 'none', |
| 65 | + bundle: true, |
| 66 | + outdir: 'dist/', |
| 67 | + publicPath: '/', |
| 68 | + entryNames: isDevelopment ? undefined : '[name].[hash]', |
| 69 | + metafile: true, |
| 70 | + minify: isRelease || !isDevelopment, |
| 71 | + logLevel: isDevelopment ? 'info' : 'error', |
| 72 | + watch: isDevelopment |
| 73 | + ? { |
| 74 | + onRebuild(error) { |
| 75 | + if (error) console.error('Recompiling failed:', error) |
| 76 | + else console.log('Recompiled successfully') |
| 77 | + }, |
| 78 | + } |
| 79 | + : false, |
| 80 | + sourcemap: isRelease || isDevelopment, |
| 81 | + define: { |
| 82 | + inject: JSON.stringify({ |
| 83 | + ...env.parsed, |
| 84 | + VERSION: version, |
| 85 | + DEVELOPMENT: isDevelopment, |
| 86 | + }), |
| 87 | + }, |
| 88 | + plugins, |
| 89 | + }) |
| 90 | +} catch (e) { |
| 91 | + console.error(e) |
| 92 | + process.exit(1) |
| 93 | +} finally { |
| 94 | + console.log('React Map Compiled') |
| 95 | +} |
0 commit comments