@coraltravelcenter/b2c-landing-builder 3.0.0 → 3.2.0

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.
@@ -6,7 +6,9 @@ import {cleanDir, ensureDir, existsFile, normalizeHtml, r, readBlocks} from "./_
6
6
  import {loadProjectVuePlugin} from "./loadProjectVuePlugin.mjs";
7
7
  import {processMarkup} from "./processMarkup.mjs";
8
8
  import {renderPugFile} from "./pugMarkup.mjs";
9
+ import {resolveMarkupFile} from "../config/markup-stack.mjs";
9
10
  import {resolveScriptStack} from "../config/script-stack.mjs";
11
+ import {hasEmptyDefaultInitializer} from "../config/validate-script-contract.mjs";
10
12
 
11
13
  const ORDER_FILE = r("src/order.json");
12
14
 
@@ -43,7 +45,7 @@ async function bundleCssInline(absCssPath) {
43
45
  write: false,
44
46
  minify: "oxc",
45
47
  cssCodeSplit: true,
46
- rollupOptions: {input: V_ID},
48
+ rolldownOptions: {input: V_ID},
47
49
  },
48
50
  });
49
51
 
@@ -63,6 +65,11 @@ async function bundleCssInline(absCssPath) {
63
65
 
64
66
  async function bundleJsInline(absJsPath, stack) {
65
67
  const rel = "/" + path.relative(process.cwd(), absJsPath).replaceAll("\\", "/");
68
+ const emptyInitializer = hasEmptyDefaultInitializer({
69
+ source: fs.readFileSync(absJsPath, "utf8"),
70
+ filePath: absJsPath,
71
+ extension: resolveScriptStack(stack.script).extension,
72
+ });
66
73
 
67
74
  const V_ID = "virtual:cms-js";
68
75
  const R_ID = "\0" + V_ID;
@@ -75,6 +82,7 @@ async function bundleJsInline(absJsPath, stack) {
75
82
  },
76
83
  load(id) {
77
84
  if (id === R_ID) {
85
+ if (emptyInitializer) return `import ${JSON.stringify(rel)};`;
78
86
  // контракт: scripts/<key>.js экспортирует default init()
79
87
  return `
80
88
  import init from ${JSON.stringify(rel)};
@@ -95,7 +103,7 @@ try { if (typeof init === "function") init(); } catch (e) { console.warn(e); }
95
103
  write: false,
96
104
  minify: "oxc",
97
105
  cssCodeSplit: false,
98
- rollupOptions: {
106
+ rolldownOptions: {
99
107
  input: V_ID,
100
108
  output: {format: "iife"},
101
109
  },
@@ -122,16 +130,16 @@ try { if (typeof init === "function") init(); } catch (e) { console.warn(e); }
122
130
 
123
131
  async function buildBlock(key, stack) {
124
132
  const scriptStack = resolveScriptStack(stack.script);
125
- const markupPath = path.join(MARKUP_DIR, `${key}.${stack.markup}`);
126
- if (!markupPath) return null;
127
- if (!existsFile(markupPath)) return null;
133
+ const markup = resolveMarkupFile({directory: MARKUP_DIR, key, markup: stack.markup});
134
+ if (!markup) return null;
135
+ const markupPath = markup.filePath;
128
136
 
129
137
  const jsPath = path.join(SCRIPTS_DIR, `${key}.${scriptStack.extension}`);
130
138
 
131
- const markup = stack.markup === "pug"
139
+ const source = markup.format === "pug"
132
140
  ? renderPugFile(markupPath)
133
141
  : normalizeHtml(fs.readFileSync(markupPath, "utf8"));
134
- const html = processMarkup(markup, {filePath: markupPath});
142
+ const html = processMarkup(source, {filePath: markupPath});
135
143
 
136
144
  const configuredCssPath = path.join(STYLES_DIR, `${key}.${stack.styles}`);
137
145
  const cssPath = existsFile(configuredCssPath) ? configuredCssPath : null;
@@ -4,6 +4,7 @@ import path from "node:path";
4
4
  import {existsFile, normalizeHtml, readBlocks} from "./_utils.mjs";
5
5
  import {processMarkup} from "./processMarkup.mjs";
6
6
  import {getPugConfigFile, renderPugFile} from "./pugMarkup.mjs";
7
+ import {resolveMarkupFile} from "../config/markup-stack.mjs";
7
8
  import {resolveScriptStack} from "../config/script-stack.mjs";
8
9
 
9
10
  const VIRTUAL_ID = "virtual:landing-blocks";
@@ -75,9 +76,9 @@ export function landingBlocksPlugin({root = process.cwd(), stack} = {}) {
75
76
  throw new Error(`[landing-blocks] Invalid block key: "${key}"`);
76
77
  }
77
78
 
78
- const markupPath = path.join(markupDir, `${key}.${stack.markup}`);
79
- if (!markupPath) return;
80
- if (!existsFile(markupPath)) return;
79
+ const markup = resolveMarkupFile({directory: markupDir, key, markup: stack.markup});
80
+ if (!markup) return;
81
+ const markupPath = markup.filePath;
81
82
 
82
83
  this.addWatchFile(markupPath);
83
84
 
@@ -97,7 +98,7 @@ export function landingBlocksPlugin({root = process.cwd(), stack} = {}) {
97
98
  imports.push(`import ${init} from ${JSON.stringify(toVitePath(scriptPath))};`);
98
99
  }
99
100
 
100
- const source = stack.markup === "pug"
101
+ const source = markup.format === "pug"
101
102
  ? renderPugFile(markupPath)
102
103
  : normalizeHtml(fs.readFileSync(markupPath, "utf8"));
103
104
  const html = processMarkup(source, {
@@ -5,6 +5,8 @@ import {fileURLToPath} from "node:url";
5
5
  import {createServer} from "vite";
6
6
  import open from "open";
7
7
 
8
+ import {readOrder} from "../blocks/utils.mjs";
9
+ import {resolveDevPath} from "../config/dev-path.mjs";
8
10
  import {loadConfig} from "../config/load-config.mjs";
9
11
  import {resolvePreset} from "../config/resolve-preset.mjs";
10
12
  import {landingBlocksPlugin} from "../lib/landingBlocksPlugin.mjs";
@@ -55,17 +57,24 @@ export function saveDevServerPort(root, port) {
55
57
  fs.writeFileSync(filePath, `${port}\n`);
56
58
  }
57
59
 
58
- export function createTargetUrl(preset) {
59
- return `https://${preset.domain}/monkey/`;
60
+ export function createTargetUrl(preset, devPath) {
61
+ return new URL(resolveDevPath(devPath), `https://${preset.domain}`).href;
62
+ }
63
+
64
+ function createTargetMatches(preset, devPath) {
65
+ const target = new URL(createTargetUrl(preset, devPath));
66
+ const page = `${target.origin}${target.pathname}`;
67
+ return target.pathname === "/monkey/" ? [`${page}*`] : [page, `${page}?*`];
60
68
  }
61
69
 
62
70
  export async function presentTargetPage({
63
71
  preset,
72
+ devPath,
64
73
  shouldOpen = true,
65
74
  opener = open,
66
75
  log = console.log,
67
76
  } = {}) {
68
- const targetUrl = createTargetUrl(preset);
77
+ const targetUrl = createTargetUrl(preset, devPath);
69
78
  log(` ➜ Target: ${targetUrl}`);
70
79
  if (!shouldOpen) return targetUrl;
71
80
 
@@ -93,6 +102,7 @@ async function loadMonkeyPlugin() {
93
102
  export async function createViteConfig(root = process.cwd(), options = {}) {
94
103
  const config = options.config || await loadConfig(root);
95
104
  const preset = options.preset || resolvePreset(config.site.preset);
105
+ const devPath = resolveDevPath(readOrder(root).order.devPath);
96
106
  const monkey = await loadMonkeyPlugin();
97
107
  const shouldOpen = process.env.B2C_NO_OPEN !== "1";
98
108
  const devPort = resolveDevServerPort(process.env, root);
@@ -119,7 +129,7 @@ export async function createViteConfig(root = process.cwd(), options = {}) {
119
129
  userscript: {
120
130
  name: config.project.name,
121
131
  namespace: `b2c-landing-builder:${preset.id}`,
122
- match: [`https://${preset.domain}/monkey/*`],
132
+ match: createTargetMatches(preset, devPath),
123
133
  grant: "none",
124
134
  "run-at": "document-idle",
125
135
  },
@@ -134,6 +144,7 @@ export async function createViteConfig(root = process.cwd(), options = {}) {
134
144
  export async function startDevServer(root = process.cwd(), {ui} = {}) {
135
145
  const config = await loadConfig(root);
136
146
  const preset = resolvePreset(config.site.preset);
147
+ const devPath = resolveDevPath(readOrder(root).order.devPath);
137
148
  const shouldOpen = process.env.B2C_NO_OPEN !== "1";
138
149
  const viteConfig = await createViteConfig(root, {config, preset});
139
150
  const server = await createServer(viteConfig);
@@ -158,13 +169,14 @@ export async function startDevServer(root = process.cwd(), {ui} = {}) {
158
169
  ui.info(shouldOpen ? "Userscript installation page opened" : "Browser opening disabled · B2C_NO_OPEN=1");
159
170
  await presentTargetPage({
160
171
  preset,
172
+ devPath,
161
173
  shouldOpen,
162
174
  log: (message) => message.includes("Could not open") ? ui.warning(message.trim()) : ui.success(message.trim()),
163
175
  });
164
176
  ui.step("Watching for changes · press Ctrl+C to stop");
165
177
  } else {
166
178
  server.printUrls();
167
- await presentTargetPage({preset, shouldOpen});
179
+ await presentTargetPage({preset, devPath, shouldOpen});
168
180
  }
169
181
  return server;
170
182
  }