From 9d3598403383475f8a79fff466b31e3334db390c Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Fri, 2 Aug 2019 09:11:52 +0700 Subject: [PATCH] Add frontend build scripts --- packages/scripts/src/Subprocess.ts | 8 ++ packages/scripts/src/commands/build.ts | 102 ++++++++++++++++++++++++- packages/scripts/src/commands/index.ts | 1 - packages/scripts/src/commands/watch.ts | 5 -- packages/scripts/src/index.ts | 2 +- 5 files changed, 109 insertions(+), 9 deletions(-) delete mode 100644 packages/scripts/src/commands/watch.ts diff --git a/packages/scripts/src/Subprocess.ts b/packages/scripts/src/Subprocess.ts index 16e083a..bf80ea7 100644 --- a/packages/scripts/src/Subprocess.ts +++ b/packages/scripts/src/Subprocess.ts @@ -36,7 +36,15 @@ export class Subprocess { reject(new Error(`"${this.command}" exited with code ${code}`)) } }) + let exited = false + subprocess.on('exit', () => exited = true) subprocess.on('error', reject) + + process.on('exit', () => { + if (!exited) { + subprocess.kill() + } + }) }) } } diff --git a/packages/scripts/src/commands/build.ts b/packages/scripts/src/commands/build.ts index 61a8628..498c516 100644 --- a/packages/scripts/src/commands/build.ts +++ b/packages/scripts/src/commands/build.ts @@ -1,5 +1,103 @@ import {run} from '../run' +import {join} from 'path' +import {mkdirSync} from 'fs' -export async function build(path: string) { - await run('ttsc', ['--build', path]) +const tsc = 'ttsc' + +export async function build(path: string = '.') { + await run(tsc, ['--build', path]) +} + +export async function buildEsm(path: string = '.') { + await run(tsc, ['--build', join(path, 'tsconfig.esm.json')]) +} + +export async function watch(path: string = '.') { + await run(tsc, ['--build', '--watch', '--preserveWatchOutput', path]) +} + +export async function watchEsm(path: string = '.') { + await run(tsc, [ + '--build', + '--watch', + '--preserveWatchOutput', + join(path, 'tsconfig.esm.json'), + ]) +} + +export async function browserify(path: string = '.') { + // mkdirSync(join(path, 'build'), {recursive: true}) + await run('browserify', [ + join(path, 'esm', 'index.js'), + '--full-paths', // TODO this might be unneccessary + '-g', '[', 'loose-envify', 'purge', '--NODE_ENV', 'production', ']', + '-p', '[', 'esmify', ']', + '-p', '[', 'common-shakeify', '-v', ']', + '-v', '-o', join(path, 'build', 'client.prod.js'), + ]) +} + +export async function uglify(path: string = '.') { + await run('uglifyjs', [ + '--compress', + '--mangle', + '--source-map', + '-o', join(path, 'build', 'client.js'), + '--', + join(path, 'build', 'client.prod.js'), + ]) +} + +export async function js(path: string = '.') { + await buildEsm(path) + await browserify(path) + await uglify(path) +} + +export async function watchJs(path: string = '.') { + await run('watchify', [ + join(path, 'esm', 'index.js'), + // '-p', '[', 'tsify', '--project', path, ']', + '-g', '[', 'loose-envify', 'purge', '--NODE_ENV', 'development', ']', + '-v', + '--debug', + '-o', + join(path, 'build', 'client.js'), + ]) +} + +export async function css(path = '.') { + await run('node-sass', [ + '--output-style', 'compressed', + '--output', join(path, 'build'), + join(path, 'scss'), + ]) +} + +export async function watchCss(path = '.') { + await run('node-sass', [ + join(path, 'scss'), + '--output', join(path, 'build'), + '--source-map', 'true', + '--source-map-contents', + '--source-map-embed', + ]) + await run('node-sass', [ + '--watch', + join(path, 'scss'), + '--output', join(path, 'build'), + '--source-map', 'true', + '--source-map-contents', + '--source-map-embed', + ]) +} + +export async function frontend(path = '.') { + await buildEsm(path) + const promises = [ + watchEsm(path), + watchJs(path), + watchCss(path), + ] + return Promise.all(promises) } diff --git a/packages/scripts/src/commands/index.ts b/packages/scripts/src/commands/index.ts index ac960ec..6501a0c 100644 --- a/packages/scripts/src/commands/index.ts +++ b/packages/scripts/src/commands/index.ts @@ -1,3 +1,2 @@ export * from './help' export * from './build' -export * from './watch' diff --git a/packages/scripts/src/commands/watch.ts b/packages/scripts/src/commands/watch.ts deleted file mode 100644 index 10d2f31..0000000 --- a/packages/scripts/src/commands/watch.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {run} from '../run' - -export async function watch(path: string) { - await run('ttsc', ['--build', '--watch', '--preserveWatchOutput', path]) -} diff --git a/packages/scripts/src/index.ts b/packages/scripts/src/index.ts index 4046665..94e560e 100644 --- a/packages/scripts/src/index.ts +++ b/packages/scripts/src/index.ts @@ -3,7 +3,7 @@ import * as commands from './commands' import {TCommand} from './TCommand' async function run(...argv: string[]) { - const commandName = argv[0] || 'help' + const commandName = argv[0] if (!(commandName in commands)) { const c = Object.keys(commands).filter(cmd => !cmd.startsWith('_')) console.log(