buner 1.0.6 → 1.0.8

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/dist/prerender.js CHANGED
@@ -8,151 +8,159 @@ import slash from "slash";
8
8
  import _ from "lodash";
9
9
  import { loadEnv } from "vite";
10
10
  import chalk from "chalk";
11
- const argvModeIndex = process.argv.indexOf("--mode");
12
- const mode = argvModeIndex >= 0 && argvModeIndex < process.argv.length - 1 && !process.argv[argvModeIndex + 1].startsWith("-") ? process.argv[argvModeIndex + 1] : "production";
13
- const projectRoot = process.cwd();
14
- const xpackEnv = loadEnv(mode, projectRoot);
15
- const toAbsolute = (p) => path.resolve(projectRoot, p);
16
- const log = console.log.bind(console);
17
- const template = fs.readFileSync(toAbsolute(process.env.VITE_TEMPLATE ?? "dist/static/index.html"), "utf-8");
18
- const { render, routesToPrerender } = await import(pathToFileURL(toAbsolute("./dist/server/entry-server.js")).href);
19
- const beautifyOptions = {
20
- indent_size: 2,
21
- indent_char: " ",
22
- keep_array_indentation: false,
23
- break_chained_methods: false,
24
- indent_scripts: "normal",
25
- brace_style: "collapse",
26
- space_before_conditional: true,
27
- unescape_strings: false,
28
- jslint_happy: false,
29
- end_with_newline: false,
30
- wrap_line_length: 0,
31
- indent_inner_html: false,
32
- comma_first: false,
33
- e4x: false,
34
- indent_empty_lines: false,
35
- wrap_attributes: "force",
36
- max_preserve_newlines: 5,
37
- preserve_newlines: true
38
- };
39
- const updateResourcePath = ($, tagName, attr, addHash) => {
40
- $(tagName).each((_2, el) => {
41
- const href = $(el).attr(attr);
42
- if (href && href.startsWith("/")) {
43
- let newPath = href;
44
- if (process.env.VITE_DOMAIN) {
45
- newPath = process.env.VITE_DOMAIN + newPath;
46
- }
47
- if (href.startsWith(xpackEnv.VITE_BASE_URL) && !href.startsWith(xpackEnv.VITE_BASE_URL + "assets/vendors/") && [".css", ".ico", ".js", ".webmanifest", ".svg"].includes(path.extname(href).toLowerCase()) && !/\.0x[a-z0-9]{8}\.\w+$/gi.test(href)) {
48
- const path2 = toAbsolute("dist/static/" + href.substring(xpackEnv.VITE_BASE_URL.length));
49
- if (fs.existsSync(path2)) {
50
- const content = fs.readFileSync(path2);
51
- const sha1Hash = crypto.createHash("sha1");
52
- sha1Hash.update(content);
53
- const hash = sha1Hash.digest("base64url").substring(0, 10);
54
- if (addHash) {
55
- newPath += "?v=" + hash;
11
+ const runPrerender = async (options = {}) => {
12
+ const mode = options.mode ?? "production";
13
+ const addHash = options.addHash ?? false;
14
+ const projectRoot = process.cwd();
15
+ const xpackEnv = loadEnv(mode, projectRoot);
16
+ const toAbsolute = (p) => path.resolve(projectRoot, p);
17
+ const log = console.log.bind(console);
18
+ const template = fs.readFileSync(toAbsolute(process.env.VITE_TEMPLATE ?? "dist/static/index.html"), "utf-8");
19
+ const { render, routesToPrerender } = await import(pathToFileURL(toAbsolute("./dist/server/entry-server.js")).href);
20
+ const beautifyOptions = {
21
+ indent_size: 2,
22
+ indent_char: " ",
23
+ keep_array_indentation: false,
24
+ break_chained_methods: false,
25
+ indent_scripts: "normal",
26
+ brace_style: "collapse",
27
+ space_before_conditional: true,
28
+ unescape_strings: false,
29
+ jslint_happy: false,
30
+ end_with_newline: false,
31
+ wrap_line_length: 0,
32
+ indent_inner_html: false,
33
+ comma_first: false,
34
+ e4x: false,
35
+ indent_empty_lines: false,
36
+ wrap_attributes: "force",
37
+ max_preserve_newlines: 5,
38
+ preserve_newlines: true
39
+ };
40
+ const updateResourcePath = ($, tagName, attr, addHash2) => {
41
+ $(tagName).each((_2, el) => {
42
+ const href = $(el).attr(attr);
43
+ if (href && href.startsWith("/")) {
44
+ let newPath = href;
45
+ if (process.env.VITE_DOMAIN) {
46
+ newPath = process.env.VITE_DOMAIN + newPath;
47
+ }
48
+ if (href.startsWith(xpackEnv.VITE_BASE_URL) && !href.startsWith(xpackEnv.VITE_BASE_URL + "assets/vendors/") && [".css", ".ico", ".js", ".webmanifest", ".svg"].includes(path.extname(href).toLowerCase()) && !/\.0x[a-z0-9]{8}\.\w+$/gi.test(href)) {
49
+ const path2 = toAbsolute("dist/static/" + href.substring(xpackEnv.VITE_BASE_URL.length));
50
+ if (fs.existsSync(path2)) {
51
+ const content = fs.readFileSync(path2);
52
+ const sha1Hash = crypto.createHash("sha1");
53
+ sha1Hash.update(content);
54
+ const hash = sha1Hash.digest("base64url").substring(0, 10);
55
+ if (addHash2) {
56
+ newPath += "?v=" + hash;
57
+ }
58
+ } else if (path2.endsWith("mock-api.js")) ;
59
+ else {
60
+ log(chalk.yellow("Cannot find:", path2));
56
61
  }
57
- } else if (path2.endsWith("mock-api.js")) ;
58
- else {
59
- log(chalk.yellow("Cannot find:", path2));
62
+ }
63
+ if (newPath != href) {
64
+ $(el).attr(attr, newPath);
60
65
  }
61
66
  }
62
- if (newPath != href) {
63
- $(el).attr(attr, newPath);
67
+ });
68
+ };
69
+ const removeStyleBase = ($) => {
70
+ $('link[rel="stylesheet"]').each((_2, el) => {
71
+ const href = $(el).attr("href");
72
+ if (href?.includes("style-base")) {
73
+ $(el).remove();
64
74
  }
65
- }
66
- });
67
- };
68
- const removeStyleBase = ($) => {
69
- $('link[rel="stylesheet"]').each((_2, el) => {
70
- const href = $(el).attr("href");
71
- if (href?.includes("style-base")) {
72
- $(el).remove();
73
- }
74
- });
75
- };
76
- const removeDuplicateAssets = ($, selector, attr, paths) => {
77
- $(selector).each((_2, el) => {
78
- if ($(el).attr("data-pl-inplace") === "true") {
79
- return;
80
- }
81
- const path2 = $(el).attr(attr);
82
- if (!path2) {
83
- return;
84
- }
85
- const index = $(el).index();
86
- const parent = $(el).parent().clone();
87
- const child = parent.children()[index];
88
- parent.empty();
89
- parent.append(child);
90
- const html = parent.html();
91
- $(el).after("\n<!-- " + html + " -->");
92
- if (paths.includes(path2)) {
93
- $(el).remove();
94
- return;
95
- }
96
- paths.push(path2);
97
- $(el).removeAttr("data-pl-require");
98
- if ($(el).attr("type") === "module") {
99
- const deferValue = $(el).attr("defer");
100
- if ($(el).attr("defer") === "" || deferValue === "defer" || deferValue === "true") {
101
- $(el).removeAttr("defer");
75
+ });
76
+ };
77
+ const removeDuplicateAssets = ($, selector, attr, paths) => {
78
+ $(selector).each((_2, el) => {
79
+ if ($(el).attr("data-pl-inplace") === "true") {
80
+ return;
102
81
  }
82
+ const path2 = $(el).attr(attr);
83
+ if (!path2) {
84
+ return;
85
+ }
86
+ const index = $(el).index();
87
+ const parent = $(el).parent().clone();
88
+ const child = parent.children()[index];
89
+ parent.empty();
90
+ parent.append(child);
91
+ const html = parent.html();
92
+ $(el).after("\n<!-- " + html + " -->");
93
+ if (paths.includes(path2)) {
94
+ $(el).remove();
95
+ return;
96
+ }
97
+ paths.push(path2);
98
+ $(el).removeAttr("data-pl-require");
99
+ if ($(el).attr("type") === "module") {
100
+ const deferValue = $(el).attr("defer");
101
+ if ($(el).attr("defer") === "" || deferValue === "defer" || deferValue === "true") {
102
+ $(el).removeAttr("defer");
103
+ }
104
+ }
105
+ $("head").append(el);
106
+ });
107
+ };
108
+ const viteAbsoluteUrl = (remain, addExtension = false) => {
109
+ const baseUrl = xpackEnv.VITE_BASE_URL;
110
+ const normalizedRemain = (remain?.startsWith("/") ? remain : "/" + remain) + (addExtension && !remain.endsWith("/") ? xpackEnv.VITE_PATH_EXTENSION ?? "" : "");
111
+ if (!baseUrl) {
112
+ return normalizedRemain;
103
113
  }
104
- $("head").append(el);
105
- });
106
- };
107
- const viteAbsoluteUrl = (remain, addExtension = false) => {
108
- const baseUrl = xpackEnv.VITE_BASE_URL;
109
- const normalizedRemain = (remain?.startsWith("/") ? remain : "/" + remain) + (addExtension && !remain.endsWith("/") ? xpackEnv.VITE_PATH_EXTENSION ?? "" : "");
110
- if (!baseUrl) {
111
- return normalizedRemain;
112
- }
113
- if (!baseUrl.endsWith("/")) {
114
- return baseUrl + normalizedRemain;
115
- }
116
- const len = baseUrl.length;
117
- return baseUrl.substring(0, len - 1) + normalizedRemain;
118
- };
119
- const renderPage = async (renderedPages, addHash) => {
120
- for (const route of routesToPrerender) {
121
- const output = await render(viteAbsoluteUrl(route.route, true));
122
- const destLocalizedFolderPath = toAbsolute("dist/static");
123
- let html = template.replace("<!--app-html-->", output.html ?? "").replace("@style.scss", "/assets/css/" + route.name + ".css");
124
- const $ = cheerio.load(html);
125
- const paths = [];
126
- removeDuplicateAssets($, "link[data-pl-require][href]", "href", paths);
127
- removeDuplicateAssets($, "script[data-pl-require][src]", "src", paths);
128
- updateResourcePath($, "link", "href", addHash);
129
- updateResourcePath($, "script", "src", addHash);
130
- updateResourcePath($, "img", "src", addHash);
131
- if (route.route === "/") {
132
- removeStyleBase($);
114
+ if (!baseUrl.endsWith("/")) {
115
+ return baseUrl + normalizedRemain;
133
116
  }
134
- $("head title").text(route.name);
135
- const fileName = (route.route === "/" ? "/index" : route.route) + ".html";
136
- const filePath = `${destLocalizedFolderPath}${fileName}`;
137
- if (!fs.existsSync(path.dirname(filePath))) {
138
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
117
+ const len = baseUrl.length;
118
+ return baseUrl.substring(0, len - 1) + normalizedRemain;
119
+ };
120
+ const renderPage = async (renderedPages2, addHash2) => {
121
+ for (const route of routesToPrerender) {
122
+ const output = await render(viteAbsoluteUrl(route.route, true));
123
+ const destLocalizedFolderPath = toAbsolute("dist/static");
124
+ let html = template.replace("<!--app-html-->", output.html ?? "").replace("@style.scss", "/assets/css/" + route.name + ".css");
125
+ const $ = cheerio.load(html);
126
+ const paths = [];
127
+ removeDuplicateAssets($, "link[data-pl-require][href]", "href", paths);
128
+ removeDuplicateAssets($, "script[data-pl-require][src]", "src", paths);
129
+ updateResourcePath($, "link", "href", addHash2);
130
+ updateResourcePath($, "script", "src", addHash2);
131
+ updateResourcePath($, "img", "src", addHash2);
132
+ if (route.route === "/") {
133
+ removeStyleBase($);
134
+ }
135
+ $("head title").text(route.name);
136
+ const fileName = (route.route === "/" ? "/index" : route.route) + ".html";
137
+ const filePath = `${destLocalizedFolderPath}${fileName}`;
138
+ if (!fs.existsSync(path.dirname(filePath))) {
139
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
140
+ }
141
+ html = $.html();
142
+ html = jsBeautify.html_beautify(html, beautifyOptions);
143
+ html = html.replace("/* app-styles */", output.styles);
144
+ fs.writeFileSync(toAbsolute(filePath), html);
145
+ log("pre-rendered:", slash(filePath));
146
+ renderedPages2.push({
147
+ name: _.kebabCase(fileName.replaceAll(/\.\w+$/gi, "")),
148
+ url: `${process.env.VITE_DOMAIN ?? ""}${fileName}`,
149
+ fileName
150
+ });
139
151
  }
140
- html = $.html();
141
- html = jsBeautify.html_beautify(html, beautifyOptions);
142
- html = html.replace("/* app-styles */", output.styles);
143
- fs.writeFileSync(toAbsolute(filePath), html);
144
- log("pre-rendered:", slash(filePath));
145
- renderedPages.push({
146
- name: _.kebabCase(fileName.replaceAll(/\.\w+$/gi, "")),
147
- url: `${process.env.VITE_DOMAIN ?? ""}${fileName}`,
148
- fileName
149
- });
150
- }
151
- };
152
- (async () => {
152
+ };
153
153
  const renderedPages = [];
154
154
  const pool = [];
155
- const addHash = !!process.argv.includes("--add-hash");
156
155
  pool.push(renderPage(renderedPages, addHash));
157
156
  await Promise.all(pool);
158
- })();
157
+ };
158
+ const isDirectRun = process.argv[1]?.endsWith("prerender.js") || process.argv[1]?.endsWith("prerender.ts");
159
+ if (isDirectRun) {
160
+ const argvModeIndex = process.argv.indexOf("--mode");
161
+ const mode = argvModeIndex >= 0 && argvModeIndex < process.argv.length - 1 && !process.argv[argvModeIndex + 1].startsWith("-") ? process.argv[argvModeIndex + 1] : "production";
162
+ runPrerender({ addHash: process.argv.includes("--add-hash"), mode });
163
+ }
164
+ export {
165
+ runPrerender
166
+ };
package/dist/server.js CHANGED
@@ -8,11 +8,11 @@ import fs from "fs";
8
8
  import path from "path";
9
9
  import * as cheerio from "cheerio";
10
10
  import jsBeautify from "js-beautify";
11
- const createViteDevServer = ({ root: root2, baseUrl, hmrPort, isTest: isTest2 }) => {
11
+ const createViteDevServer = ({ root, baseUrl, hmrPort, isTest }) => {
12
12
  const server = createServer$1({
13
- root: root2,
13
+ root,
14
14
  base: baseUrl,
15
- logLevel: isTest2 ? "error" : "info",
15
+ logLevel: isTest ? "error" : "info",
16
16
  server: {
17
17
  middlewareMode: true,
18
18
  watch: {
@@ -121,17 +121,17 @@ const _useRenderer = ({ app, indexProd, isProd: isProd2, viteDevServer, resolve
121
121
  }
122
122
  });
123
123
  };
124
- const argvModeIndex$1 = process.argv.indexOf("--mode");
125
- const mode$1 = argvModeIndex$1 >= 0 && argvModeIndex$1 < process.argv.length - 1 && !process.argv[argvModeIndex$1 + 1].startsWith("-") ? process.argv[argvModeIndex$1 + 1] : "production";
124
+ const argvModeIndex = process.argv.indexOf("--mode");
125
+ const mode = argvModeIndex >= 0 && argvModeIndex < process.argv.length - 1 && !process.argv[argvModeIndex + 1].startsWith("-") ? process.argv[argvModeIndex + 1] : "production";
126
126
  process.env.MY_CUSTOM_SECRET = "API_KEY_4c2928b5a14b475d94c3579cbea06178";
127
127
  const isProd = process.env.NODE_ENV === "production";
128
- const createServer = async ({ root: root2, hmrPort, baseUrl, isTest: isTest2 }) => {
129
- const resolve = (p) => path$1.join(root2, p);
128
+ const createServer = async ({ root, hmrPort, baseUrl, isTest }) => {
129
+ const resolve = (p) => path$1.join(root, p);
130
130
  const indexProd = isProd ? fs$1.readFileSync(resolve("index.html"), "utf-8") : "";
131
131
  const app = express();
132
132
  let viteDevServer;
133
133
  if (!isProd) {
134
- viteDevServer = await createViteDevServer({ root: root2, baseUrl, hmrPort, isTest: isTest2 });
134
+ viteDevServer = await createViteDevServer({ root, baseUrl, hmrPort, isTest });
135
135
  app.use(viteDevServer.middlewares);
136
136
  }
137
137
  app.use("/assets/images", serveStatic(resolve("public/assets/images"), { index: false }));
@@ -147,25 +147,35 @@ const createServer = async ({ root: root2, hmrPort, baseUrl, isTest: isTest2 })
147
147
  const startServer = (props) => {
148
148
  createServer(props).then(({ app }) => {
149
149
  app.listen(props.port, () => {
150
- const xpackEnv2 = loadEnv(mode$1, props.root);
151
- console.log("Running on " + chalk.green("http://localhost:" + props.port + xpackEnv2.VITE_BASE_URL));
150
+ const xpackEnv = loadEnv(mode, props.root);
151
+ console.log("Running on " + chalk.green("http://localhost:" + props.port + xpackEnv.VITE_BASE_URL));
152
152
  });
153
153
  });
154
154
  };
155
- console.log("[INIT] server");
156
- const argvModeIndex = process.argv.indexOf("--mode");
157
- const mode = argvModeIndex >= 0 && argvModeIndex < process.argv.length - 1 && !process.argv[argvModeIndex + 1].startsWith("-") ? process.argv[argvModeIndex + 1] : "production";
158
- const root = process.cwd();
159
- const xpackEnv = loadEnv(mode, root);
160
- const isTest = !!xpackEnv.VITE_TEST_BUILD || process.env.NODE_ENV === "test";
161
- const port = xpackEnv.VITE_PORT ? parseInt(xpackEnv.VITE_PORT) : 5e3;
162
- if (!isTest) {
163
- console.log(root);
164
- startServer({
165
- root,
166
- isTest,
167
- port,
168
- hmrPort: port + 1,
169
- baseUrl: xpackEnv.VITE_BASE_URL
170
- });
155
+ const runServer = (options = {}) => {
156
+ const mode2 = options.mode ?? "production";
157
+ console.log("[INIT] server");
158
+ const root = process.cwd();
159
+ const xpackEnv = loadEnv(mode2, root);
160
+ const isTest = !!xpackEnv.VITE_TEST_BUILD || process.env.NODE_ENV === "test";
161
+ const port = xpackEnv.VITE_PORT ? parseInt(xpackEnv.VITE_PORT) : 5e3;
162
+ if (!isTest) {
163
+ console.log(root);
164
+ startServer({
165
+ root,
166
+ isTest,
167
+ port,
168
+ hmrPort: port + 1,
169
+ baseUrl: xpackEnv.VITE_BASE_URL
170
+ });
171
+ }
172
+ };
173
+ const isDirectRun = process.argv[1]?.endsWith("server.js") || process.argv[1]?.endsWith("server.ts");
174
+ if (isDirectRun) {
175
+ const argvModeIndex2 = process.argv.indexOf("--mode");
176
+ const mode2 = argvModeIndex2 >= 0 && argvModeIndex2 < process.argv.length - 1 && !process.argv[argvModeIndex2 + 1].startsWith("-") ? process.argv[argvModeIndex2 + 1] : "production";
177
+ runServer({ mode: mode2 });
171
178
  }
179
+ export {
180
+ runServer
181
+ };
package/dist/states.js CHANGED
@@ -2,40 +2,49 @@ import fs from "fs";
2
2
  import chokidar from "chokidar";
3
3
  import debounce from "debounce";
4
4
  import { glob } from "glob";
5
- const isWatch = process.argv.includes("--watch");
6
- const log = console.log.bind(console);
7
- const states = {};
8
- const buildStates = debounce(() => {
9
- const output = [];
10
- const keys = Object.keys(states);
11
- [].forEach.call(keys, (key) => {
12
- const state = states[key];
13
- if (!state) {
14
- return;
15
- }
16
- try {
17
- output.push(JSON.parse(state));
18
- } catch (error) {
19
- console.log(error);
20
- }
21
- });
22
- const json = JSON.stringify(output, null, " ");
23
- fs.writeFileSync("public/pl-states.json", json);
24
- }, 500);
25
- const setStates = (statePath) => {
26
- const state = fs.readFileSync(statePath, "utf-8");
27
- states[statePath] = state;
28
- buildStates();
5
+ const runStates = (options = {}) => {
6
+ const isWatch = options.watch ?? false;
7
+ const log = console.log.bind(console);
8
+ const states = {};
9
+ const buildStates = debounce(() => {
10
+ const output = [];
11
+ const keys = Object.keys(states);
12
+ [].forEach.call(keys, (key) => {
13
+ const state = states[key];
14
+ if (!state) {
15
+ return;
16
+ }
17
+ try {
18
+ output.push(JSON.parse(state));
19
+ } catch (error) {
20
+ console.log(error);
21
+ }
22
+ });
23
+ const json = JSON.stringify(output, null, " ");
24
+ fs.writeFileSync("public/pl-states.json", json);
25
+ }, 500);
26
+ const setStates = (statePath) => {
27
+ const state = fs.readFileSync(statePath, "utf-8");
28
+ states[statePath] = state;
29
+ buildStates();
30
+ };
31
+ const removeStates = (statePath) => {
32
+ delete states[statePath];
33
+ buildStates();
34
+ };
35
+ if (isWatch) {
36
+ const watcher = chokidar.watch("src/**/*.states.json");
37
+ watcher.on("ready", () => {
38
+ log("States are ready!");
39
+ }).on("add", (path) => setStates(path)).on("change", (path) => setStates(path)).on("unlink", (path) => removeStates(path));
40
+ } else {
41
+ glob.sync("src/**/*.states.json").forEach((path) => setStates(path));
42
+ }
29
43
  };
30
- const removeStates = (statePath) => {
31
- delete states[statePath];
32
- buildStates();
33
- };
34
- if (isWatch) {
35
- const watcher = chokidar.watch("src/**/*.states.json");
36
- watcher.on("ready", () => {
37
- log("States are ready!");
38
- }).on("add", (path) => setStates(path)).on("change", (path) => setStates(path)).on("unlink", (path) => removeStates(path));
39
- } else {
40
- glob.sync("src/**/*.states.json").forEach((path) => setStates(path));
44
+ const isDirectRun = process.argv[1]?.endsWith("states.js") || process.argv[1]?.endsWith("states.ts");
45
+ if (isDirectRun) {
46
+ runStates({ watch: process.argv.includes("--watch") });
41
47
  }
48
+ export {
49
+ runStates
50
+ };