-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(uniquely/soffit-site-pwa): build with esbuild
- Loading branch information
1 parent
23924bb
commit a9fd707
Showing
37 changed files
with
168 additions
and
2,276 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-undef */ | ||
/* eslint-disable @typescript-eslint/explicit-function-return-type */ | ||
|
||
import {promises as fs, existsSync} from 'node:fs'; | ||
|
||
import {createLogger} from '@alwatr/logger'; | ||
import * as esbuild from 'esbuild'; | ||
|
||
import packageJson from './package.json' assert {type: 'json'}; | ||
|
||
const logger = createLogger('alwatr-pwa-build'); | ||
const banner = '/* ..:: Alwatr UI Demo ::.. */\n'; | ||
|
||
const srcDir = 'src'; | ||
const resDir = 'res'; | ||
const outDir = 'dist'; | ||
const srcFilename = 'alwatr-pwa'; | ||
|
||
const cleanMode = process.argv.includes('--clean'); | ||
const watchMode = process.argv.includes('--watch'); | ||
const debugMode = process.argv.includes('--debug'); | ||
|
||
logger.logOther(banner); | ||
|
||
logger.logProperty('cleanMode', cleanMode); | ||
logger.logProperty('watchMode', watchMode); | ||
logger.logProperty('debugMode', debugMode); | ||
|
||
if (cleanMode) { | ||
logger.logMethod('clean build'); | ||
await fs.rm(outDir, {recursive: true, force: true}); | ||
} | ||
|
||
const copyPromise = fs.cp(resDir, outDir, {recursive: true, force: true, verbatimSymlinks: true}); | ||
|
||
const esBuild = esbuild.build({ | ||
entryPoints: [`${srcDir}/${srcFilename}.ts`], | ||
|
||
logLevel: 'info', | ||
platform: 'browser', | ||
target: 'es2018', | ||
format: 'esm', | ||
conditions: debugMode ? ['development'] : undefined, | ||
|
||
minify: true, | ||
treeShaking: true, | ||
sourcemap: true, | ||
sourcesContent: debugMode, | ||
bundle: true, | ||
splitting: true, | ||
charset: 'ascii', | ||
legalComments: 'none', | ||
metafile: true, | ||
|
||
define: { | ||
_ALWATR_VERSION_: `'${packageJson.version}'`, | ||
}, | ||
// drop: ['debugger'], | ||
|
||
loader: { | ||
'.png': 'copy', | ||
'.jpg': 'copy', | ||
'.woff': 'copy', | ||
'.woff2': 'copy', | ||
}, | ||
|
||
banner: { | ||
js: banner, | ||
css: banner, | ||
}, | ||
|
||
outbase: srcDir, | ||
outdir: outDir, | ||
assetNames: 'asset/[name]-[hash]', | ||
entryNames: watchMode ? '[name]' : '[dir]/[name]-[hash]', | ||
chunkNames: 'chunks/[name]-[hash]', | ||
|
||
incremental: watchMode, | ||
watch: watchMode, | ||
}); | ||
|
||
async function makeHtml() { | ||
logger.logMethod('makeHtml'); | ||
|
||
let htmlContent = await fs.readFile(`${resDir}/index.html`, {encoding: 'utf-8'}); | ||
|
||
const metafile = (await esBuild).metafile; | ||
const outFiles = Object.keys(metafile.outputs); | ||
|
||
const jsFilename = outFiles | ||
.find((filename) => filename.includes(srcFilename) && filename.endsWith('.js')) | ||
.substring(outDir.length + 1); | ||
|
||
const cssFilename = outFiles | ||
.find((filename) => filename.includes(srcFilename) && filename.endsWith('.css')) | ||
.substring(outDir.length + 1); | ||
|
||
logger.logProperty('jsFilename', jsFilename); | ||
logger.logProperty('cssFilename', cssFilename); | ||
|
||
if (!existsSync(`${outDir}/${jsFilename}`)) { | ||
logger.error('makeHtml', 'js_filename_not_found', {jsFilename}); | ||
throw new Error('js_filename_not_found'); | ||
} | ||
|
||
if (!existsSync(`${outDir}/${cssFilename}`)) { | ||
logger.error('makeHtml', 'css_filename_not_found', {cssFilename}); | ||
throw new Error('css_filename_not_found'); | ||
} | ||
|
||
htmlContent = htmlContent | ||
.replace('alwatr-pwa.css', cssFilename) | ||
.replace('alwatr-pwa.js', jsFilename) | ||
; | ||
|
||
await copyPromise; // wait to cp done | ||
await fs.writeFile(`${outDir}/index.html`, htmlContent, {encoding: 'utf-8', flag: 'w'}); | ||
} | ||
|
||
if (!watchMode) { | ||
makeHtml(); | ||
} | ||
|
||
if (debugMode) { | ||
console.log(await esbuild.analyzeMetafile((await esBuild).metafile)); | ||
} | ||
|
||
/* | ||
TODO: | ||
- Assets hash | ||
- PostCSS css file | ||
- lit css loader | ||
- PostCSS lit internal styles | ||
- Dynamic from @alwatr/build | ||
- sideEffects | ||
https://esbuild.github.io/api/#ignore-annotations | ||
https://webpack.js.org/guides/tree-shaking/ | ||
- readme | ||
https://esbuild.github.io/api/#write | ||
https://github.com/fakundo/esbuild-plugin-replace-regex/blob/master/src/index.js#L30 | ||
https://github.com/zandaqo/esbuild-plugin-lit/blob/master/css-loader.ts | ||
https://github.com/chialab/rna/tree/main/packages/esbuild-plugin-html | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,8 @@ | |
<meta charset="utf-8" /> | ||
<title>بازرگانی سافیت</title> | ||
|
||
<link | ||
crossorigin | ||
media="print" | ||
rel="stylesheet" | ||
href="https://cdn.jsdelivr.net/npm/@alwatr/[email protected]/vazirmatn.min.css" | ||
onload="this.media='all';this.onload=null;" | ||
/> | ||
<link rel="stylesheet" href="style/index.css" /> | ||
<link rel="stylesheet" href="alwatr-pwa.css" /> | ||
<script type="module" src="alwatr-pwa.js"></script> | ||
|
||
<meta name="color-scheme" content="light dark" /> | ||
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#673ab7" /> | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.