Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rewrite bundle as node script #10

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions frontend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const {exec} = require('child_process');
const esbuild = require('esbuild');
const bs = require('browser-sync').create('amagaki-browsersync');

class amagakiFeBuilder {
constructor() {
this.prod = process.argv[2] && process.argv[2] === '-p';

if (!this.prod) {
this.devMode();
} else {
this.buildProd();
}
}

/**
* Run development mode.
*/
devMode() {
// Start browser sync.
bs.init({
watch: true,
files: 'dist',
proxy: 'localhost:8080',
});

// Watch ts files.
bs.watch('src/ts/**/*.ts', async () => {
await this.buildEs();
this.reload;
});

// Watch view files.
bs.watch('*.yaml', this.reloadBrowserSync);
bs.watch('*.njk', this.reloadBrowserSync);

// Watch Sass files.
this.watchSass();
}

reloadBrowserSync() {
bs.reload();
}

async buildProd() {
await this.buildSass();
await this.buildEs();
}

buildSass() {
return new Promise(resolve => {
const command =
'sass --no-source-map ./src/sass/:./dist/css --style compressed';
uxder marked this conversation as resolved.
Show resolved Hide resolved
exec(command, (error, stderr) => {
if (stderr) {
console.error('Sass build errors');
console.error(stderr);
} else {
resolve();
}
});
});
}

watchSass() {
return new Promise(resolve => {
const command =
'sass --watch --no-source-map ./src/sass/:./dist/css --style compressed';
exec(command, () => {
resolve();
});
});
}

/**
* Build es files.
*/
buildEs() {
return new Promise(resolve => {
exec('tsc -noEmit', (error, stderr) => {
uxder marked this conversation as resolved.
Show resolved Hide resolved
if (stderr) {
console.error('Typescript errors');
console.error(stderr);
} else {
esbuild
.build({
entryPoints: ['src/ts/main.ts'],
outfile: 'dist/js/main.min.js',
bundle: true,
platform: 'browser',
minify: this.prod,
})
.then(() => {
resolve();
})
.catch(() => {
throw new Error('Es build Failed');
});
}
});
});
}
}

new amagakiFeBuilder();
Loading