@shopify/cli-hydrogen 8.0.2 → 8.0.4

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,228 +0,0 @@
1
- import path from 'node:path';
2
- import { fileURLToPath } from 'node:url';
3
- import { setH2OVerbose, isH2Verbose, muteDevLogs, enhanceH2Logs } from '../../lib/log.js';
4
- import { commonFlags, overrideFlag, flagsToCamelObject, DEFAULT_INSPECTOR_PORT, DEFAULT_APP_PORT } from '../../lib/flags.js';
5
- import Command from '@shopify/cli-kit/node/base-command';
6
- import colors from '@shopify/cli-kit/node/colors';
7
- import { collectLog } from '@shopify/cli-kit/node/output';
8
- import { renderSuccess } from '@shopify/cli-kit/node/ui';
9
- import { AbortError } from '@shopify/cli-kit/node/error';
10
- import { Flags } from '@oclif/core';
11
- import { spawnCodegenProcess } from '../../lib/codegen.js';
12
- import { getAllEnvironmentVariables } from '../../lib/environment-variables.js';
13
- import { displayDevUpgradeNotice } from './upgrade.js';
14
- import { prepareDiffDirectory } from '../../lib/template-diff.js';
15
- import { getDevConfigInBackground, TUNNEL_DOMAIN, startTunnelAndPushConfig, getUtilityBannerlines, getDebugBannerLine, isMockShop, notifyIssueWithTunnelAndMockShop } from '../../lib/dev-shared.js';
16
- import { getCliCommand } from '../../lib/shell.js';
17
- import { findPort } from '../../lib/find-port.js';
18
- import { logRequestLine } from '../../lib/mini-oxygen/common.js';
19
- import { findHydrogenPlugin, findOxygenPlugin } from '../../lib/vite-config.js';
20
-
21
- class DevVite extends Command {
22
- static description = "Runs Hydrogen storefront in an Oxygen worker for development.";
23
- static flags = {
24
- ...commonFlags.path,
25
- ...commonFlags.entry,
26
- ...overrideFlag(commonFlags.port, {
27
- port: { default: void 0, required: false }
28
- }),
29
- ...commonFlags.codegen,
30
- "disable-virtual-routes": Flags.boolean({
31
- description: "Disable rendering fallback routes when a route file doesn't exist.",
32
- env: "SHOPIFY_HYDROGEN_FLAG_DISABLE_VIRTUAL_ROUTES"
33
- }),
34
- ...commonFlags.debug,
35
- ...commonFlags.inspectorPort,
36
- host: Flags.boolean({
37
- description: "Expose the server to the network",
38
- default: false,
39
- required: false
40
- }),
41
- ...commonFlags.env,
42
- ...commonFlags.envBranch,
43
- "disable-version-check": Flags.boolean({
44
- description: "Skip the version check when running `hydrogen dev`",
45
- default: false,
46
- required: false
47
- }),
48
- ...commonFlags.diff,
49
- ...commonFlags.customerAccountPush,
50
- ...commonFlags.verbose
51
- };
52
- static hidden = true;
53
- async run() {
54
- const { flags } = await this.parse(DevVite);
55
- let directory = flags.path ? path.resolve(flags.path) : process.cwd();
56
- if (flags.diff) {
57
- directory = await prepareDiffDirectory(directory, true);
58
- }
59
- await runViteDev({
60
- ...flagsToCamelObject(flags),
61
- customerAccountPush: flags["customer-account-push__unstable"],
62
- path: directory,
63
- isLocalDev: flags.diff,
64
- cliConfig: this.config
65
- });
66
- }
67
- }
68
- async function runViteDev({
69
- entry: ssrEntry,
70
- port: appPort,
71
- path: appPath,
72
- host,
73
- codegen: useCodegen = false,
74
- codegenConfigPath,
75
- disableVirtualRoutes,
76
- envBranch,
77
- env: envHandle,
78
- debug = false,
79
- disableVersionCheck = false,
80
- inspectorPort,
81
- isLocalDev = false,
82
- customerAccountPush: customerAccountPushFlag = false,
83
- cliConfig,
84
- verbose
85
- }) {
86
- if (!process.env.NODE_ENV)
87
- process.env.NODE_ENV = "development";
88
- if (verbose)
89
- setH2OVerbose();
90
- if (!isH2Verbose())
91
- muteDevLogs();
92
- const root = appPath ?? process.cwd();
93
- const cliCommandPromise = getCliCommand(root);
94
- const backgroundPromise = getDevConfigInBackground(
95
- root,
96
- customerAccountPushFlag
97
- );
98
- const envPromise = backgroundPromise.then(
99
- ({ fetchRemote, localVariables }) => getAllEnvironmentVariables({
100
- root,
101
- envBranch,
102
- envHandle,
103
- fetchRemote,
104
- localVariables
105
- })
106
- );
107
- if (debug && !inspectorPort) {
108
- inspectorPort = await findPort(DEFAULT_INSPECTOR_PORT);
109
- }
110
- const vite = await import('vite');
111
- const fs = isLocalDev ? { allow: [root, fileURLToPath(new URL("../../../../", import.meta.url))] } : void 0;
112
- const customLogger = vite.createLogger();
113
- if (process.env.SHOPIFY_UNIT_TEST) {
114
- customLogger.info = (msg) => collectLog("info", msg);
115
- customLogger.warn = (msg) => collectLog("warn", msg);
116
- customLogger.error = (msg) => collectLog("error", msg);
117
- }
118
- const viteServer = await vite.createServer({
119
- root,
120
- customLogger,
121
- clearScreen: false,
122
- server: { fs, host: host ? true : void 0 },
123
- plugins: [
124
- {
125
- name: "hydrogen:cli",
126
- configResolved(config) {
127
- findHydrogenPlugin(config)?.api?.registerPluginOptions({
128
- disableVirtualRoutes
129
- });
130
- findOxygenPlugin(config)?.api?.registerPluginOptions({
131
- debug,
132
- entry: ssrEntry,
133
- envPromise: envPromise.then(({ allVariables: allVariables2 }) => allVariables2),
134
- inspectorPort,
135
- logRequestLine
136
- });
137
- },
138
- configureServer: (viteDevServer) => {
139
- if (customerAccountPushFlag) {
140
- viteDevServer.middlewares.use((req, res, next) => {
141
- const host2 = req.headers.host;
142
- if (host2?.includes(TUNNEL_DOMAIN.ORIGINAL)) {
143
- req.headers.host = host2.replace(
144
- TUNNEL_DOMAIN.ORIGINAL,
145
- TUNNEL_DOMAIN.REBRANDED
146
- );
147
- }
148
- next();
149
- });
150
- }
151
- }
152
- }
153
- ]
154
- });
155
- const h2Plugin = findHydrogenPlugin(viteServer.config);
156
- if (!h2Plugin) {
157
- await viteServer.close();
158
- throw new AbortError(
159
- "Hydrogen plugin not found.",
160
- "Add `hydrogen()` plugin to your Vite config."
161
- );
162
- }
163
- const h2PluginOptions = h2Plugin.api?.getPluginOptions?.();
164
- const codegenProcess = useCodegen ? spawnCodegenProcess({
165
- rootDirectory: root,
166
- configFilePath: codegenConfigPath,
167
- appDirectory: h2PluginOptions?.remixConfig?.appDirectory
168
- }) : void 0;
169
- process.on("unhandledRejection", (err) => {
170
- console.log("Unhandled Rejection: ", err);
171
- });
172
- const publicPort = appPort ?? viteServer.config.server.port ?? DEFAULT_APP_PORT;
173
- const [tunnel, cliCommand] = await Promise.all([
174
- backgroundPromise.then(
175
- ({ customerAccountPush, storefrontId }) => customerAccountPush ? startTunnelAndPushConfig(root, cliConfig, publicPort, storefrontId) : void 0
176
- ),
177
- cliCommandPromise,
178
- viteServer.listen(publicPort)
179
- ]);
180
- const publicUrl = new URL(
181
- viteServer.resolvedUrls.local[0] ?? viteServer.resolvedUrls.network[0]
182
- );
183
- const finalHost = tunnel?.host || publicUrl.toString() || publicUrl.origin;
184
- enhanceH2Logs({
185
- rootDirectory: root,
186
- host: finalHost,
187
- cliCommand
188
- });
189
- const { logInjectedVariables, allVariables } = await envPromise;
190
- logInjectedVariables();
191
- console.log("");
192
- viteServer.printUrls();
193
- viteServer.bindCLIShortcuts({ print: true });
194
- console.log("\n");
195
- const customSections = [];
196
- if (!h2PluginOptions?.disableVirtualRoutes) {
197
- customSections.push({ body: getUtilityBannerlines(finalHost) });
198
- }
199
- if (debug && inspectorPort) {
200
- customSections.push({
201
- body: { warn: getDebugBannerLine(inspectorPort) }
202
- });
203
- }
204
- const { storefrontTitle } = await backgroundPromise;
205
- renderSuccess({
206
- body: [
207
- `View ${storefrontTitle ? colors.cyan(storefrontTitle) : "Hydrogen"} app:`,
208
- { link: { url: finalHost } }
209
- ],
210
- customSections
211
- });
212
- if (!disableVersionCheck) {
213
- displayDevUpgradeNotice({ targetPath: root });
214
- }
215
- if (customerAccountPushFlag && isMockShop(allVariables)) {
216
- notifyIssueWithTunnelAndMockShop(cliCommand);
217
- }
218
- return {
219
- getUrl: () => finalHost,
220
- async close() {
221
- codegenProcess?.removeAllListeners("close");
222
- codegenProcess?.kill("SIGINT");
223
- await Promise.allSettled([viteServer.close(), tunnel?.cleanup?.()]);
224
- }
225
- };
226
- }
227
-
228
- export { DevVite as default, runViteDev };