Skip to content

Commit

Permalink
feat(pwa): watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Jan 17, 2023
1 parent 9cf8f20 commit a0a0d88
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"esbuild",
"Kubernetes",
"mastmalize",
"metafile",
"mihandoost",
"nanoserver",
"nanoservice",
Expand Down
34 changes: 22 additions & 12 deletions ui/demo-pwa/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,38 @@ 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 productionMode = process.env.NODE_ENV === 'production';
const watchMode = process.argv.includes('--watch');

logger.logOther(banner);

logger.logProperty('watchMode', watchMode);
logger.logProperty('productionMode', productionMode);

if (process.argv.includes('--clean')) {
logger.logMethod('clean build');
await fs.rmdir(outDir, {recursive: true, force: true});
await fs.rm(outDir, {recursive: true, force: true});
}

const copyPromise = fs.cp(resDir, outDir, {recursive: true, force: true, verbatimSymlinks: true});

const buildPromise = esbuild.build({
entryPoints: [`src/${srcFilename}.ts`],
const esBuild = esbuild.build({
entryPoints: [`${srcDir}/${srcFilename}.ts`],

logLevel: 'info',
platform: 'browser',
target: 'es2018',
format: 'esm',
conditions: ['development'], // dev-mode
conditions: productionMode ? undefined : ['development'],

minify: false, // dev-mode
minify: true,
treeShaking: true,
sourcemap: true,
sourcesContent: true, // dev-mode
sourcesContent: !productionMode,
bundle: true,
splitting: true,
charset: 'ascii',
Expand All @@ -60,19 +66,22 @@ const buildPromise = esbuild.build({
css: banner,
},

outbase: 'src',
outbase: srcDir,
outdir: outDir,
assetNames: 'asset/[name]-[hash]',
entryNames: '[dir]/[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 buildPromise).metafile;
const metafile = (await esBuild).metafile;
const outFiles = Object.keys(metafile.outputs);

const jsFilename = outFiles
Expand Down Expand Up @@ -105,9 +114,10 @@ async function makeHtml() {
await fs.writeFile(`${outDir}/index.html`, htmlContent, {encoding: 'utf-8', flag: 'w'});
}

makeHtml();

console.log(await esbuild.analyzeMetafile((await buildPromise).metafile));
if (!watchMode) {
makeHtml();
console.log(await esbuild.analyzeMetafile((await esBuild).metafile));
}

/*
TODO:
Expand Down
11 changes: 5 additions & 6 deletions ui/demo-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"s": "yarn start",
"w": "yarn watch",
"start": "NODE_OPTIONS=--enable-source-maps run-s clean build serve",
"clean": "rimraf dist build .tsbuildinfo **/*.{d.ts,map} src/**/*.{js,cjs,mjs}",
"build": "run-s build:ts build:es",
"build:ts": "tsc --build",
"build:es": "rimraf dist && rollup -c",
"clean": "rimraf dist build .tsbuildinfo",
"build": "./esbuild.mjs",
"build:tsc": "tsc --build",
"serve": "wds",
"watch": "run-s clean build:ts && run-p watch:ts serve",
"watch:ts": "yarn build:ts --watch --preserveWatchOutput"
"watch": "run-p watch:ts serve",
"watch:ts": "yarn build --clean --watch"
},
"devDependencies": {
"@alwatr/element": "^0.27.0",
Expand Down
2 changes: 1 addition & 1 deletion ui/demo-pwa/web-dev-server.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const config = {
port: 8080,
open: true,
watch: true,
debug: true,
// debug: true,
rootDir: 'dist',
appIndex: 'index.html',
plugins: [],
Expand Down

0 comments on commit a0a0d88

Please sign in to comment.