@10up/wp-vite-plugins 1.0.0-alpha.3 → 1.0.0-alpha.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.
- package/LICENSE +338 -0
- package/README.md +84 -58
- package/dist/index.d.ts +400 -366
- package/dist/index.js +4290 -4020
- package/dist/php/wp-boot/Page.php +803 -0
- package/dist/php/wp-boot/Pages.php +133 -0
- package/dist/php/wp-vite-dev-server/Bridge.php +507 -0
- package/dist/php/wp-vite-dev-server/HotFile.php +95 -0
- package/package.json +19 -51
- package/dist/build.d.ts +0 -13
- package/dist/build.js +0 -413
- package/dist/build.js.map +0 -1
- package/dist/cli.js +0 -628
- package/dist/cli.js.map +0 -1
- package/dist/dev.d.ts +0 -35
- package/dist/dev.js +0 -384
- package/dist/dev.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/runner-jUVlAHlZ.d.ts +0 -183
- package/dist/watch.d.ts +0 -12
- package/dist/watch.js +0 -398
- package/dist/watch.js.map +0 -1
package/dist/cli.js
DELETED
|
@@ -1,628 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { realpathSync, readFileSync, existsSync } from 'fs';
|
|
3
|
-
import { dirname, join, resolve, isAbsolute, parse, sep } from 'path';
|
|
4
|
-
import { fileURLToPath } from 'url';
|
|
5
|
-
import { spawn } from 'child_process';
|
|
6
|
-
import { rm } from 'fs/promises';
|
|
7
|
-
|
|
8
|
-
var DEFAULT_BUILD_ARGS = ["build", "--no-emptyOutDir", "--no-clearScreen"];
|
|
9
|
-
var DEFAULT_WATCH_ARGS = ["build", "--watch", "--no-emptyOutDir", "--no-clearScreen"];
|
|
10
|
-
var BIN_CANDIDATES = ["vite", "vp"];
|
|
11
|
-
function spawnPass(options) {
|
|
12
|
-
const { pass, stdout, stderr } = options;
|
|
13
|
-
const child = spawn(options.bin, options.argv, {
|
|
14
|
-
cwd: options.cwd,
|
|
15
|
-
env: {
|
|
16
|
-
...process.env,
|
|
17
|
-
...options.env,
|
|
18
|
-
WP_VITE_RUNNER: options.runner,
|
|
19
|
-
WP_VITE_PASS: pass,
|
|
20
|
-
// Kept for the documented watch-only guard:
|
|
21
|
-
// `emptyOutDir: !isModulePass && !process.env.WP_VITE_UNIFIED_WATCH`.
|
|
22
|
-
...options.runner === "watch" ? { WP_VITE_UNIFIED_WATCH: "1" } : {}
|
|
23
|
-
},
|
|
24
|
-
stdio: options.inherit ? "inherit" : ["ignore", "pipe", "pipe"],
|
|
25
|
-
// .cmd shims are not directly executable on Windows.
|
|
26
|
-
shell: process.platform === "win32"
|
|
27
|
-
});
|
|
28
|
-
if (options.inherit) {
|
|
29
|
-
const result2 = new Promise((settle) => {
|
|
30
|
-
let settled = false;
|
|
31
|
-
const finish = (passResult) => {
|
|
32
|
-
if (settled) return;
|
|
33
|
-
settled = true;
|
|
34
|
-
settle(passResult);
|
|
35
|
-
};
|
|
36
|
-
child.on("error", (error) => {
|
|
37
|
-
stderr.write(`[wp-vite] the ${pass} pass failed to start: ${error.message}
|
|
38
|
-
`);
|
|
39
|
-
finish({ pass, code: null, signal: null, error });
|
|
40
|
-
});
|
|
41
|
-
child.on("close", (code, signal) => finish({ pass, code, signal }));
|
|
42
|
-
});
|
|
43
|
-
return { pass, child, result: result2 };
|
|
44
|
-
}
|
|
45
|
-
const outPrefixer = createLinePrefixer(`[${pass}] `, (text) => stdout.write(text));
|
|
46
|
-
const errPrefixer = createLinePrefixer(`[${pass}] `, (text) => stderr.write(text));
|
|
47
|
-
child.stdout?.setEncoding("utf8");
|
|
48
|
-
child.stdout?.on("data", (chunk) => outPrefixer.write(chunk));
|
|
49
|
-
child.stderr?.setEncoding("utf8");
|
|
50
|
-
child.stderr?.on("data", (chunk) => errPrefixer.write(chunk));
|
|
51
|
-
const result = new Promise((settle) => {
|
|
52
|
-
let settled = false;
|
|
53
|
-
const finish = (passResult) => {
|
|
54
|
-
if (settled) return;
|
|
55
|
-
settled = true;
|
|
56
|
-
outPrefixer.flush();
|
|
57
|
-
errPrefixer.flush();
|
|
58
|
-
settle(passResult);
|
|
59
|
-
};
|
|
60
|
-
child.on("error", (error) => {
|
|
61
|
-
stderr.write(`[wp-vite] the ${pass} pass failed to start: ${error.message}
|
|
62
|
-
`);
|
|
63
|
-
finish({ pass, code: null, signal: null, error });
|
|
64
|
-
});
|
|
65
|
-
child.on("close", (code, signal) => finish({ pass, code, signal }));
|
|
66
|
-
});
|
|
67
|
-
return { pass, child, result };
|
|
68
|
-
}
|
|
69
|
-
function passArgs(base, pass, modulesMode, extra = []) {
|
|
70
|
-
return [...base, ...pass === "modules" ? ["--mode", modulesMode] : [], ...extra];
|
|
71
|
-
}
|
|
72
|
-
function forwardSignals(handler) {
|
|
73
|
-
const bound = ["SIGINT", "SIGTERM"].map(
|
|
74
|
-
(signal) => [signal, () => handler(signal)]
|
|
75
|
-
);
|
|
76
|
-
for (const [signal, listener] of bound) process.on(signal, listener);
|
|
77
|
-
return () => {
|
|
78
|
-
for (const [signal, listener] of bound) process.off(signal, listener);
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
function describeExit(code, signal) {
|
|
82
|
-
if (signal) return `signal ${signal}`;
|
|
83
|
-
if (code === null) return "unknown cause";
|
|
84
|
-
return `exit code ${code}`;
|
|
85
|
-
}
|
|
86
|
-
var SIGNAL_EXIT_CODES = {
|
|
87
|
-
SIGINT: 130,
|
|
88
|
-
SIGTERM: 143,
|
|
89
|
-
SIGHUP: 129,
|
|
90
|
-
SIGQUIT: 131
|
|
91
|
-
};
|
|
92
|
-
function signalExitCode(signal) {
|
|
93
|
-
if (!signal) return 1;
|
|
94
|
-
return SIGNAL_EXIT_CODES[signal] ?? 1;
|
|
95
|
-
}
|
|
96
|
-
function passExitCode(result) {
|
|
97
|
-
if (result.error) return 1;
|
|
98
|
-
if (result.code !== null) return result.code;
|
|
99
|
-
return signalExitCode(result.signal);
|
|
100
|
-
}
|
|
101
|
-
var VITE_CONFIG_FILES = [
|
|
102
|
-
"vite.config.ts",
|
|
103
|
-
"vite.config.mts",
|
|
104
|
-
"vite.config.cts",
|
|
105
|
-
"vite.config.js",
|
|
106
|
-
"vite.config.mjs",
|
|
107
|
-
"vite.config.cjs"
|
|
108
|
-
];
|
|
109
|
-
function detectModulePass(options = {}) {
|
|
110
|
-
if (typeof options.modules === "boolean") {
|
|
111
|
-
return {
|
|
112
|
-
modules: options.modules,
|
|
113
|
-
reason: `explicit --${options.modules ? "modules" : "no-modules"} flag`
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
const cwd = resolve(options.cwd ?? process.cwd());
|
|
117
|
-
const modulesMode = options.modulesMode ?? "modules";
|
|
118
|
-
const escapedMode = escapeRegExp(modulesMode);
|
|
119
|
-
const scriptMatch = new RegExp(`(?:--mode[=\\s]+|(?:^|\\s)-m\\s+)${escapedMode}(?![\\w-])`);
|
|
120
|
-
const packageJson = readJson(join(cwd, "package.json"));
|
|
121
|
-
const scripts = packageJson?.scripts;
|
|
122
|
-
if (scripts && typeof scripts === "object") {
|
|
123
|
-
for (const [name, command] of Object.entries(scripts)) {
|
|
124
|
-
if (typeof command === "string" && scriptMatch.test(command)) {
|
|
125
|
-
return {
|
|
126
|
-
modules: true,
|
|
127
|
-
reason: `package.json script "${name}" runs --mode ${modulesMode}`
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
const configMatch = new RegExp(`(['"\`])${escapedMode}\\1`);
|
|
133
|
-
for (const fileName of VITE_CONFIG_FILES) {
|
|
134
|
-
const filePath = join(cwd, fileName);
|
|
135
|
-
const source = readFileIfExists(filePath);
|
|
136
|
-
if (source === null) continue;
|
|
137
|
-
if (configMatch.test(source)) {
|
|
138
|
-
return { modules: true, reason: `${fileName} mentions the "${modulesMode}" mode` };
|
|
139
|
-
}
|
|
140
|
-
return {
|
|
141
|
-
modules: false,
|
|
142
|
-
reason: `${fileName} never mentions the "${modulesMode}" mode`
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
return {
|
|
146
|
-
modules: false,
|
|
147
|
-
reason: `no package.json script or Vite config mentions the "${modulesMode}" mode`
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
function resolveViteBin(cwd = process.cwd(), candidates = BIN_CANDIDATES) {
|
|
151
|
-
const searched = [];
|
|
152
|
-
for (const binDir of nodeModulesBinDirs(cwd)) {
|
|
153
|
-
for (const name of candidates) {
|
|
154
|
-
for (const fileName of binFileNames(name)) {
|
|
155
|
-
const candidatePath = join(binDir, fileName);
|
|
156
|
-
searched.push(candidatePath);
|
|
157
|
-
if (existsSync(candidatePath)) return { name, path: candidatePath };
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
throw new Error(
|
|
162
|
-
`[wp-vite] could not find a Vite binary (${candidates.join(" or ")}) in any node_modules/.bin above ${cwd}. Install vite in the package, or pass --bin <name|path>. Looked at:
|
|
163
|
-
${searched.join("\n ")}`
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
function resolveBin(bin, cwd) {
|
|
167
|
-
if (!bin) return resolveViteBin(cwd).path;
|
|
168
|
-
if (isAbsolute(bin) || bin.includes("/") || bin.includes("\\")) return bin;
|
|
169
|
-
try {
|
|
170
|
-
return resolveViteBin(cwd, [bin]).path;
|
|
171
|
-
} catch {
|
|
172
|
-
return bin;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
function nodeModulesBinDirs(from) {
|
|
176
|
-
const dirs = [];
|
|
177
|
-
let current = resolve(from);
|
|
178
|
-
const { root } = parse(current);
|
|
179
|
-
while (true) {
|
|
180
|
-
dirs.push(join(current, "node_modules", ".bin"));
|
|
181
|
-
if (current === root) break;
|
|
182
|
-
const parent = dirname(current);
|
|
183
|
-
if (parent === current) break;
|
|
184
|
-
current = parent;
|
|
185
|
-
}
|
|
186
|
-
return dirs;
|
|
187
|
-
}
|
|
188
|
-
function binFileNames(name) {
|
|
189
|
-
return process.platform === "win32" ? [`${name}.cmd`, name] : [name];
|
|
190
|
-
}
|
|
191
|
-
async function cleanOutDir(outDir = "dist", cwd = process.cwd()) {
|
|
192
|
-
const base = resolve(cwd);
|
|
193
|
-
const target = isAbsolute(outDir) ? resolve(outDir) : resolve(base, outDir);
|
|
194
|
-
if (target === parse(target).root || target === base || !target.startsWith(base + sep)) {
|
|
195
|
-
throw new Error(
|
|
196
|
-
`[wp-vite] refusing to clean ${target}: the out dir must be inside ${base}`
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
await rm(target, { recursive: true, force: true });
|
|
200
|
-
return target;
|
|
201
|
-
}
|
|
202
|
-
var ESCAPE = String.fromCharCode(27);
|
|
203
|
-
var ERASE_SEQUENCE = new RegExp(`^(?:${ESCAPE}\\[[0-2]?[KJ])+`);
|
|
204
|
-
function createLinePrefixer(prefix, write) {
|
|
205
|
-
let buffer = "";
|
|
206
|
-
const emit = (line) => {
|
|
207
|
-
const withoutCrlf = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
208
|
-
const rendered = withoutCrlf.split("\r").pop() ?? "";
|
|
209
|
-
const content = rendered.replace(ERASE_SEQUENCE, "");
|
|
210
|
-
write(content.length === 0 ? "\n" : `${prefix}${content}
|
|
211
|
-
`);
|
|
212
|
-
};
|
|
213
|
-
return {
|
|
214
|
-
write(chunk) {
|
|
215
|
-
buffer += chunk;
|
|
216
|
-
const lines = buffer.split("\n");
|
|
217
|
-
buffer = lines.pop() ?? "";
|
|
218
|
-
for (const line of lines) emit(line);
|
|
219
|
-
},
|
|
220
|
-
flush() {
|
|
221
|
-
if (buffer.length === 0) return;
|
|
222
|
-
const pending = buffer;
|
|
223
|
-
buffer = "";
|
|
224
|
-
emit(pending);
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
function parseCliArgs(argv) {
|
|
229
|
-
const parsed = {
|
|
230
|
-
command: void 0,
|
|
231
|
-
options: {},
|
|
232
|
-
help: false,
|
|
233
|
-
version: false,
|
|
234
|
-
unknown: []
|
|
235
|
-
};
|
|
236
|
-
const passthrough = [];
|
|
237
|
-
let index = 0;
|
|
238
|
-
for (; index < argv.length; index += 1) {
|
|
239
|
-
const arg = argv[index];
|
|
240
|
-
if (arg === "--") {
|
|
241
|
-
passthrough.push(...argv.slice(index + 1));
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
if (!arg.startsWith("-")) {
|
|
245
|
-
if (parsed.command === void 0) parsed.command = arg;
|
|
246
|
-
else parsed.unknown.push(arg);
|
|
247
|
-
continue;
|
|
248
|
-
}
|
|
249
|
-
const equalsAt = arg.indexOf("=");
|
|
250
|
-
const flag = equalsAt === -1 ? arg : arg.slice(0, equalsAt);
|
|
251
|
-
const inlineValue = equalsAt === -1 ? void 0 : arg.slice(equalsAt + 1);
|
|
252
|
-
const takeValue = () => {
|
|
253
|
-
if (inlineValue !== void 0) return inlineValue;
|
|
254
|
-
const next = argv[index + 1];
|
|
255
|
-
if (next === void 0 || next.startsWith("-")) return void 0;
|
|
256
|
-
index += 1;
|
|
257
|
-
return next;
|
|
258
|
-
};
|
|
259
|
-
switch (flag) {
|
|
260
|
-
case "-h":
|
|
261
|
-
case "--help":
|
|
262
|
-
parsed.help = true;
|
|
263
|
-
break;
|
|
264
|
-
case "-v":
|
|
265
|
-
case "--version":
|
|
266
|
-
parsed.version = true;
|
|
267
|
-
break;
|
|
268
|
-
case "--out-dir":
|
|
269
|
-
case "--outDir": {
|
|
270
|
-
const value = takeValue();
|
|
271
|
-
if (value === void 0) parsed.unknown.push(`${flag} (missing value)`);
|
|
272
|
-
else parsed.options.outDir = value;
|
|
273
|
-
break;
|
|
274
|
-
}
|
|
275
|
-
case "--modules-mode":
|
|
276
|
-
case "--modulesMode": {
|
|
277
|
-
const value = takeValue();
|
|
278
|
-
if (value === void 0) parsed.unknown.push(`${flag} (missing value)`);
|
|
279
|
-
else parsed.options.modulesMode = value;
|
|
280
|
-
break;
|
|
281
|
-
}
|
|
282
|
-
case "--bin": {
|
|
283
|
-
const value = takeValue();
|
|
284
|
-
if (value === void 0) parsed.unknown.push(`${flag} (missing value)`);
|
|
285
|
-
else parsed.options.bin = value;
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
288
|
-
case "--cwd": {
|
|
289
|
-
const value = takeValue();
|
|
290
|
-
if (value === void 0) parsed.unknown.push(`${flag} (missing value)`);
|
|
291
|
-
else parsed.options.cwd = value;
|
|
292
|
-
break;
|
|
293
|
-
}
|
|
294
|
-
case "--modules":
|
|
295
|
-
parsed.options.modules = true;
|
|
296
|
-
break;
|
|
297
|
-
case "--no-modules":
|
|
298
|
-
parsed.options.modules = false;
|
|
299
|
-
break;
|
|
300
|
-
case "--clean":
|
|
301
|
-
parsed.options.clean = true;
|
|
302
|
-
break;
|
|
303
|
-
case "--no-clean":
|
|
304
|
-
parsed.options.clean = false;
|
|
305
|
-
break;
|
|
306
|
-
default:
|
|
307
|
-
parsed.unknown.push(flag);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
if (passthrough.length > 0) parsed.options.args = passthrough;
|
|
311
|
-
return parsed;
|
|
312
|
-
}
|
|
313
|
-
async function prepareRun(options, runner) {
|
|
314
|
-
const cwd = resolve(options.cwd ?? process.cwd());
|
|
315
|
-
const outDir = options.outDir ?? "dist";
|
|
316
|
-
const modulesMode = options.modulesMode ?? "modules";
|
|
317
|
-
const stdout = options.stdout ?? process.stdout;
|
|
318
|
-
const stderr = options.stderr ?? process.stderr;
|
|
319
|
-
const bin = resolveBin(options.bin, cwd);
|
|
320
|
-
const detection = detectModulePass({ cwd, modulesMode, modules: options.modules });
|
|
321
|
-
const passes = detection.modules ? ["script", "modules"] : ["script"];
|
|
322
|
-
if (options.clean !== false) {
|
|
323
|
-
const cleaned = await cleanOutDir(outDir, cwd);
|
|
324
|
-
stdout.write(`[wp-vite] cleaned ${cleaned}
|
|
325
|
-
`);
|
|
326
|
-
}
|
|
327
|
-
stdout.write(
|
|
328
|
-
`[wp-vite] module pass ${detection.modules ? "enabled" : "disabled"} \u2014 ${detection.reason}; ${runner === "watch" ? "watching" : "building"} ${passes.join(runner === "watch" ? " + " : " then ")} via ${bin}
|
|
329
|
-
`
|
|
330
|
-
);
|
|
331
|
-
return { cwd, bin, modulesMode, passes, stdout, stderr };
|
|
332
|
-
}
|
|
333
|
-
function escapeRegExp(value) {
|
|
334
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
335
|
-
}
|
|
336
|
-
function readFileIfExists(filePath) {
|
|
337
|
-
try {
|
|
338
|
-
return readFileSync(filePath, "utf8");
|
|
339
|
-
} catch {
|
|
340
|
-
return null;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
function readJson(filePath) {
|
|
344
|
-
const source = readFileIfExists(filePath);
|
|
345
|
-
if (source === null) return null;
|
|
346
|
-
try {
|
|
347
|
-
return JSON.parse(source);
|
|
348
|
-
} catch {
|
|
349
|
-
return null;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// src/build.ts
|
|
354
|
-
async function build(options = {}) {
|
|
355
|
-
const { cwd, bin, modulesMode, passes, stdout, stderr } = await prepareRun(options, "build");
|
|
356
|
-
let current;
|
|
357
|
-
let interrupted = false;
|
|
358
|
-
let interruptCode = 0;
|
|
359
|
-
const disposeSignals = options.handleSignals === false ? () => {
|
|
360
|
-
} : forwardSignals((signal) => {
|
|
361
|
-
if (interrupted) return;
|
|
362
|
-
interrupted = true;
|
|
363
|
-
interruptCode = signalExitCode(signal);
|
|
364
|
-
if (current && current.exitCode === null && current.signalCode === null) {
|
|
365
|
-
current.kill(signal);
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
try {
|
|
369
|
-
for (const [index, pass] of passes.entries()) {
|
|
370
|
-
if (interrupted) {
|
|
371
|
-
stderr.write(`[wp-vite] interrupted \u2014 skipping the ${pass} pass
|
|
372
|
-
`);
|
|
373
|
-
return interruptCode;
|
|
374
|
-
}
|
|
375
|
-
const spawned = spawnPass({
|
|
376
|
-
pass,
|
|
377
|
-
runner: "build",
|
|
378
|
-
bin,
|
|
379
|
-
cwd,
|
|
380
|
-
argv: passArgs(
|
|
381
|
-
options.buildArgs ?? DEFAULT_BUILD_ARGS,
|
|
382
|
-
pass,
|
|
383
|
-
modulesMode,
|
|
384
|
-
options.args ?? []
|
|
385
|
-
),
|
|
386
|
-
env: options.env,
|
|
387
|
-
stdout,
|
|
388
|
-
stderr
|
|
389
|
-
});
|
|
390
|
-
current = spawned.child;
|
|
391
|
-
const result = await spawned.result;
|
|
392
|
-
current = void 0;
|
|
393
|
-
const code = passExitCode(result);
|
|
394
|
-
if (code === 0) continue;
|
|
395
|
-
const skipped = passes.slice(index + 1);
|
|
396
|
-
stderr.write(
|
|
397
|
-
`[wp-vite] the ${pass} pass failed (${describeExit(result.code, result.signal)})` + (skipped.length > 0 ? ` \u2014 skipping the ${skipped.join(" and ")} pass` : "") + "\n"
|
|
398
|
-
);
|
|
399
|
-
return code;
|
|
400
|
-
}
|
|
401
|
-
stdout.write(
|
|
402
|
-
passes.length > 1 ? `[wp-vite] built ${passes.join(" + ")} into one dist
|
|
403
|
-
` : `[wp-vite] built the ${passes[0]} pass
|
|
404
|
-
`
|
|
405
|
-
);
|
|
406
|
-
return 0;
|
|
407
|
-
} finally {
|
|
408
|
-
disposeSignals();
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
var DEFAULT_DEV_ARGS = ["dev", "--no-clearScreen"];
|
|
412
|
-
async function dev(options = {}) {
|
|
413
|
-
const cwd = resolve(options.cwd ?? process.cwd());
|
|
414
|
-
const stdout = options.stdout ?? process.stdout;
|
|
415
|
-
const stderr = options.stderr ?? process.stderr;
|
|
416
|
-
const bin = resolveBin(options.bin, cwd);
|
|
417
|
-
if (options.clean === true) {
|
|
418
|
-
const cleaned = await cleanOutDir(options.outDir ?? "dist", cwd);
|
|
419
|
-
stdout.write(`[wp-vite] cleaned ${cleaned}
|
|
420
|
-
`);
|
|
421
|
-
}
|
|
422
|
-
stdout.write(`[wp-vite] starting the dev server via ${bin}
|
|
423
|
-
`);
|
|
424
|
-
let current;
|
|
425
|
-
let interrupted = false;
|
|
426
|
-
const disposeSignals = options.handleSignals === false ? () => {
|
|
427
|
-
} : forwardSignals((signal) => {
|
|
428
|
-
if (interrupted) return;
|
|
429
|
-
interrupted = true;
|
|
430
|
-
if (current && current.exitCode === null && current.signalCode === null) {
|
|
431
|
-
current.kill(signal);
|
|
432
|
-
}
|
|
433
|
-
});
|
|
434
|
-
try {
|
|
435
|
-
const spawned = spawnPass({
|
|
436
|
-
pass: "dev",
|
|
437
|
-
runner: "dev",
|
|
438
|
-
bin,
|
|
439
|
-
cwd,
|
|
440
|
-
argv: [...options.devArgs ?? DEFAULT_DEV_ARGS, ...options.args ?? []],
|
|
441
|
-
env: options.env,
|
|
442
|
-
stdout,
|
|
443
|
-
stderr,
|
|
444
|
-
inherit: options.inherit !== false
|
|
445
|
-
});
|
|
446
|
-
current = spawned.child;
|
|
447
|
-
const result = await spawned.result;
|
|
448
|
-
current = void 0;
|
|
449
|
-
if (interrupted) return 0;
|
|
450
|
-
const code = passExitCode(result);
|
|
451
|
-
if (code !== 0) {
|
|
452
|
-
stderr.write(
|
|
453
|
-
`[wp-vite] the dev server exited (${describeExit(result.code, result.signal)})
|
|
454
|
-
`
|
|
455
|
-
);
|
|
456
|
-
}
|
|
457
|
-
return code;
|
|
458
|
-
} finally {
|
|
459
|
-
disposeSignals();
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
// src/watch.ts
|
|
464
|
-
async function watch(options = {}) {
|
|
465
|
-
const { cwd, bin, modulesMode, passes, stdout, stderr } = await prepareRun(options, "watch");
|
|
466
|
-
const children = [];
|
|
467
|
-
let shuttingDown = false;
|
|
468
|
-
let exitCode = 0;
|
|
469
|
-
const shutdown = (signal) => {
|
|
470
|
-
shuttingDown = true;
|
|
471
|
-
for (const child of children) {
|
|
472
|
-
if (child.exitCode === null && child.signalCode === null) child.kill(signal);
|
|
473
|
-
}
|
|
474
|
-
};
|
|
475
|
-
const disposeSignals = options.handleSignals === false ? () => {
|
|
476
|
-
} : forwardSignals((signal) => {
|
|
477
|
-
if (shuttingDown) return;
|
|
478
|
-
exitCode = 0;
|
|
479
|
-
shutdown(signal);
|
|
480
|
-
});
|
|
481
|
-
const spawned = passes.map(
|
|
482
|
-
(pass) => spawnPass({
|
|
483
|
-
pass,
|
|
484
|
-
runner: "watch",
|
|
485
|
-
bin,
|
|
486
|
-
cwd,
|
|
487
|
-
argv: passArgs(
|
|
488
|
-
options.buildArgs ?? DEFAULT_WATCH_ARGS,
|
|
489
|
-
pass,
|
|
490
|
-
modulesMode,
|
|
491
|
-
options.args ?? []
|
|
492
|
-
),
|
|
493
|
-
env: options.env,
|
|
494
|
-
stdout,
|
|
495
|
-
stderr
|
|
496
|
-
})
|
|
497
|
-
);
|
|
498
|
-
children.push(...spawned.map(({ child }) => child));
|
|
499
|
-
let running = spawned.length;
|
|
500
|
-
try {
|
|
501
|
-
await Promise.all(
|
|
502
|
-
spawned.map(async ({ pass, result }) => {
|
|
503
|
-
const passResult = await result;
|
|
504
|
-
running -= 1;
|
|
505
|
-
if (shuttingDown) return;
|
|
506
|
-
const unexpected = passResult.error != null || passResult.code === null || passResult.code !== 0 || running > 0;
|
|
507
|
-
if (!unexpected) return;
|
|
508
|
-
exitCode = passResult.code && passResult.code !== 0 ? passResult.code : 1;
|
|
509
|
-
stderr.write(
|
|
510
|
-
`[wp-vite] the ${pass} pass exited unexpectedly (${describeExit(passResult.code, passResult.signal)}) \u2014 stopping
|
|
511
|
-
`
|
|
512
|
-
);
|
|
513
|
-
shutdown("SIGTERM");
|
|
514
|
-
})
|
|
515
|
-
);
|
|
516
|
-
return exitCode;
|
|
517
|
-
} finally {
|
|
518
|
-
disposeSignals();
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
// src/cli.ts
|
|
523
|
-
var HELP = `
|
|
524
|
-
wp-vite \u2014 build helpers for @10up/wp-vite-plugins
|
|
525
|
-
|
|
526
|
-
Usage
|
|
527
|
-
$ wp-vite build [options] [-- <extra vite args>]
|
|
528
|
-
$ wp-vite watch [options] [-- <extra vite args>]
|
|
529
|
-
$ wp-vite dev [options] [-- <extra vite args>]
|
|
530
|
-
|
|
531
|
-
\`build\` and \`watch\` drive the two passes of a dual-pass WordPress build \u2014
|
|
532
|
-
the script pass (\`vite build\`) and, when the package has module entries, the
|
|
533
|
-
module pass (\`vite build --mode modules\`) \u2014 from a single process, after
|
|
534
|
-
cleaning the out dir exactly once.
|
|
535
|
-
|
|
536
|
-
build Runs the script pass to completion, then the module pass.
|
|
537
|
-
Fails fast: a failing pass skips the rest and exits with its code.
|
|
538
|
-
watch Runs both passes concurrently as \`--watch\` processes and stays
|
|
539
|
-
alive until interrupted.
|
|
540
|
-
dev Starts ONE \`vite\` dev server (no second pass \u2014 the wpDevServer
|
|
541
|
-
plugin serves classic scripts and Script Modules from the same
|
|
542
|
-
server) and writes the hot file WordPress reads. Does not clean
|
|
543
|
-
the out dir unless you ask with --clean.
|
|
544
|
-
|
|
545
|
-
Options
|
|
546
|
-
--out-dir <dir> Directory cleaned once at startup (default: dist).
|
|
547
|
-
Must match \`build.outDir\` in the Vite config.
|
|
548
|
-
--modules Force the module pass on. (build/watch)
|
|
549
|
-
--no-modules Force the module pass off. (build/watch)
|
|
550
|
-
--modules-mode <mode> \`--mode\` value for the module pass (default: modules).
|
|
551
|
-
--bin <name|path> Vite CLI to run (default: vite, else vp).
|
|
552
|
-
--clean Clean the out dir at startup (dev: off by default).
|
|
553
|
-
--no-clean Skip the startup clean of the out dir.
|
|
554
|
-
--cwd <dir> Package directory to run in (default: cwd).
|
|
555
|
-
-h, --help Show this message.
|
|
556
|
-
-v, --version Show the package version.
|
|
557
|
-
|
|
558
|
-
Examples
|
|
559
|
-
$ wp-vite build
|
|
560
|
-
$ wp-vite watch
|
|
561
|
-
$ wp-vite dev
|
|
562
|
-
$ wp-vite build --bin vp --out-dir build
|
|
563
|
-
$ wp-vite watch -- --minify false
|
|
564
|
-
$ wp-vite dev -- --port 5199
|
|
565
|
-
`;
|
|
566
|
-
async function runCli(argv) {
|
|
567
|
-
const parsed = parseCliArgs(argv);
|
|
568
|
-
if (parsed.version) {
|
|
569
|
-
process.stdout.write(`${readPackageVersion()}
|
|
570
|
-
`);
|
|
571
|
-
return 0;
|
|
572
|
-
}
|
|
573
|
-
if (parsed.help || parsed.command === void 0) {
|
|
574
|
-
process.stdout.write(`${HELP.trim()}
|
|
575
|
-
`);
|
|
576
|
-
return parsed.command === void 0 && !parsed.help ? 1 : 0;
|
|
577
|
-
}
|
|
578
|
-
if (parsed.unknown.length > 0) {
|
|
579
|
-
process.stderr.write(`[wp-vite] unknown argument(s): ${parsed.unknown.join(", ")}
|
|
580
|
-
`);
|
|
581
|
-
process.stderr.write(`${HELP.trim()}
|
|
582
|
-
`);
|
|
583
|
-
return 1;
|
|
584
|
-
}
|
|
585
|
-
if (parsed.command !== "build" && parsed.command !== "watch" && parsed.command !== "dev") {
|
|
586
|
-
process.stderr.write(`[wp-vite] unknown command: ${parsed.command}
|
|
587
|
-
`);
|
|
588
|
-
process.stderr.write(`${HELP.trim()}
|
|
589
|
-
`);
|
|
590
|
-
return 1;
|
|
591
|
-
}
|
|
592
|
-
try {
|
|
593
|
-
if (parsed.command === "build") return await build(parsed.options);
|
|
594
|
-
if (parsed.command === "watch") return await watch(parsed.options);
|
|
595
|
-
return await dev(parsed.options);
|
|
596
|
-
} catch (error) {
|
|
597
|
-
process.stderr.write(`${error instanceof Error ? error.message : String(error)}
|
|
598
|
-
`);
|
|
599
|
-
return 1;
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
function readPackageVersion() {
|
|
603
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
604
|
-
for (const candidate of [join(here, "..", "package.json"), join(here, "package.json")]) {
|
|
605
|
-
try {
|
|
606
|
-
const parsed = JSON.parse(readFileSync(candidate, "utf8"));
|
|
607
|
-
if (typeof parsed.version === "string") return parsed.version;
|
|
608
|
-
} catch {
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
return "unknown";
|
|
612
|
-
}
|
|
613
|
-
function isEntryPoint() {
|
|
614
|
-
const entry = process.argv[1];
|
|
615
|
-
if (!entry) return false;
|
|
616
|
-
try {
|
|
617
|
-
return realpathSync(fileURLToPath(import.meta.url)) === realpathSync(entry);
|
|
618
|
-
} catch {
|
|
619
|
-
return false;
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
if (isEntryPoint()) {
|
|
623
|
-
process.exitCode = await runCli(process.argv.slice(2));
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
export { runCli };
|
|
627
|
-
//# sourceMappingURL=cli.js.map
|
|
628
|
-
//# sourceMappingURL=cli.js.map
|