Skip to content

Commit

Permalink
Merge pull request #148 from Morphclue/build/esbuild
Browse files Browse the repository at this point in the history
ESBuild
  • Loading branch information
Morphclue authored Apr 6, 2024
2 parents 492336a + 7694167 commit 6de1da7
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 163 deletions.
81 changes: 13 additions & 68 deletions apps/frontend/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"tags": [],
"targets": {
"build": {
"executor": "@angular-devkit/build-angular:browser",
"outputs": ["{options.outputPath}"],
"executor": "@nx/angular:application",
"outputs": ["{options.outputPath.base}"],
"options": {
"outputPath": "dist/apps/frontend/browser",
"outputPath": {
"base": "dist/apps/frontend"
},
"index": "apps/frontend/src/index.html",
"main": "apps/frontend/src/main.ts",
"polyfills": [
"@angular/localize/init",
"zone.js",
Expand All @@ -28,8 +29,13 @@
"styles": ["apps/frontend/src/styles.scss"],
"allowedCommonJsDependencies": ["validator"],
"scripts": [],
"serviceWorker": true,
"ngswConfigPath": "apps/frontend/ngsw-config.json"
"serviceWorker": "apps/frontend/ngsw-config.json",
"browser": "apps/frontend/src/main.ts",
"server": "apps/frontend/src/main.server.ts",
"prerender": true,
"ssr": {
"entry": "apps/frontend/server.ts"
}
},
"configurations": {
"production": {
Expand All @@ -54,9 +60,7 @@
]
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
Expand All @@ -65,7 +69,7 @@
"defaultConfiguration": "production"
},
"serve": {
"executor": "@angular-devkit/build-angular:dev-server",
"executor": "@nx/angular:dev-server",
"configurations": {
"production": {
"buildTarget": "frontend:build:production"
Expand Down Expand Up @@ -101,65 +105,6 @@
"options": {
"buildTarget": "frontend:build"
}
},
"server": {
"dependsOn": ["build"],
"executor": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/apps/frontend/server",
"main": "apps/frontend/server.ts",
"tsConfig": "apps/frontend/tsconfig.server.json"
},
"configurations": {
"production": {
"outputHashing": "media",
"fileReplacements": [
{
"replace": "apps/frontend/src/environments/environment.ts",
"with": "apps/frontend/src/environments/environment.prod.ts"
}
]
},
"development": {
"buildOptimizer": false,
"optimization": false,
"sourceMap": true,
"extractLicenses": false,
"vendorChunk": true
}
},
"defaultConfiguration": "production"
},
"serve-ssr": {
"executor": "@angular-devkit/build-angular:ssr-dev-server",
"configurations": {
"development": {
"browserTarget": "frontend:build:development",
"serverTarget": "frontend:server:development"
},
"production": {
"browserTarget": "frontend:build:production",
"serverTarget": "frontend:server:production"
}
},
"defaultConfiguration": "development"
},
"prerender": {
"executor": "@angular-devkit/build-angular:prerender",
"options": {
"routes": ["/"]
},
"configurations": {
"development": {
"browserTarget": "frontend:build:development",
"serverTarget": "frontend:server:development"
},
"production": {
"browserTarget": "frontend:build:production",
"serverTarget": "frontend:server:production"
}
},
"defaultConfiguration": "production"
}
}
}
40 changes: 15 additions & 25 deletions apps/frontend/server.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import 'zone.js/node';

import { APP_BASE_HREF } from '@angular/common';
import { CommonEngine } from '@angular/ssr';
import express from 'express';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

import { AppServerModule } from './src/main.server';
import AppServerModule from './src/main.server';

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
const distFolder = join(process.cwd(), 'dist/apps/frontend/browser');
const indexHtml = existsSync(join(distFolder, 'index.original.html'))
? join(distFolder, 'index.original.html')
: join(distFolder, 'index.html');
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');
const indexHtml = join(serverDistFolder, 'index.server.html');

const commonEngine = new CommonEngine();

server.set('view engine', 'html');
server.set('views', distFolder);
server.set('views', browserDistFolder);

// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get(
'*.*',
express.static(distFolder, {
express.static(browserDistFolder, {
maxAge: '1y',
})
}),
);

// All regular routes use the Angular engine
Expand All @@ -40,8 +37,11 @@ export function app(): express.Express {
bootstrap: AppServerModule,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: distFolder,
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
publicPath: browserDistFolder,
providers: [
{ provide: APP_BASE_HREF, useValue: baseUrl },
{ provide: 'BASE_URL', useValue: `${protocol}://${headers.host}` },
],
})
.then((html) => res.send(html))
.catch((err) => next(err));
Expand All @@ -60,14 +60,4 @@ function run(): void {
});
}

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = (mainModule && mainModule.filename) || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}

export default AppServerModule;
run();
3 changes: 0 additions & 3 deletions apps/frontend/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {Component, Inject, OnInit, Optional} from '@angular/core';
import {Meta} from '@angular/platform-browser';
import {SwUpdate} from '@angular/service-worker';

import {TokenService} from './core/services';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
Expand All @@ -13,7 +11,6 @@ export class AppComponent implements OnInit {
title = 'apollusia';

constructor(
private tokenService: TokenService,
private swUpdate: SwUpdate,
private meta: Meta,
@Optional() @Inject('BASE_URL') private baseUrl?: string,
Expand Down
9 changes: 7 additions & 2 deletions apps/frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
import {HTTP_INTERCEPTORS, HttpClientModule, provideHttpClient, withFetch} from '@angular/common/http';
import {isDevMode, NgModule} from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms';
import {BrowserModule, provideClientHydration} from '@angular/platform-browser';
Expand All @@ -17,7 +17,7 @@ import {LegalModule} from './legal/legal.module';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule.withServerTransition({appId: 'serverApp'}),
BrowserModule,
AppRoutingModule,
NgbModule,
ModalModule,
Expand All @@ -42,6 +42,11 @@ import {LegalModule} from './legal/legal.module';
multi: true,
},
provideClientHydration(),
provideHttpClient(withFetch()),
...globalThis.document ? [{
provide: 'BASE_URL',
useValue: globalThis.document?.baseURI,
}] : [],
],
bootstrap: [AppComponent],
})
Expand Down
22 changes: 6 additions & 16 deletions apps/frontend/src/app/app.server.module.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import {NgModule} from '@angular/core';
import {ServerModule} from '@angular/platform-server';
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';

import {AppComponent} from './app.component';
import {AppModule} from './app.module';
import { AppComponent } from './app.component';
import { AppModule } from './app.module';

@NgModule({
imports: [
AppModule,
ServerModule,
],
imports: [AppModule, ServerModule],
bootstrap: [AppComponent],
providers: [
{
provide: 'BASE_URL',
useValue: process.env['BASE_URL'],
},
],
})
export class AppServerModule {
}
export class AppServerModule {}
5 changes: 3 additions & 2 deletions apps/frontend/src/app/core/info-table/info-table.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, Input, OnInit} from '@angular/core';
import {Component, Inject, Input, OnInit, Optional} from '@angular/core';
import {ToastService} from '@mean-stream/ngbx';

import {ReadPoll} from '../../model';
Expand All @@ -17,11 +17,12 @@ export class InfoTableComponent implements OnInit {

constructor(
private toastService: ToastService,
@Optional() @Inject('BASE_URL') private baseUrl?: string,
) {
}

ngOnInit() {
this.url = `${document.baseURI}poll/${this.poll.id}/participate`;
this.url = `${this.baseUrl}poll/${this.poll.id}/participate`;
}

copyToClipboard() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,17 @@ export class ChooseEventsComponent implements OnInit {
this.currentSort = sortMethod.name;
this.currentSortDirection = sortMethod.defaultDirection;
}
this.participants?.sortBy(sortMethod.by, this.currentSortDirection);
this.participants?.sort((a, b) => {
const aVal = sortMethod.by(a);
const bVal = sortMethod.by(b);
if (aVal < bVal) {
return -this.currentSortDirection;
}
if (aVal > bVal) {
return this.currentSortDirection;
}
return 0;
});
}

// Helpers
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/main.server.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {AppServerModule} from './app/app.server.module';
export { AppServerModule as default } from './app/app.server.module';
19 changes: 0 additions & 19 deletions apps/frontend/src/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,19 +0,0 @@
declare global {
interface Array<T> {
sortBy(expr: (item: T) => any, direction?: 1 | -1): T[];
}
}

Array.prototype.sortBy = function(expr, direction = 1) {
return this.sort((a, b) => {
const aVal = expr(a);
const bVal = expr(b);
if (aVal < bVal) {
return -direction;
}
if (aVal > bVal) {
return direction;
}
return 0;
});
};
10 changes: 5 additions & 5 deletions apps/frontend/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ $primary: $french-violet;
$dark: $persian-indigo;

/* Imports */
@import 'node_modules/bootstrap/scss/bootstrap';
@import 'node_modules/bootstrap-icons/font/bootstrap-icons.css';
@import 'node_modules/angular-calendar/css/angular-calendar.css';
@import 'node_modules/flatpickr/dist/flatpickr.css';
@import 'bootstrap/scss/bootstrap'; // NB: @import 'bootstrap' does not allow variable overrides
@import 'bootstrap-icons';
@import 'angular-calendar/scss/angular-calendar';
@import 'flatpickr';

.logo-glow {
filter: drop-shadow(0 0 10px $primary);
Expand Down Expand Up @@ -106,8 +106,8 @@ $dark: $persian-indigo;
}

.ngb-dp-today {
@extend .fw-bold;
color: $primary;
font-weight: $font-weight-bold;
}

.ngb-dp-day:hover {
Expand Down
11 changes: 7 additions & 4 deletions apps/frontend/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": [
"@angular/localize"
]
"types": ["@angular/localize", "node"]
},
"files": ["src/main.ts", "src/polyfills.ts"],
"files": [
"src/main.ts",
"src/polyfills.ts",
"src/main.server.ts",
"server.ts"
],
"include": ["src/**/*.d.ts"],
"exclude": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts"]
}
1 change: 0 additions & 1 deletion apps/frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"target": "es2022",
"useDefineForClassFields": false,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"strict": true,
Expand Down
13 changes: 0 additions & 13 deletions apps/frontend/tsconfig.server.json

This file was deleted.

Loading

0 comments on commit 6de1da7

Please sign in to comment.