@olenbetong/appframe-vite 1.0.0 → 1.0.3

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.
@@ -1,5 +1,6 @@
1
1
  import { Application, RequestHandler, Router } from "express";
2
2
  import { ViteDevServer } from "vite";
3
3
  export declare function createProxyMiddleware(): Promise<Router>;
4
+ export declare function createHtmlTransformer(): (html: string) => Promise<string>;
4
5
  export declare function createDevMiddleware(vite: ViteDevServer): Promise<RequestHandler>;
5
6
  export declare function startDevServer(app: Application): Promise<void>;
package/lib/devServer.js CHANGED
@@ -55,32 +55,47 @@ export async function createProxyMiddleware() {
55
55
  });
56
56
  return router;
57
57
  }
58
- export async function createDevMiddleware(vite) {
59
- var _a, _b, _c, _d;
58
+ export function createHtmlTransformer() {
59
+ var _a, _b, _c, _d, _e, _f;
60
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";
61
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";
62
63
  if (!username || !password) {
63
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.");
64
65
  }
65
- return async (req, res, next) => {
66
- const url = req.originalUrl;
67
- try {
68
- let template = await fs.readFile(path.resolve(process.cwd(), "index.html"), "utf-8");
69
- template = await vite.transformIndexHtml(url, template);
70
- let userSession = await getUserSession(hostname, username, password);
71
- let html = template
72
- .replace("<!-- {Appframe} -->", `<script>
66
+ return async (html) => {
67
+ let userSession = await getUserSession(hostname, username, password);
68
+ return html
69
+ .replace("<!-- {Appframe} -->", `<script>
73
70
  af = globalThis.af ?? {};
74
71
  af.userSession = ${JSON.stringify(userSession)};
75
72
  </script>
76
73
  <script src="/file/article/static-script/${appframe.article.id}.js"></script>`)
77
- .replace(`<!-- {Theme} -->`, `<link rel="stylesheet" href="/file/site/style/ob.theme.base.less" />
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" />
78
83
  <link rel="stylesheet" href="/file/site/style/ob.theme.betongost.less" />
79
84
  <link rel="stylesheet" href="/file/site/style/ob.theme.forsand.less" />
80
85
  <link rel="stylesheet" href="/file/site/style/ob.theme.olenbetong.less" />
81
86
  <link rel="stylesheet" href="/file/site/style/ob.theme.ribe.less" />
82
87
  <link rel="stylesheet" href="/file/site/style/ob.theme.sjb.less" />
83
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);
84
99
  res.status(200).set({ "Content-Type": "text/html" }).end(html);
85
100
  }
86
101
  catch (error) {
package/lib/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- export * from "./build.js";
2
- export * from "./devServer.js";
1
+ import { Plugin } from "vite";
2
+ import { addAppframeBuildConfig } from "./build.js";
3
+ import { createDevMiddleware, createProxyMiddleware, startDevServer } from "./devServer.js";
4
+ export default function appframe(): Plugin;
5
+ export { addAppframeBuildConfig };
6
+ export { createDevMiddleware, createProxyMiddleware, startDevServer };
package/lib/index.js CHANGED
@@ -1,2 +1,29 @@
1
- export * from "./build.js";
2
- export * from "./devServer.js";
1
+ import { addAppframeBuildConfig } from "./build.js";
2
+ import { createDevMiddleware, createHtmlTransformer, createProxyMiddleware, startDevServer } from "./devServer.js";
3
+ export default function appframe() {
4
+ let transformIndexHtml = 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.0",
3
+ "version": "1.0.3",
4
4
  "description": "Tools to use and deploy Vite applications to Appframe",
5
5
  "main": "./lib/index.js",
6
6
  "type": "module",