From 40c0add0e3468502c06c5102ce5e9518f9f4706f Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 16 Mar 2023 12:18:17 +0100 Subject: [PATCH] Add vite-plugin-node to be able to run API locally --- package.json | 3 ++- pnpm-lock.yaml | 33 ++++++++++++++++++++++++++++++++- vite.config.js | 16 +++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1d6b2c6..345c756 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "sass": "^1.49.7", "svgo": "^3.0.1", "terser": "^5.16.6", - "vite": "^4.1.4" + "vite": "^4.1.4", + "vite-plugin-node": "^3.0.2" }, "postcss": { "map": true, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8b4928c..87d20e8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,7 @@ specifiers: svgo: ^3.0.1 terser: ^5.16.6 vite: ^4.1.4 + vite-plugin-node: ^3.0.2 devDependencies: '@vitejs/plugin-legacy': 4.0.2_terser@5.16.6+vite@4.1.4 @@ -29,6 +30,7 @@ devDependencies: svgo: 3.0.2 terser: 5.16.6 vite: 4.1.4_sass@1.49.7+terser@5.16.6 + vite-plugin-node: 3.0.2_vite@4.1.4 packages: @@ -1502,6 +1504,14 @@ packages: fastq: 1.15.0 dev: true + /@rollup/pluginutils/4.2.1: + resolution: {integrity: sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==} + engines: {node: '>= 8.0.0'} + dependencies: + estree-walker: 2.0.2 + picomatch: 2.3.1 + dev: true + /@trysound/sax/0.2.0: resolution: {integrity: sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA==} engines: {node: '>=10.13.0'} @@ -2119,7 +2129,11 @@ packages: engines: {node: '>=4.0'} dev: true - /esutils@2.0.3: + /estree-walker/2.0.2: + resolution: {integrity: sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==} + dev: true + + /esutils/2.0.3: resolution: {integrity: sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==} engines: {node: '>=0.10.0'} dev: true @@ -3052,6 +3066,23 @@ packages: spdx-expression-parse: 3.0.1 dev: true + /vite-plugin-node/3.0.2_vite@4.1.4: + resolution: {integrity: sha512-pA/QZDe4VRFUSChTrR64EfdsnwHbTnkXKx1Be8MP2oW43TPeQgtIfqiGOuXTPUKjwINtyEl8JMzRN0+dYyfTow==} + peerDependencies: + '@swc/core': ^1.2.153 + vite: ^4.0.0 + peerDependenciesMeta: + '@swc/core': + optional: true + dependencies: + '@rollup/pluginutils': 4.2.1 + chalk: 4.1.2 + debug: 4.3.4 + vite: 4.1.4_sass@1.49.7+terser@5.16.6 + transitivePeerDependencies: + - supports-color + dev: true + /vite/4.1.4_sass@1.49.7+terser@5.16.6: resolution: {integrity: sha512-3knk/HsbSTKEin43zHu7jTwYWv81f8kgAL99G5NWBcA1LKvtvcVAC4JjBH1arBunO9kQka+1oGbrMKOjk4ZrBg==} engines: {node: ^14.18.0 || >=16.0.0} diff --git a/vite.config.js b/vite.config.js index e5d07e5..2242930 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,4 +1,5 @@ import legacy from "@vitejs/plugin-legacy"; +import { VitePluginNode } from "vite-plugin-node"; export default { build: { @@ -6,5 +7,18 @@ export default { terserOptions: { ecma: 5 }, sourcemap: "true", }, - plugins: [legacy()], + plugins: [ + legacy(), + VitePluginNode({ + // Workaround from: https://github.com/axe-me/vite-plugin-node/issues/47 + adapter({ app, req, res, next }) { + if (req.url.startsWith("/api/")) { + app(req, res); + } else { + next(); + } + }, + appPath: "./api/share.js", + }), + ], };