From 15fbe413124384257426ff07f45a7680e52ddec5 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Wed, 23 Apr 2025 13:10:13 +0300 Subject: [PATCH] chore(nx/desktop): switch to webpack-based build --- apps/desktop-e2e/.spec.swcrc | 22 +++++++++++ apps/desktop-e2e/eslint.config.mjs | 5 +++ apps/desktop-e2e/jest.config.ts | 24 ++++++++++++ apps/desktop-e2e/package.json | 25 +++++++++++++ apps/desktop-e2e/src/desktop/desktop.spec.ts | 10 +++++ apps/desktop-e2e/src/support/global-setup.ts | 11 ++++++ .../src/support/global-teardown.ts | 7 ++++ apps/desktop-e2e/src/support/test-setup.ts | 9 +++++ apps/desktop-e2e/tsconfig.json | 14 +++++++ apps/desktop/package.json | 37 ------------------- apps/desktop/src/main.ts | 22 ++++++++++- apps/desktop/tsconfig.app.json | 3 +- apps/desktop/webpack.config.js | 20 ++++++++++ 13 files changed, 170 insertions(+), 39 deletions(-) create mode 100644 apps/desktop-e2e/.spec.swcrc create mode 100644 apps/desktop-e2e/eslint.config.mjs create mode 100644 apps/desktop-e2e/jest.config.ts create mode 100644 apps/desktop-e2e/package.json create mode 100644 apps/desktop-e2e/src/desktop/desktop.spec.ts create mode 100644 apps/desktop-e2e/src/support/global-setup.ts create mode 100644 apps/desktop-e2e/src/support/global-teardown.ts create mode 100644 apps/desktop-e2e/src/support/test-setup.ts create mode 100644 apps/desktop-e2e/tsconfig.json create mode 100644 apps/desktop/webpack.config.js diff --git a/apps/desktop-e2e/.spec.swcrc b/apps/desktop-e2e/.spec.swcrc new file mode 100644 index 000000000..3b52a5376 --- /dev/null +++ b/apps/desktop-e2e/.spec.swcrc @@ -0,0 +1,22 @@ +{ + "jsc": { + "target": "es2017", + "parser": { + "syntax": "typescript", + "decorators": true, + "dynamicImport": true + }, + "transform": { + "decoratorMetadata": true, + "legacyDecorator": true + }, + "keepClassNames": true, + "externalHelpers": true, + "loose": true + }, + "module": { + "type": "es6" + }, + "sourceMaps": true, + "exclude": [] +} diff --git a/apps/desktop-e2e/eslint.config.mjs b/apps/desktop-e2e/eslint.config.mjs new file mode 100644 index 000000000..724052a2e --- /dev/null +++ b/apps/desktop-e2e/eslint.config.mjs @@ -0,0 +1,5 @@ +import baseConfig from "../../eslint.config.mjs"; + +export default [ + ...baseConfig +]; diff --git a/apps/desktop-e2e/jest.config.ts b/apps/desktop-e2e/jest.config.ts new file mode 100644 index 000000000..25564ee81 --- /dev/null +++ b/apps/desktop-e2e/jest.config.ts @@ -0,0 +1,24 @@ +/* eslint-disable */ +import { readFileSync } from 'fs'; + +// Reading the SWC compilation config for the spec files +const swcJestConfig = JSON.parse( + readFileSync(`${__dirname}/.spec.swcrc`, 'utf-8') +); + +// Disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves +swcJestConfig.swcrc = false; + +export default { + displayName: '@triliumnext/desktop-e2e', + preset: '../../jest.preset.js', + globalSetup: '/src/support/global-setup.ts', + globalTeardown: '/src/support/global-teardown.ts', + setupFiles: ['/src/support/test-setup.ts'], + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['@swc/jest', swcJestConfig], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: 'test-output/jest/coverage', +}; diff --git a/apps/desktop-e2e/package.json b/apps/desktop-e2e/package.json new file mode 100644 index 000000000..37556fa95 --- /dev/null +++ b/apps/desktop-e2e/package.json @@ -0,0 +1,25 @@ +{ + "name": "@triliumnext/desktop-e2e", + "version": "0.0.1", + "private": true, + "nx": { + "implicitDependencies": [ + "@triliumnext/desktop" + ], + "targets": { + "e2e": { + "executor": "@nx/jest:jest", + "outputs": [ + "{projectRoot}/test-output/jest/coverage" + ], + "options": { + "jestConfig": "apps/desktop-e2e/jest.config.ts", + "passWithNoTests": true + }, + "dependsOn": [ + "@triliumnext/desktop:build" + ] + } + } + } +} diff --git a/apps/desktop-e2e/src/desktop/desktop.spec.ts b/apps/desktop-e2e/src/desktop/desktop.spec.ts new file mode 100644 index 000000000..52572d00f --- /dev/null +++ b/apps/desktop-e2e/src/desktop/desktop.spec.ts @@ -0,0 +1,10 @@ +import axios from 'axios'; + +describe('GET /', () => { + it('should return a message', async () => { + const res = await axios.get(`/`); + + expect(res.status).toBe(200); + expect(res.data).toEqual({ message: 'Hello API' }); + }); +}) diff --git a/apps/desktop-e2e/src/support/global-setup.ts b/apps/desktop-e2e/src/support/global-setup.ts new file mode 100644 index 000000000..d18e0f294 --- /dev/null +++ b/apps/desktop-e2e/src/support/global-setup.ts @@ -0,0 +1,11 @@ +/* eslint-disable */ +var __TEARDOWN_MESSAGE__: string; + +module.exports = async function() { + // Start services that that the app needs to run (e.g. database, docker-compose, etc.). + console.log('\nSetting up...\n'); + + // Hint: Use `globalThis` to pass variables to global teardown. + globalThis.__TEARDOWN_MESSAGE__ = '\nTearing down...\n'; +}; + diff --git a/apps/desktop-e2e/src/support/global-teardown.ts b/apps/desktop-e2e/src/support/global-teardown.ts new file mode 100644 index 000000000..67746cebd --- /dev/null +++ b/apps/desktop-e2e/src/support/global-teardown.ts @@ -0,0 +1,7 @@ +/* eslint-disable */ + +module.exports = async function() { + // Put clean up logic here (e.g. stopping services, docker-compose, etc.). + // Hint: `globalThis` is shared between setup and teardown. + console.log(globalThis.__TEARDOWN_MESSAGE__); +}; diff --git a/apps/desktop-e2e/src/support/test-setup.ts b/apps/desktop-e2e/src/support/test-setup.ts new file mode 100644 index 000000000..c803356a2 --- /dev/null +++ b/apps/desktop-e2e/src/support/test-setup.ts @@ -0,0 +1,9 @@ +/* eslint-disable */ +import axios from 'axios'; + +module.exports = async function() { + // Configure axios for tests to use. + const host = process.env.HOST ?? 'localhost'; + const port = process.env.PORT ?? '3000'; + axios.defaults.baseURL = `http://${host}:${port}`; +}; diff --git a/apps/desktop-e2e/tsconfig.json b/apps/desktop-e2e/tsconfig.json new file mode 100644 index 000000000..2e98c0f60 --- /dev/null +++ b/apps/desktop-e2e/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "outDir": "out-tsc/@triliumnext/desktop-e2e", + "esModuleInterop": true, + "noUnusedLocals": false, + "noImplicitAny": false + }, + "include": [ + "jest.config.ts", + "src/**/*.ts" + ], + "references": [] +} diff --git a/apps/desktop/package.json b/apps/desktop/package.json index b0691054a..66428c072 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -4,43 +4,6 @@ "private": true, "nx": { "targets": { - "build": { - "executor": "@nx/esbuild:esbuild", - "outputs": [ - "{options.outputPath}" - ], - "defaultConfiguration": "production", - "options": { - "platform": "node", - "outputPath": "apps/desktop/dist", - "format": [ - "cjs" - ], - "bundle": false, - "main": "apps/desktop/src/main.ts", - "tsConfig": "apps/desktop/tsconfig.app.json", - "assets": [ - "apps/desktop/src/assets" - ], - "esbuildOptions": { - "sourcemap": true, - "outExtension": { - ".js": ".js" - } - } - }, - "configurations": { - "development": {}, - "production": { - "esbuildOptions": { - "sourcemap": false, - "outExtension": { - ".js": ".js" - } - } - } - } - }, "serve": { "executor": "@nx/js:node", "defaultConfiguration": "development", diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 73c02658c..a8a385463 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -1 +1,21 @@ -console.log('Hello World'); \ No newline at end of file +/** + * This is not a production server yet! + * This is only a minimal backend to get started. + */ + +import express from 'express'; +import * as path from 'path'; + +const app = express(); + +app.use('/assets', express.static(path.join(__dirname, 'assets'))); + +app.get('/api', (req, res) => { + res.send({ message: 'Welcome to desktop!' }); +}); + +const port = process.env.PORT || 3333; +const server = app.listen(port, () => { + console.log(`Listening at http://localhost:${port}/api`); +}); +server.on('error', console.error); diff --git a/apps/desktop/tsconfig.app.json b/apps/desktop/tsconfig.app.json index bdf9ba861..b293e155d 100644 --- a/apps/desktop/tsconfig.app.json +++ b/apps/desktop/tsconfig.app.json @@ -3,7 +3,8 @@ "compilerOptions": { "outDir": "dist", "types": [ - "node" + "node", + "express" ], "rootDir": "src", "tsBuildInfoFile": "dist/tsconfig.app.tsbuildinfo" diff --git a/apps/desktop/webpack.config.js b/apps/desktop/webpack.config.js new file mode 100644 index 000000000..4f8180c8e --- /dev/null +++ b/apps/desktop/webpack.config.js @@ -0,0 +1,20 @@ +const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin'); +const { join } = require('path'); + +module.exports = { + output: { + path: join(__dirname, 'dist'), + }, + plugins: [ + new NxAppWebpackPlugin({ + target: 'node', + compiler: 'tsc', + main: './src/main.ts', + tsConfig: './tsconfig.app.json', + assets: ["./src/assets"], + optimization: false, + outputHashing: 'none', + generatePackageJson: true, + }) + ], +};