@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
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared internals for the `wp-vite` commands.
|
|
3
|
-
*
|
|
4
|
-
* `wp-vite build` and `wp-vite watch` are the same idea run two ways: take a
|
|
5
|
-
* dual-pass project (`vite build` for classic scripts, `vite build --mode
|
|
6
|
-
* modules` for Script Modules), clean the out dir once, then drive both passes
|
|
7
|
-
* — sequentially for a build, concurrently for a watch — as a single process
|
|
8
|
-
* with prefixed output and signal-clean shutdown.
|
|
9
|
-
*
|
|
10
|
-
* Everything they have in common lives here: out-dir cleaning, module-pass
|
|
11
|
-
* detection, Vite binary resolution, child spawning with line prefixing,
|
|
12
|
-
* signal forwarding, and argv parsing. The commands themselves are in
|
|
13
|
-
* `./build` and `./watch`.
|
|
14
|
-
*
|
|
15
|
-
* No Vite internals are used — the commands only shell out to the consuming
|
|
16
|
-
* package's own `vite` (or `vp`) binary.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* The Vite invocations the commands can drive. `script` / `modules` are the
|
|
21
|
-
* two build passes; `dev` is the single serve-mode server `wp-vite dev` runs
|
|
22
|
-
* (there is no second pass in dev — one server handles both, see
|
|
23
|
-
* `wpDevServer`).
|
|
24
|
-
*/
|
|
25
|
-
type PassName = 'script' | 'modules' | 'dev';
|
|
26
|
-
/** Which command is driving the passes. Exposed to children as `WP_VITE_RUNNER`. */
|
|
27
|
-
type RunnerName = 'build' | 'watch' | 'dev';
|
|
28
|
-
/**
|
|
29
|
-
* Signals the commands forward. Spelled out rather than pulled from the
|
|
30
|
-
* ambient Node signal type, which this repo's ESLint config reports as an
|
|
31
|
-
* undefined global.
|
|
32
|
-
*/
|
|
33
|
-
type RunnerSignal = 'SIGINT' | 'SIGTERM';
|
|
34
|
-
/** Sink for prefixed child output. `process.stdout` satisfies it. */
|
|
35
|
-
interface OutputSink {
|
|
36
|
-
write(text: string): unknown;
|
|
37
|
-
}
|
|
38
|
-
/** Options shared by `build()` and `watch()`. */
|
|
39
|
-
interface RunnerOptions {
|
|
40
|
-
/** Package directory the passes run in. Default: `process.cwd()`. */
|
|
41
|
-
cwd?: string;
|
|
42
|
-
/**
|
|
43
|
-
* Directory cleaned once at startup. Must match `build.outDir` of the
|
|
44
|
-
* Vite config. Default: `'dist'`.
|
|
45
|
-
*/
|
|
46
|
-
outDir?: string;
|
|
47
|
-
/**
|
|
48
|
-
* Force the module pass on/off. When omitted, `detectModulePass` decides.
|
|
49
|
-
*/
|
|
50
|
-
modules?: boolean;
|
|
51
|
-
/** `--mode` value for the module pass. Default: `'modules'`. */
|
|
52
|
-
modulesMode?: string;
|
|
53
|
-
/**
|
|
54
|
-
* Binary to run. A bare name (`'vite'`, `'vp'`) is looked up in the
|
|
55
|
-
* nearest `node_modules/.bin`; a path is used as-is. When omitted, `vite`
|
|
56
|
-
* is preferred over `vp` (see `resolveViteBin`).
|
|
57
|
-
*/
|
|
58
|
-
bin?: string;
|
|
59
|
-
/** Extra arguments appended to both passes. */
|
|
60
|
-
args?: string[];
|
|
61
|
-
/** Skip the startup clean. Default: `false` (it cleans). */
|
|
62
|
-
clean?: boolean;
|
|
63
|
-
/** Install SIGINT/SIGTERM handlers on `process`. Default: `true`. */
|
|
64
|
-
handleSignals?: boolean;
|
|
65
|
-
/** Extra environment for the children. */
|
|
66
|
-
env?: Record<string, string | undefined>;
|
|
67
|
-
/** Output sinks. Default: `process.stdout` / `process.stderr`. */
|
|
68
|
-
stdout?: OutputSink;
|
|
69
|
-
stderr?: OutputSink;
|
|
70
|
-
/**
|
|
71
|
-
* Replaces the built-in argv (`DEFAULT_BUILD_ARGS` / `DEFAULT_WATCH_ARGS`)
|
|
72
|
-
* for every pass. Escape hatch for tests — it lets a stand-in child stand
|
|
73
|
-
* in for a real Vite invocation.
|
|
74
|
-
*/
|
|
75
|
-
buildArgs?: string[];
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Arguments every `wp-vite build` pass gets, before the pass-specific
|
|
79
|
-
* `--mode`.
|
|
80
|
-
*
|
|
81
|
-
* `--no-emptyOutDir` makes the runner's own one-time clean the single source
|
|
82
|
-
* of truth, so no pass can delete another's output no matter how the config
|
|
83
|
-
* sets `emptyOutDir`. `--no-clearScreen` keeps the second pass from erasing
|
|
84
|
-
* the first pass' summary.
|
|
85
|
-
*/
|
|
86
|
-
declare const DEFAULT_BUILD_ARGS: string[];
|
|
87
|
-
/**
|
|
88
|
-
* Arguments every `wp-vite watch` pass gets, before the pass-specific
|
|
89
|
-
* `--mode`.
|
|
90
|
-
*
|
|
91
|
-
* `--no-emptyOutDir` is what stops the script pass from wiping the module
|
|
92
|
-
* pass' chunks out of dist/ on every rebuild; it overrides `emptyOutDir: true`
|
|
93
|
-
* from the config file (verified on Vite 8). `--no-clearScreen` stops two
|
|
94
|
-
* watchers sharing one terminal from clearing each other's output.
|
|
95
|
-
*/
|
|
96
|
-
declare const DEFAULT_WATCH_ARGS: string[];
|
|
97
|
-
/** Binary names tried, in order, when `bin` is not given. */
|
|
98
|
-
declare const BIN_CANDIDATES: readonly ["vite", "vp"];
|
|
99
|
-
interface ModulePassDetection {
|
|
100
|
-
modules: boolean;
|
|
101
|
-
/** Why — surfaced in the startup log so the heuristic is never a mystery. */
|
|
102
|
-
reason: string;
|
|
103
|
-
}
|
|
104
|
-
interface DetectModulePassOptions {
|
|
105
|
-
cwd?: string;
|
|
106
|
-
modulesMode?: string;
|
|
107
|
-
/** Explicit override; wins over every heuristic. */
|
|
108
|
-
modules?: boolean;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Decide whether this package has a module pass.
|
|
112
|
-
*
|
|
113
|
-
* The commands deliberately do not import the user's Vite config (top-level
|
|
114
|
-
* side effects) and cannot discover Script Module entries themselves — that
|
|
115
|
-
* needs `blocksDir` / `adminPagesDir`, which only the config knows. So they
|
|
116
|
-
* use a cheap, predictable, read-only heuristic:
|
|
117
|
-
*
|
|
118
|
-
* 1. an explicit `modules` option (`--modules` / `--no-modules`) wins;
|
|
119
|
-
* 2. a `package.json` script whose command contains `--mode <modulesMode>`;
|
|
120
|
-
* 3. a Vite config file whose source text contains the mode as a string
|
|
121
|
-
* literal (`mode === 'modules'`, `'modules'` in a map, …).
|
|
122
|
-
*
|
|
123
|
-
* Everything else means "script pass only".
|
|
124
|
-
*/
|
|
125
|
-
declare function detectModulePass(options?: DetectModulePassOptions): ModulePassDetection;
|
|
126
|
-
interface ResolvedBin {
|
|
127
|
-
/** Binary name, or the raw value when an explicit path was given. */
|
|
128
|
-
name: string;
|
|
129
|
-
/** What gets handed to `spawn`. */
|
|
130
|
-
path: string;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Find the Vite CLI the consuming package would use.
|
|
134
|
-
*
|
|
135
|
-
* Walks `node_modules/.bin` from `cwd` upwards, so workspace packages find a
|
|
136
|
-
* hoisted binary at the monorepo root. `vite` is preferred over `vp`: plain
|
|
137
|
-
* Vite is present in every setup (Vite+ installs it too) and `vp build`'s
|
|
138
|
-
* extra orchestration buys these commands nothing. Packages that need `vp` —
|
|
139
|
-
* for its caching or remote-cache layer — pass `--bin vp`.
|
|
140
|
-
*/
|
|
141
|
-
declare function resolveViteBin(cwd?: string, candidates?: readonly string[]): ResolvedBin;
|
|
142
|
-
/**
|
|
143
|
-
* Remove `outDir` — once, before any pass runs. Returns the absolute path that
|
|
144
|
-
* was cleaned.
|
|
145
|
-
*/
|
|
146
|
-
declare function cleanOutDir(outDir?: string, cwd?: string): Promise<string>;
|
|
147
|
-
interface LinePrefixer {
|
|
148
|
-
/** Feed a (possibly partial) chunk of child output. */
|
|
149
|
-
write(chunk: string): void;
|
|
150
|
-
/** Emit a trailing line that never got its newline. */
|
|
151
|
-
flush(): void;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Prefix child output line by line. Chunks arrive on arbitrary boundaries, so
|
|
155
|
-
* a partial trailing line is buffered until its newline shows up.
|
|
156
|
-
*
|
|
157
|
-
* Two cosmetic rules keep interleaved passes readable: blank lines pass
|
|
158
|
-
* through unprefixed (Vite prints plenty, and `[script] ` alone is noise), and
|
|
159
|
-
* a line redrawn with carriage returns is reduced to what a terminal would
|
|
160
|
-
* actually show — the text after the last CR, minus the erase codes.
|
|
161
|
-
*/
|
|
162
|
-
declare function createLinePrefixer(prefix: string, write: (text: string) => void): LinePrefixer;
|
|
163
|
-
interface ParsedCliArgs {
|
|
164
|
-
command: string | undefined;
|
|
165
|
-
options: RunnerOptions;
|
|
166
|
-
help: boolean;
|
|
167
|
-
version: boolean;
|
|
168
|
-
/** Flags that are not recognised — the CLI reports these and exits 1. */
|
|
169
|
-
unknown: string[];
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Hand-rolled argv parser (no dependency). Supports:
|
|
173
|
-
*
|
|
174
|
-
* wp-vite <build|watch> [--out-dir dist] [--modules|--no-modules]
|
|
175
|
-
* [--modules-mode modules] [--bin vite|vp|<path>]
|
|
176
|
-
* [--no-clean] [--cwd <dir>] [-- <extra vite args>]
|
|
177
|
-
*
|
|
178
|
-
* `--flag=value` and `--flag value` are both accepted. Everything after a
|
|
179
|
-
* bare `--` is forwarded verbatim to both Vite passes.
|
|
180
|
-
*/
|
|
181
|
-
declare function parseCliArgs(argv: readonly string[]): ParsedCliArgs;
|
|
182
|
-
|
|
183
|
-
export { BIN_CANDIDATES as B, DEFAULT_BUILD_ARGS as D, type LinePrefixer as L, type ModulePassDetection as M, type OutputSink as O, type ParsedCliArgs as P, type RunnerOptions as R, DEFAULT_WATCH_ARGS as a, type DetectModulePassOptions as b, type PassName as c, type ResolvedBin as d, type RunnerName as e, type RunnerSignal as f, cleanOutDir as g, createLinePrefixer as h, detectModulePass as i, parseCliArgs as p, resolveViteBin as r };
|
package/dist/watch.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { R as RunnerOptions } from './runner-jUVlAHlZ.js';
|
|
2
|
-
export { B as BIN_CANDIDATES, D as DEFAULT_BUILD_ARGS, a as DEFAULT_WATCH_ARGS, b as DetectModulePassOptions, L as LinePrefixer, M as ModulePassDetection, O as OutputSink, P as ParsedCliArgs, c as PassName, d as ResolvedBin, e as RunnerName, f as RunnerSignal, g as cleanOutDir, h as createLinePrefixer, i as detectModulePass, p as parseCliArgs, r as resolveViteBin } from './runner-jUVlAHlZ.js';
|
|
3
|
-
|
|
4
|
-
/** Options for `watch()`. Identical to `build()`'s — see `RunnerOptions`. */
|
|
5
|
-
type WatchOptions = RunnerOptions;
|
|
6
|
-
/**
|
|
7
|
-
* Run the unified watcher. Resolves with the process exit code: `0` for a
|
|
8
|
-
* clean shutdown (SIGINT/SIGTERM), non-zero when a pass died unexpectedly.
|
|
9
|
-
*/
|
|
10
|
-
declare function watch(options?: WatchOptions): Promise<number>;
|
|
11
|
-
|
|
12
|
-
export { RunnerOptions, type WatchOptions, watch };
|
package/dist/watch.js
DELETED
|
@@ -1,398 +0,0 @@
|
|
|
1
|
-
import { spawn } from 'child_process';
|
|
2
|
-
import { existsSync, readFileSync } from 'fs';
|
|
3
|
-
import { rm } from 'fs/promises';
|
|
4
|
-
import { resolve, join, parse, dirname, isAbsolute, sep } from 'path';
|
|
5
|
-
|
|
6
|
-
// src/runner.ts
|
|
7
|
-
var DEFAULT_BUILD_ARGS = ["build", "--no-emptyOutDir", "--no-clearScreen"];
|
|
8
|
-
var DEFAULT_WATCH_ARGS = ["build", "--watch", "--no-emptyOutDir", "--no-clearScreen"];
|
|
9
|
-
var BIN_CANDIDATES = ["vite", "vp"];
|
|
10
|
-
function spawnPass(options) {
|
|
11
|
-
const { pass, stdout, stderr } = options;
|
|
12
|
-
const child = spawn(options.bin, options.argv, {
|
|
13
|
-
cwd: options.cwd,
|
|
14
|
-
env: {
|
|
15
|
-
...process.env,
|
|
16
|
-
...options.env,
|
|
17
|
-
WP_VITE_RUNNER: options.runner,
|
|
18
|
-
WP_VITE_PASS: pass,
|
|
19
|
-
// Kept for the documented watch-only guard:
|
|
20
|
-
// `emptyOutDir: !isModulePass && !process.env.WP_VITE_UNIFIED_WATCH`.
|
|
21
|
-
...options.runner === "watch" ? { WP_VITE_UNIFIED_WATCH: "1" } : {}
|
|
22
|
-
},
|
|
23
|
-
stdio: options.inherit ? "inherit" : ["ignore", "pipe", "pipe"],
|
|
24
|
-
// .cmd shims are not directly executable on Windows.
|
|
25
|
-
shell: process.platform === "win32"
|
|
26
|
-
});
|
|
27
|
-
if (options.inherit) {
|
|
28
|
-
const result2 = new Promise((settle) => {
|
|
29
|
-
let settled = false;
|
|
30
|
-
const finish = (passResult) => {
|
|
31
|
-
if (settled) return;
|
|
32
|
-
settled = true;
|
|
33
|
-
settle(passResult);
|
|
34
|
-
};
|
|
35
|
-
child.on("error", (error) => {
|
|
36
|
-
stderr.write(`[wp-vite] the ${pass} pass failed to start: ${error.message}
|
|
37
|
-
`);
|
|
38
|
-
finish({ pass, code: null, signal: null, error });
|
|
39
|
-
});
|
|
40
|
-
child.on("close", (code, signal) => finish({ pass, code, signal }));
|
|
41
|
-
});
|
|
42
|
-
return { pass, child, result: result2 };
|
|
43
|
-
}
|
|
44
|
-
const outPrefixer = createLinePrefixer(`[${pass}] `, (text) => stdout.write(text));
|
|
45
|
-
const errPrefixer = createLinePrefixer(`[${pass}] `, (text) => stderr.write(text));
|
|
46
|
-
child.stdout?.setEncoding("utf8");
|
|
47
|
-
child.stdout?.on("data", (chunk) => outPrefixer.write(chunk));
|
|
48
|
-
child.stderr?.setEncoding("utf8");
|
|
49
|
-
child.stderr?.on("data", (chunk) => errPrefixer.write(chunk));
|
|
50
|
-
const result = new Promise((settle) => {
|
|
51
|
-
let settled = false;
|
|
52
|
-
const finish = (passResult) => {
|
|
53
|
-
if (settled) return;
|
|
54
|
-
settled = true;
|
|
55
|
-
outPrefixer.flush();
|
|
56
|
-
errPrefixer.flush();
|
|
57
|
-
settle(passResult);
|
|
58
|
-
};
|
|
59
|
-
child.on("error", (error) => {
|
|
60
|
-
stderr.write(`[wp-vite] the ${pass} pass failed to start: ${error.message}
|
|
61
|
-
`);
|
|
62
|
-
finish({ pass, code: null, signal: null, error });
|
|
63
|
-
});
|
|
64
|
-
child.on("close", (code, signal) => finish({ pass, code, signal }));
|
|
65
|
-
});
|
|
66
|
-
return { pass, child, result };
|
|
67
|
-
}
|
|
68
|
-
function passArgs(base, pass, modulesMode, extra = []) {
|
|
69
|
-
return [...base, ...pass === "modules" ? ["--mode", modulesMode] : [], ...extra];
|
|
70
|
-
}
|
|
71
|
-
function forwardSignals(handler) {
|
|
72
|
-
const bound = ["SIGINT", "SIGTERM"].map(
|
|
73
|
-
(signal) => [signal, () => handler(signal)]
|
|
74
|
-
);
|
|
75
|
-
for (const [signal, listener] of bound) process.on(signal, listener);
|
|
76
|
-
return () => {
|
|
77
|
-
for (const [signal, listener] of bound) process.off(signal, listener);
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
function describeExit(code, signal) {
|
|
81
|
-
if (signal) return `signal ${signal}`;
|
|
82
|
-
if (code === null) return "unknown cause";
|
|
83
|
-
return `exit code ${code}`;
|
|
84
|
-
}
|
|
85
|
-
var VITE_CONFIG_FILES = [
|
|
86
|
-
"vite.config.ts",
|
|
87
|
-
"vite.config.mts",
|
|
88
|
-
"vite.config.cts",
|
|
89
|
-
"vite.config.js",
|
|
90
|
-
"vite.config.mjs",
|
|
91
|
-
"vite.config.cjs"
|
|
92
|
-
];
|
|
93
|
-
function detectModulePass(options = {}) {
|
|
94
|
-
if (typeof options.modules === "boolean") {
|
|
95
|
-
return {
|
|
96
|
-
modules: options.modules,
|
|
97
|
-
reason: `explicit --${options.modules ? "modules" : "no-modules"} flag`
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
const cwd = resolve(options.cwd ?? process.cwd());
|
|
101
|
-
const modulesMode = options.modulesMode ?? "modules";
|
|
102
|
-
const escapedMode = escapeRegExp(modulesMode);
|
|
103
|
-
const scriptMatch = new RegExp(`(?:--mode[=\\s]+|(?:^|\\s)-m\\s+)${escapedMode}(?![\\w-])`);
|
|
104
|
-
const packageJson = readJson(join(cwd, "package.json"));
|
|
105
|
-
const scripts = packageJson?.scripts;
|
|
106
|
-
if (scripts && typeof scripts === "object") {
|
|
107
|
-
for (const [name, command] of Object.entries(scripts)) {
|
|
108
|
-
if (typeof command === "string" && scriptMatch.test(command)) {
|
|
109
|
-
return {
|
|
110
|
-
modules: true,
|
|
111
|
-
reason: `package.json script "${name}" runs --mode ${modulesMode}`
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const configMatch = new RegExp(`(['"\`])${escapedMode}\\1`);
|
|
117
|
-
for (const fileName of VITE_CONFIG_FILES) {
|
|
118
|
-
const filePath = join(cwd, fileName);
|
|
119
|
-
const source = readFileIfExists(filePath);
|
|
120
|
-
if (source === null) continue;
|
|
121
|
-
if (configMatch.test(source)) {
|
|
122
|
-
return { modules: true, reason: `${fileName} mentions the "${modulesMode}" mode` };
|
|
123
|
-
}
|
|
124
|
-
return {
|
|
125
|
-
modules: false,
|
|
126
|
-
reason: `${fileName} never mentions the "${modulesMode}" mode`
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
return {
|
|
130
|
-
modules: false,
|
|
131
|
-
reason: `no package.json script or Vite config mentions the "${modulesMode}" mode`
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
function resolveViteBin(cwd = process.cwd(), candidates = BIN_CANDIDATES) {
|
|
135
|
-
const searched = [];
|
|
136
|
-
for (const binDir of nodeModulesBinDirs(cwd)) {
|
|
137
|
-
for (const name of candidates) {
|
|
138
|
-
for (const fileName of binFileNames(name)) {
|
|
139
|
-
const candidatePath = join(binDir, fileName);
|
|
140
|
-
searched.push(candidatePath);
|
|
141
|
-
if (existsSync(candidatePath)) return { name, path: candidatePath };
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
throw new Error(
|
|
146
|
-
`[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:
|
|
147
|
-
${searched.join("\n ")}`
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
function resolveBin(bin, cwd) {
|
|
151
|
-
if (!bin) return resolveViteBin(cwd).path;
|
|
152
|
-
if (isAbsolute(bin) || bin.includes("/") || bin.includes("\\")) return bin;
|
|
153
|
-
try {
|
|
154
|
-
return resolveViteBin(cwd, [bin]).path;
|
|
155
|
-
} catch {
|
|
156
|
-
return bin;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
function nodeModulesBinDirs(from) {
|
|
160
|
-
const dirs = [];
|
|
161
|
-
let current = resolve(from);
|
|
162
|
-
const { root } = parse(current);
|
|
163
|
-
while (true) {
|
|
164
|
-
dirs.push(join(current, "node_modules", ".bin"));
|
|
165
|
-
if (current === root) break;
|
|
166
|
-
const parent = dirname(current);
|
|
167
|
-
if (parent === current) break;
|
|
168
|
-
current = parent;
|
|
169
|
-
}
|
|
170
|
-
return dirs;
|
|
171
|
-
}
|
|
172
|
-
function binFileNames(name) {
|
|
173
|
-
return process.platform === "win32" ? [`${name}.cmd`, name] : [name];
|
|
174
|
-
}
|
|
175
|
-
async function cleanOutDir(outDir = "dist", cwd = process.cwd()) {
|
|
176
|
-
const base = resolve(cwd);
|
|
177
|
-
const target = isAbsolute(outDir) ? resolve(outDir) : resolve(base, outDir);
|
|
178
|
-
if (target === parse(target).root || target === base || !target.startsWith(base + sep)) {
|
|
179
|
-
throw new Error(
|
|
180
|
-
`[wp-vite] refusing to clean ${target}: the out dir must be inside ${base}`
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
await rm(target, { recursive: true, force: true });
|
|
184
|
-
return target;
|
|
185
|
-
}
|
|
186
|
-
var ESCAPE = String.fromCharCode(27);
|
|
187
|
-
var ERASE_SEQUENCE = new RegExp(`^(?:${ESCAPE}\\[[0-2]?[KJ])+`);
|
|
188
|
-
function createLinePrefixer(prefix, write) {
|
|
189
|
-
let buffer = "";
|
|
190
|
-
const emit = (line) => {
|
|
191
|
-
const withoutCrlf = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
192
|
-
const rendered = withoutCrlf.split("\r").pop() ?? "";
|
|
193
|
-
const content = rendered.replace(ERASE_SEQUENCE, "");
|
|
194
|
-
write(content.length === 0 ? "\n" : `${prefix}${content}
|
|
195
|
-
`);
|
|
196
|
-
};
|
|
197
|
-
return {
|
|
198
|
-
write(chunk) {
|
|
199
|
-
buffer += chunk;
|
|
200
|
-
const lines = buffer.split("\n");
|
|
201
|
-
buffer = lines.pop() ?? "";
|
|
202
|
-
for (const line of lines) emit(line);
|
|
203
|
-
},
|
|
204
|
-
flush() {
|
|
205
|
-
if (buffer.length === 0) return;
|
|
206
|
-
const pending = buffer;
|
|
207
|
-
buffer = "";
|
|
208
|
-
emit(pending);
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
function parseCliArgs(argv) {
|
|
213
|
-
const parsed = {
|
|
214
|
-
command: void 0,
|
|
215
|
-
options: {},
|
|
216
|
-
help: false,
|
|
217
|
-
version: false,
|
|
218
|
-
unknown: []
|
|
219
|
-
};
|
|
220
|
-
const passthrough = [];
|
|
221
|
-
let index = 0;
|
|
222
|
-
for (; index < argv.length; index += 1) {
|
|
223
|
-
const arg = argv[index];
|
|
224
|
-
if (arg === "--") {
|
|
225
|
-
passthrough.push(...argv.slice(index + 1));
|
|
226
|
-
break;
|
|
227
|
-
}
|
|
228
|
-
if (!arg.startsWith("-")) {
|
|
229
|
-
if (parsed.command === void 0) parsed.command = arg;
|
|
230
|
-
else parsed.unknown.push(arg);
|
|
231
|
-
continue;
|
|
232
|
-
}
|
|
233
|
-
const equalsAt = arg.indexOf("=");
|
|
234
|
-
const flag = equalsAt === -1 ? arg : arg.slice(0, equalsAt);
|
|
235
|
-
const inlineValue = equalsAt === -1 ? void 0 : arg.slice(equalsAt + 1);
|
|
236
|
-
const takeValue = () => {
|
|
237
|
-
if (inlineValue !== void 0) return inlineValue;
|
|
238
|
-
const next = argv[index + 1];
|
|
239
|
-
if (next === void 0 || next.startsWith("-")) return void 0;
|
|
240
|
-
index += 1;
|
|
241
|
-
return next;
|
|
242
|
-
};
|
|
243
|
-
switch (flag) {
|
|
244
|
-
case "-h":
|
|
245
|
-
case "--help":
|
|
246
|
-
parsed.help = true;
|
|
247
|
-
break;
|
|
248
|
-
case "-v":
|
|
249
|
-
case "--version":
|
|
250
|
-
parsed.version = true;
|
|
251
|
-
break;
|
|
252
|
-
case "--out-dir":
|
|
253
|
-
case "--outDir": {
|
|
254
|
-
const value = takeValue();
|
|
255
|
-
if (value === void 0) parsed.unknown.push(`${flag} (missing value)`);
|
|
256
|
-
else parsed.options.outDir = value;
|
|
257
|
-
break;
|
|
258
|
-
}
|
|
259
|
-
case "--modules-mode":
|
|
260
|
-
case "--modulesMode": {
|
|
261
|
-
const value = takeValue();
|
|
262
|
-
if (value === void 0) parsed.unknown.push(`${flag} (missing value)`);
|
|
263
|
-
else parsed.options.modulesMode = value;
|
|
264
|
-
break;
|
|
265
|
-
}
|
|
266
|
-
case "--bin": {
|
|
267
|
-
const value = takeValue();
|
|
268
|
-
if (value === void 0) parsed.unknown.push(`${flag} (missing value)`);
|
|
269
|
-
else parsed.options.bin = value;
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
272
|
-
case "--cwd": {
|
|
273
|
-
const value = takeValue();
|
|
274
|
-
if (value === void 0) parsed.unknown.push(`${flag} (missing value)`);
|
|
275
|
-
else parsed.options.cwd = value;
|
|
276
|
-
break;
|
|
277
|
-
}
|
|
278
|
-
case "--modules":
|
|
279
|
-
parsed.options.modules = true;
|
|
280
|
-
break;
|
|
281
|
-
case "--no-modules":
|
|
282
|
-
parsed.options.modules = false;
|
|
283
|
-
break;
|
|
284
|
-
case "--clean":
|
|
285
|
-
parsed.options.clean = true;
|
|
286
|
-
break;
|
|
287
|
-
case "--no-clean":
|
|
288
|
-
parsed.options.clean = false;
|
|
289
|
-
break;
|
|
290
|
-
default:
|
|
291
|
-
parsed.unknown.push(flag);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
if (passthrough.length > 0) parsed.options.args = passthrough;
|
|
295
|
-
return parsed;
|
|
296
|
-
}
|
|
297
|
-
async function prepareRun(options, runner) {
|
|
298
|
-
const cwd = resolve(options.cwd ?? process.cwd());
|
|
299
|
-
const outDir = options.outDir ?? "dist";
|
|
300
|
-
const modulesMode = options.modulesMode ?? "modules";
|
|
301
|
-
const stdout = options.stdout ?? process.stdout;
|
|
302
|
-
const stderr = options.stderr ?? process.stderr;
|
|
303
|
-
const bin = resolveBin(options.bin, cwd);
|
|
304
|
-
const detection = detectModulePass({ cwd, modulesMode, modules: options.modules });
|
|
305
|
-
const passes = detection.modules ? ["script", "modules"] : ["script"];
|
|
306
|
-
if (options.clean !== false) {
|
|
307
|
-
const cleaned = await cleanOutDir(outDir, cwd);
|
|
308
|
-
stdout.write(`[wp-vite] cleaned ${cleaned}
|
|
309
|
-
`);
|
|
310
|
-
}
|
|
311
|
-
stdout.write(
|
|
312
|
-
`[wp-vite] module pass ${detection.modules ? "enabled" : "disabled"} \u2014 ${detection.reason}; ${"watching" } ${passes.join(" + " )} via ${bin}
|
|
313
|
-
`
|
|
314
|
-
);
|
|
315
|
-
return { cwd, bin, modulesMode, passes, stdout, stderr };
|
|
316
|
-
}
|
|
317
|
-
function escapeRegExp(value) {
|
|
318
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
319
|
-
}
|
|
320
|
-
function readFileIfExists(filePath) {
|
|
321
|
-
try {
|
|
322
|
-
return readFileSync(filePath, "utf8");
|
|
323
|
-
} catch {
|
|
324
|
-
return null;
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
function readJson(filePath) {
|
|
328
|
-
const source = readFileIfExists(filePath);
|
|
329
|
-
if (source === null) return null;
|
|
330
|
-
try {
|
|
331
|
-
return JSON.parse(source);
|
|
332
|
-
} catch {
|
|
333
|
-
return null;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// src/watch.ts
|
|
338
|
-
async function watch(options = {}) {
|
|
339
|
-
const { cwd, bin, modulesMode, passes, stdout, stderr } = await prepareRun(options);
|
|
340
|
-
const children = [];
|
|
341
|
-
let shuttingDown = false;
|
|
342
|
-
let exitCode = 0;
|
|
343
|
-
const shutdown = (signal) => {
|
|
344
|
-
shuttingDown = true;
|
|
345
|
-
for (const child of children) {
|
|
346
|
-
if (child.exitCode === null && child.signalCode === null) child.kill(signal);
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
const disposeSignals = options.handleSignals === false ? () => {
|
|
350
|
-
} : forwardSignals((signal) => {
|
|
351
|
-
if (shuttingDown) return;
|
|
352
|
-
exitCode = 0;
|
|
353
|
-
shutdown(signal);
|
|
354
|
-
});
|
|
355
|
-
const spawned = passes.map(
|
|
356
|
-
(pass) => spawnPass({
|
|
357
|
-
pass,
|
|
358
|
-
runner: "watch",
|
|
359
|
-
bin,
|
|
360
|
-
cwd,
|
|
361
|
-
argv: passArgs(
|
|
362
|
-
options.buildArgs ?? DEFAULT_WATCH_ARGS,
|
|
363
|
-
pass,
|
|
364
|
-
modulesMode,
|
|
365
|
-
options.args ?? []
|
|
366
|
-
),
|
|
367
|
-
env: options.env,
|
|
368
|
-
stdout,
|
|
369
|
-
stderr
|
|
370
|
-
})
|
|
371
|
-
);
|
|
372
|
-
children.push(...spawned.map(({ child }) => child));
|
|
373
|
-
let running = spawned.length;
|
|
374
|
-
try {
|
|
375
|
-
await Promise.all(
|
|
376
|
-
spawned.map(async ({ pass, result }) => {
|
|
377
|
-
const passResult = await result;
|
|
378
|
-
running -= 1;
|
|
379
|
-
if (shuttingDown) return;
|
|
380
|
-
const unexpected = passResult.error != null || passResult.code === null || passResult.code !== 0 || running > 0;
|
|
381
|
-
if (!unexpected) return;
|
|
382
|
-
exitCode = passResult.code && passResult.code !== 0 ? passResult.code : 1;
|
|
383
|
-
stderr.write(
|
|
384
|
-
`[wp-vite] the ${pass} pass exited unexpectedly (${describeExit(passResult.code, passResult.signal)}) \u2014 stopping
|
|
385
|
-
`
|
|
386
|
-
);
|
|
387
|
-
shutdown("SIGTERM");
|
|
388
|
-
})
|
|
389
|
-
);
|
|
390
|
-
return exitCode;
|
|
391
|
-
} finally {
|
|
392
|
-
disposeSignals();
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
export { BIN_CANDIDATES, DEFAULT_BUILD_ARGS, DEFAULT_WATCH_ARGS, cleanOutDir, createLinePrefixer, detectModulePass, parseCliArgs, resolveViteBin, watch };
|
|
397
|
-
//# sourceMappingURL=watch.js.map
|
|
398
|
-
//# sourceMappingURL=watch.js.map
|