Merge pull request #45 from IT4Change/build-server

feat(frontend): build server
This commit is contained in:
Ulf Gebhardt 2024-01-09 07:11:10 +01:00 committed by GitHub
commit c7db269664
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 106 additions and 2 deletions

View File

@ -42,6 +42,8 @@ The following commands are available:
| **Develop** | | | **Develop** | |
| `npm run dev` | Compiles and hot-reloads for development | | `npm run dev` | Compiles and hot-reloads for development |
| `npm run server:dev` | Run development server | | `npm run server:dev` | Run development server |
| `npm run server:prod:ts` | Run production server without build (ts-node) |
| `npm run server:build` | Build Server into an executable cjs file |
| **Test** | | | **Test** | |
| `npm run test:lint` | Run all linters | | `npm run test:lint` | Run all linters |
| `npm run test:lint:eslint` | Run linter eslint | | `npm run test:lint:eslint` | Run linter eslint |

20
package-lock.json generated
View File

@ -80,6 +80,7 @@
"stylelint-config-recommended-vue": "^1.5.0", "stylelint-config-recommended-vue": "^1.5.0",
"stylelint-config-standard": "^36.0.0", "stylelint-config-standard": "^36.0.0",
"stylelint-config-standard-scss": "^13.0.0", "stylelint-config-standard-scss": "^13.0.0",
"tsx": "^4.7.0",
"vite-plugin-checker": "^0.6.2", "vite-plugin-checker": "^0.6.2",
"vite-plugin-compression": "^0.5.1", "vite-plugin-compression": "^0.5.1",
"vite-plugin-vuetify": "^2.0.1", "vite-plugin-vuetify": "^2.0.1",
@ -32118,6 +32119,25 @@
"integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==",
"dev": true "dev": true
}, },
"node_modules/tsx": {
"version": "4.7.0",
"resolved": "https://registry.npmjs.org/tsx/-/tsx-4.7.0.tgz",
"integrity": "sha512-I+t79RYPlEYlHn9a+KzwrvEwhJg35h/1zHsLC2JXvhC2mdynMv6Zxzvhv5EMV6VF5qJlLlkSnMVvdZV3PSIGcg==",
"dev": true,
"dependencies": {
"esbuild": "~0.19.10",
"get-tsconfig": "^4.7.2"
},
"bin": {
"tsx": "dist/cli.mjs"
},
"engines": {
"node": ">=18.0.0"
},
"optionalDependencies": {
"fsevents": "~2.3.3"
}
},
"node_modules/type-check": { "node_modules/type-check": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",

View File

@ -37,10 +37,12 @@
"scripts": { "scripts": {
"dev": "npm run server:dev", "dev": "npm run server:dev",
"prod": "npm run build && npm run server:prod", "prod": "npm run build && npm run server:prod",
"build": "vite build", "build": "vite build && npm run server:build",
"server": "node --loader ts-node/esm ./server/index.ts", "server": "node --loader ts-node/esm ./server/index.ts",
"server:dev": "npm run server", "server:dev": "npm run server",
"server:prod": "cross-env NODE_ENV=production npm run server", "server:prod": "node ./build/index.cjs",
"server:prod:ts": "cross-env NODE_ENV=production npm run server",
"server:build": "tsx scripts/buildServer/buildServer",
"storybook": "storybook dev -p 6006", "storybook": "storybook dev -p 6006",
"storybook:build": "storybook build -o build/storybook", "storybook:build": "storybook build -o build/storybook",
"storybook:test": "test-storybook", "storybook:test": "test-storybook",
@ -129,6 +131,7 @@
"stylelint-config-recommended-vue": "^1.5.0", "stylelint-config-recommended-vue": "^1.5.0",
"stylelint-config-standard": "^36.0.0", "stylelint-config-standard": "^36.0.0",
"stylelint-config-standard-scss": "^13.0.0", "stylelint-config-standard-scss": "^13.0.0",
"tsx": "^4.7.0",
"vite-plugin-checker": "^0.6.2", "vite-plugin-checker": "^0.6.2",
"vite-plugin-compression": "^0.5.1", "vite-plugin-compression": "^0.5.1",
"vite-plugin-vuetify": "^2.0.1", "vite-plugin-vuetify": "^2.0.1",

View File

@ -0,0 +1,56 @@
import path from 'node:path'
import { fileURLToPath } from 'url'
// eslint-disable-next-line import/no-extraneous-dependencies
import { build } from 'esbuild'
// eslint-disable-next-line import/no-extraneous-dependencies
import fs, { ensureDir, remove } from 'fs-extra'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
async function buildServer() {
const result = await build({
absWorkingDir: process.cwd(),
entryPoints: [path.join(path.resolve(__dirname, '../../server/'), 'index.ts')],
outfile: 'index.cjs',
write: false,
minify: true,
platform: 'node',
bundle: true,
format: 'cjs',
sourcemap: false,
treeShaking: true,
define: { 'import.meta.url': 'importMetaUrl', 'process.env.NODE_ENV': '"production"' },
inject: [path.resolve(__dirname, './import.meta.url-polyfill.ts')],
banner: {
js: `/* eslint-disable prettier/prettier */`,
},
tsconfig: path.resolve(__dirname, './tsconfig.buildServer.json'),
plugins: [
{
name: 'externalize-deps',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
const id = args.path
if (id[0] !== '.' && !path.isAbsolute(id)) {
return {
external: true,
}
}
})
},
},
],
})
const { text } = result.outputFiles[0]
const filePath = path.join(path.resolve(__dirname, '../../build/'), 'index.cjs')
if (fs.existsSync(filePath)) {
await remove(filePath)
}
await ensureDir(path.dirname(filePath))
// eslint-disable-next-line import/no-named-as-default-member
await fs.writeFile(filePath, text)
}
void buildServer()

View File

@ -0,0 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any */
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
export const importMetaUrl =
typeof document === 'undefined'
? (new (require('url').URL)('file:' + __filename) as URL).href
: (document.currentScript && (document.currentScript as any).src) ||
new URL('main.js', document.baseURI).href

View File

@ -0,0 +1,16 @@
{
"compilerOptions": {
"baseUrl": ".",
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"isolatedModules": true,
"lib": ["esnext", "dom", "DOM.Iterable"],
"strict": false,
"sourceMap": false,
"resolveJsonModule": true,
"skipLibCheck": true,
"esModuleInterop": true,
"declaration": false
}
}