@olenbetong/appframe-vite 1.0.0-alpha.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/build.d.ts +0 -6
- package/lib/build.js +2 -12
- package/lib/devServer.d.ts +4 -3
- package/lib/devServer.js +33 -15
- package/lib/index.d.ts +6 -2
- package/lib/index.js +29 -2
- package/package.json +2 -2
package/lib/build.d.ts
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
1
|
import { UserConfig } from "vite";
|
|
2
|
-
/**
|
|
3
|
-
* Node.js doesn't support JSON imports yet, so this is a simple
|
|
4
|
-
* function that reads a JSON file from disk and returns the parsed
|
|
5
|
-
* content.
|
|
6
|
-
*/
|
|
7
|
-
export declare function importJson(url: string, useCwd?: boolean): Promise<any>;
|
|
8
2
|
export declare function addAppframeBuildConfig(config: UserConfig): Promise<UserConfig>;
|
package/lib/build.js
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
import { viteExternalsPlugin } from "vite-plugin-externals";
|
|
2
1
|
import { visualizer } from "rollup-plugin-visualizer";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
* Node.js doesn't support JSON imports yet, so this is a simple
|
|
6
|
-
* function that reads a JSON file from disk and returns the parsed
|
|
7
|
-
* content.
|
|
8
|
-
*/
|
|
9
|
-
export async function importJson(url, useCwd = false) {
|
|
10
|
-
let completeUrl = new URL(url, useCwd ? `file://${process.cwd()}/` : import.meta.url);
|
|
11
|
-
console.log(completeUrl);
|
|
12
|
-
return JSON.parse(await readFile(completeUrl, "utf-8"));
|
|
13
|
-
}
|
|
2
|
+
import { viteExternalsPlugin } from "vite-plugin-externals";
|
|
3
|
+
import { importJson } from "./importJson.js";
|
|
14
4
|
export async function addAppframeBuildConfig(config) {
|
|
15
5
|
var _a, _b;
|
|
16
6
|
let appPkg = await importJson("./package.json", true);
|
package/lib/devServer.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Application } from "express";
|
|
1
|
+
import { Application, RequestHandler, Router } from "express";
|
|
2
2
|
import { ViteDevServer } from "vite";
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
3
|
+
export declare function createProxyMiddleware(): Promise<Router>;
|
|
4
|
+
export declare function createHtmlTransformer(): Promise<(html: string) => string>;
|
|
5
|
+
export declare function createDevMiddleware(vite: ViteDevServer): Promise<RequestHandler>;
|
|
5
6
|
export declare function startDevServer(app: Application): Promise<void>;
|
package/lib/devServer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import dotenv from "dotenv";
|
|
2
|
+
import express from "express";
|
|
2
3
|
import fs from "node:fs/promises";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import open from "open";
|
|
@@ -7,7 +8,7 @@ import { Client } from "@olenbetong/data-object";
|
|
|
7
8
|
import { importJson } from "./importJson.js";
|
|
8
9
|
import { createAppframeProxy } from "./proxy.js";
|
|
9
10
|
dotenv.config();
|
|
10
|
-
let appPkg = await importJson("./package.json");
|
|
11
|
+
let appPkg = await importJson("./package.json", true);
|
|
11
12
|
let { appframe } = appPkg;
|
|
12
13
|
let userSession = null;
|
|
13
14
|
async function getUserSession(hostname, username, password) {
|
|
@@ -22,10 +23,11 @@ async function getUserSession(hostname, username, password) {
|
|
|
22
23
|
userSession = await userResult.json();
|
|
23
24
|
return userSession;
|
|
24
25
|
}
|
|
25
|
-
export async function
|
|
26
|
+
export async function createProxyMiddleware() {
|
|
26
27
|
var _a, _b, _c, _d, _e;
|
|
27
28
|
const hostname = (_d = (_b = (_a = appframe.proxy) === null || _a === void 0 ? void 0 : _a.hostname) !== null && _b !== void 0 ? _b : (_c = appframe.deploy) === null || _c === void 0 ? void 0 : _c.hostname) !== null && _d !== void 0 ? _d : "dev.obet.no";
|
|
28
29
|
const { APPFRAME_LOGIN: username, APPFRAME_PWD: password } = process.env;
|
|
30
|
+
const router = express.Router();
|
|
29
31
|
if (!username || !password) {
|
|
30
32
|
throw new Error("Your Appframe credentials are not set in the environment variables. Username should be set in APPFRAME_LOGIN and password in APPFRAME_PWD.");
|
|
31
33
|
}
|
|
@@ -49,42 +51,58 @@ export async function addProxyRoutes(app) {
|
|
|
49
51
|
proxyRoutes.push(...appframe.proxy.routes);
|
|
50
52
|
}
|
|
51
53
|
proxyRoutes.forEach(route => {
|
|
52
|
-
|
|
54
|
+
router.use(route, proxy);
|
|
53
55
|
});
|
|
56
|
+
return router;
|
|
54
57
|
}
|
|
55
|
-
export async function
|
|
56
|
-
var _a, _b, _c, _d;
|
|
58
|
+
export async function createHtmlTransformer() {
|
|
59
|
+
var _a, _b, _c, _d, _e, _f;
|
|
57
60
|
const hostname = (_d = (_b = (_a = appframe.proxy) === null || _a === void 0 ? void 0 : _a.hostname) !== null && _b !== void 0 ? _b : (_c = appframe.deploy) === null || _c === void 0 ? void 0 : _c.hostname) !== null && _d !== void 0 ? _d : "dev.obet.no";
|
|
58
61
|
const { APPFRAME_LOGIN: username, APPFRAME_PWD: password } = process.env;
|
|
62
|
+
let isPartner = ((_f = (_e = appPkg.appframe) === null || _e === void 0 ? void 0 : _e.article) === null || _f === void 0 ? void 0 : _f.hostname) === "partner.olenbetong.no";
|
|
59
63
|
if (!username || !password) {
|
|
60
64
|
throw new Error("Your Appframe credentials are not set in the environment variables. Username should be set in APPFRAME_LOGIN and password in APPFRAME_PWD.");
|
|
61
65
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
template = await vite.transformIndexHtml(url, template);
|
|
67
|
-
let userSession = await getUserSession(hostname, username, password);
|
|
68
|
-
let html = template
|
|
69
|
-
.replace("<!-- {Appframe} -->", `<script>
|
|
66
|
+
let userSession = await getUserSession(hostname, username, password);
|
|
67
|
+
return (html) => {
|
|
68
|
+
return html
|
|
69
|
+
.replace("<!-- {Appframe} -->", `<script>
|
|
70
70
|
af = globalThis.af ?? {};
|
|
71
71
|
af.userSession = ${JSON.stringify(userSession)};
|
|
72
72
|
</script>
|
|
73
73
|
<script src="/file/article/static-script/${appframe.article.id}.js"></script>`)
|
|
74
|
-
|
|
74
|
+
.replace(`<!-- {Theme} -->`, isPartner
|
|
75
|
+
? ` <link rel="stylesheet" href="/file/site/style/ob.application.min.css" />
|
|
76
|
+
<link rel="stylesheet" href="/file/site/style/ob.theme.base.less" />
|
|
77
|
+
<link rel="stylesheet" href="/file/site/style/ob.theme.betongost.less" />
|
|
78
|
+
<link rel="stylesheet" href="/file/site/style/ob.theme.olenbetong.less" />
|
|
79
|
+
<link rel="stylesheet" href="/file/site/style/ob.theme.ribe.less" />
|
|
80
|
+
<link rel="stylesheet" href="/file/site/style/ob.theme.sjb.less" />
|
|
81
|
+
`
|
|
82
|
+
: ` <link rel="stylesheet" href="/file/site/style/ob.theme.base.less" />
|
|
75
83
|
<link rel="stylesheet" href="/file/site/style/ob.theme.betongost.less" />
|
|
76
84
|
<link rel="stylesheet" href="/file/site/style/ob.theme.forsand.less" />
|
|
77
85
|
<link rel="stylesheet" href="/file/site/style/ob.theme.olenbetong.less" />
|
|
78
86
|
<link rel="stylesheet" href="/file/site/style/ob.theme.ribe.less" />
|
|
79
87
|
<link rel="stylesheet" href="/file/site/style/ob.theme.sjb.less" />
|
|
80
88
|
<link rel="stylesheet" href="/file/site/style/ob.es.application.min.css" />`);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export async function createDevMiddleware(vite) {
|
|
92
|
+
let transformer = await createHtmlTransformer();
|
|
93
|
+
return async (req, res, next) => {
|
|
94
|
+
const url = req.originalUrl;
|
|
95
|
+
try {
|
|
96
|
+
let template = await fs.readFile(path.resolve(process.cwd(), "index.html"), "utf-8");
|
|
97
|
+
template = await vite.transformIndexHtml(url, template);
|
|
98
|
+
let html = transformer(template);
|
|
81
99
|
res.status(200).set({ "Content-Type": "text/html" }).end(html);
|
|
82
100
|
}
|
|
83
101
|
catch (error) {
|
|
84
102
|
vite.ssrFixStacktrace(error);
|
|
85
103
|
next(error);
|
|
86
104
|
}
|
|
87
|
-
}
|
|
105
|
+
};
|
|
88
106
|
}
|
|
89
107
|
export async function startDevServer(app) {
|
|
90
108
|
let port = 3000;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
import { addAppframeBuildConfig } from "./build.js";
|
|
3
|
+
import { createDevMiddleware, createProxyMiddleware, startDevServer } from "./devServer.js";
|
|
4
|
+
export default function appframe(): Promise<Plugin>;
|
|
5
|
+
export { addAppframeBuildConfig };
|
|
6
|
+
export { createDevMiddleware, createProxyMiddleware, startDevServer };
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { addAppframeBuildConfig } from "./build.js";
|
|
2
|
+
import { createDevMiddleware, createHtmlTransformer, createProxyMiddleware, startDevServer } from "./devServer.js";
|
|
3
|
+
export default async function appframe() {
|
|
4
|
+
let transformIndexHtml = await createHtmlTransformer();
|
|
5
|
+
return {
|
|
6
|
+
name: "appframe",
|
|
7
|
+
async config(_, { command }) {
|
|
8
|
+
let config = {
|
|
9
|
+
build: { target: ["chrome90", "safari14"] },
|
|
10
|
+
resolve: {
|
|
11
|
+
alias: [{ find: "@/", replacement: "/src/" }]
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
if (command === "build") {
|
|
15
|
+
await addAppframeBuildConfig(config);
|
|
16
|
+
}
|
|
17
|
+
return config;
|
|
18
|
+
},
|
|
19
|
+
async configureServer(_server) {
|
|
20
|
+
_server.middlewares.use(await createProxyMiddleware());
|
|
21
|
+
return async () => {
|
|
22
|
+
_server.middlewares.use(await createDevMiddleware(_server));
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
transformIndexHtml
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export { addAppframeBuildConfig };
|
|
29
|
+
export { createDevMiddleware, createProxyMiddleware, startDevServer };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/appframe-vite",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Tools to use and deploy Vite applications to Appframe",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -39,4 +39,4 @@
|
|
|
39
39
|
"open": "^8.4.0",
|
|
40
40
|
"tcp-port-used": "^1.0.2"
|
|
41
41
|
}
|
|
42
|
-
}
|
|
42
|
+
}
|