@olenbetong/appframe-vite 1.0.1 → 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.
@@ -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(): Promise<(html: string) => 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,42 +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 async 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
- var _a, _b;
67
- const url = req.originalUrl;
68
- try {
69
- let isPartner = ((_b = (_a = appPkg.appframe) === null || _a === void 0 ? void 0 : _a.article) === null || _b === void 0 ? void 0 : _b.hostname) === "partner.olenbetong.no";
70
- let template = await fs.readFile(path.resolve(process.cwd(), "index.html"), "utf-8");
71
- template = await vite.transformIndexHtml(url, template);
72
- let userSession = await getUserSession(hostname, username, password);
73
- let html = template
74
- .replace("<!-- {Appframe} -->", `<script>
66
+ let userSession = await getUserSession(hostname, username, password);
67
+ return (html) => {
68
+ return html
69
+ .replace("<!-- {Appframe} -->", `<script>
75
70
  af = globalThis.af ?? {};
76
71
  af.userSession = ${JSON.stringify(userSession)};
77
72
  </script>
78
73
  <script src="/file/article/static-script/${appframe.article.id}.js"></script>`)
79
- .replace(`<!-- {Theme} -->`, isPartner
80
- ? ` <link rel="stylesheet" href="/file/site/style/ob.application.min.css" />
74
+ .replace(`<!-- {Theme} -->`, isPartner
75
+ ? ` <link rel="stylesheet" href="/file/site/style/ob.application.min.css" />
81
76
  <link rel="stylesheet" href="/file/site/style/ob.theme.base.less" />
82
77
  <link rel="stylesheet" href="/file/site/style/ob.theme.betongost.less" />
83
78
  <link rel="stylesheet" href="/file/site/style/ob.theme.olenbetong.less" />
84
79
  <link rel="stylesheet" href="/file/site/style/ob.theme.ribe.less" />
85
80
  <link rel="stylesheet" href="/file/site/style/ob.theme.sjb.less" />
86
81
  `
87
- : ` <link rel="stylesheet" href="/file/site/style/ob.theme.base.less" />
82
+ : ` <link rel="stylesheet" href="/file/site/style/ob.theme.base.less" />
88
83
  <link rel="stylesheet" href="/file/site/style/ob.theme.betongost.less" />
89
84
  <link rel="stylesheet" href="/file/site/style/ob.theme.forsand.less" />
90
85
  <link rel="stylesheet" href="/file/site/style/ob.theme.olenbetong.less" />
91
86
  <link rel="stylesheet" href="/file/site/style/ob.theme.ribe.less" />
92
87
  <link rel="stylesheet" href="/file/site/style/ob.theme.sjb.less" />
93
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);
94
99
  res.status(200).set({ "Content-Type": "text/html" }).end(html);
95
100
  }
96
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(): Promise<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 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.1",
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",