From b1c18350feaaed82d03f9d4d15da8c8277266259 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 9 Jan 2024 07:10:05 +0100 Subject: [PATCH 1/3] buildServer scripts --- scripts/buildServer/buildServer.ts | 56 +++++++++++++++++++ .../buildServer/import.meta.url-polyfill.ts | 7 +++ scripts/buildServer/tsconfig.buildServer.json | 16 ++++++ 3 files changed, 79 insertions(+) create mode 100644 scripts/buildServer/buildServer.ts create mode 100644 scripts/buildServer/import.meta.url-polyfill.ts create mode 100644 scripts/buildServer/tsconfig.buildServer.json diff --git a/scripts/buildServer/buildServer.ts b/scripts/buildServer/buildServer.ts new file mode 100644 index 0000000..f036c00 --- /dev/null +++ b/scripts/buildServer/buildServer.ts @@ -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() diff --git a/scripts/buildServer/import.meta.url-polyfill.ts b/scripts/buildServer/import.meta.url-polyfill.ts new file mode 100644 index 0000000..27dbdaf --- /dev/null +++ b/scripts/buildServer/import.meta.url-polyfill.ts @@ -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 diff --git a/scripts/buildServer/tsconfig.buildServer.json b/scripts/buildServer/tsconfig.buildServer.json new file mode 100644 index 0000000..75c51ae --- /dev/null +++ b/scripts/buildServer/tsconfig.buildServer.json @@ -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 + } + } \ No newline at end of file From 0dfb3018a87832e42559b9f1cf2499a06228da15 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 9 Jan 2024 07:10:19 +0100 Subject: [PATCH 2/3] build server using tsx --- package-lock.json | 20 ++++++++++++++++++++ package.json | 7 +++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 51af06f..b3ab489 100644 --- a/package-lock.json +++ b/package-lock.json @@ -80,6 +80,7 @@ "stylelint-config-recommended-vue": "^1.5.0", "stylelint-config-standard": "^36.0.0", "stylelint-config-standard-scss": "^13.0.0", + "tsx": "^4.7.0", "vite-plugin-checker": "^0.6.2", "vite-plugin-compression": "^0.5.1", "vite-plugin-vuetify": "^2.0.1", @@ -32118,6 +32119,25 @@ "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", "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": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", diff --git a/package.json b/package.json index 9977148..c0d5a6c 100644 --- a/package.json +++ b/package.json @@ -37,10 +37,12 @@ "scripts": { "dev": "npm run server:dev", "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: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:build": "storybook build -o build/storybook", "storybook:test": "test-storybook", @@ -129,6 +131,7 @@ "stylelint-config-recommended-vue": "^1.5.0", "stylelint-config-standard": "^36.0.0", "stylelint-config-standard-scss": "^13.0.0", + "tsx": "^4.7.0", "vite-plugin-checker": "^0.6.2", "vite-plugin-compression": "^0.5.1", "vite-plugin-vuetify": "^2.0.1", From 2321a78c3292b63edfddff614d016068701d9c40 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 9 Jan 2024 07:10:24 +0100 Subject: [PATCH 3/3] document new commands --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0517b96..e4cf24c 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ The following commands are available: | **Develop** | | | `npm run dev` | Compiles and hot-reloads for development | | `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** | | | `npm run test:lint` | Run all linters | | `npm run test:lint:eslint` | Run linter eslint |