@funcd-dev/vite-plugin 0.14.0 → 0.16.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.
Files changed (3) hide show
  1. package/README.md +23 -0
  2. package/dist/index.js +45 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -35,3 +35,26 @@ the file.
35
35
 
36
36
  Each function builds into a staging directory under Vite's cache directory, and the plugin copies the
37
37
  result into `outDir`, so a shared output directory is never emptied.
38
+
39
+ ## Watch mode
40
+
41
+ `vite build --watch` keeps `outDir` current, one function at a time (funcd ADR-0244):
42
+
43
+ - When a function's rebuild ends, its `<name>.mjs` and manifest are written to `outDir`; the other
44
+ functions' files are not touched. Each file is written beside its target and renamed over it, so a
45
+ reader never sees half a file.
46
+ - Saving a function's manifest rebuilds it, and saving the shared `funcdctl.yaml` rebuilds every
47
+ function that uses it. A manifest created while the watch runs takes effect at that function's next
48
+ rebuild.
49
+ - A failed rebuild prints the error and keeps the function's last good files; the watch goes on.
50
+
51
+ Beside it, `funcdctl dev` reloads each function whose files change, so a saved handler answers with
52
+ its new code within about a second:
53
+
54
+ ```bash
55
+ vite build --watch # terminal 1
56
+ funcdctl dev dist # terminal 2
57
+ ```
58
+
59
+ A plain `vite build` still writes `outDir` only after every function has built, and writes nothing
60
+ when one fails.
package/dist/index.js CHANGED
@@ -2,8 +2,10 @@
2
2
  // nodejs22 shim loads, and puts the function's funcdctl.yaml where `funcdctl push` resolves it. Each function is its
3
3
  // own Vite environment, because one build with several inputs splits shared code into chunks. Each environment builds
4
4
  // into its own staging directory, and the plugin copies the result into outDir: Vite would otherwise empty a shared
5
- // outDir once per function, and warns when outDir is the project root.
6
- import { copyFileSync, existsSync, mkdirSync } from 'node:fs';
5
+ // outDir once per function, and warns when outDir is the project root. Under `vite build --watch` (funcd ADR-0244)
6
+ // Vite starts every environment's watcher before any build ends, so each function is delivered when its own
7
+ // environment's write ends instead, and its manifest is watched too.
8
+ import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync } from 'node:fs';
7
9
  import { builtinModules } from 'node:module';
8
10
  import { basename, dirname, join, resolve } from 'node:path';
9
11
  const prefix = 'funcd_';
@@ -21,6 +23,7 @@ export function funcd(options = {}) {
21
23
  let functions = {};
22
24
  let root = '';
23
25
  let staging = '';
26
+ const byEnvironment = new Map();
24
27
  const plugin = {
25
28
  name: 'funcd',
26
29
  config(userConfig) {
@@ -31,7 +34,7 @@ export function funcd(options = {}) {
31
34
  if (names.length === 0) {
32
35
  throw new Error('funcd: no function to build (options.functions is empty)');
33
36
  }
34
- const byEnvironment = new Map();
37
+ byEnvironment.clear();
35
38
  const environments = {};
36
39
  for (const name of names) {
37
40
  const env = environmentName(name);
@@ -73,19 +76,51 @@ export function funcd(options = {}) {
73
76
  await builder.build(env);
74
77
  }
75
78
  }
76
- const outDir = resolve(root, outDirOption);
77
- mkdirSync(outDir, { recursive: true });
78
- for (const name of names) {
79
- copyFileSync(join(staging, environmentName(name), `${name}.mjs`), join(outDir, `${name}.mjs`));
79
+ if (builder.config.build.watch) {
80
+ return; // build() returned the watchers before any build ended; writeBundle delivers each function
80
81
  }
81
- copyManifests(root, outDir, names);
82
+ deliver(root, staging, resolve(root, outDirOption), names, copyFileSync);
82
83
  },
83
84
  },
84
85
  };
85
86
  },
87
+ buildStart() {
88
+ const name = byEnvironment.get(this.environment.name);
89
+ if (name !== undefined) {
90
+ this.addWatchFile(manifestFor(root, name));
91
+ }
92
+ },
93
+ writeBundle() {
94
+ const name = byEnvironment.get(this.environment.name);
95
+ if (name !== undefined && this.environment.config.build.watch) {
96
+ deliver(root, staging, resolve(root, outDirOption), [name], replaceFile);
97
+ }
98
+ },
86
99
  };
87
100
  return [plugin];
88
101
  }
102
+ // deliver copies each function's staged <name>.mjs and its manifest into outDir.
103
+ function deliver(root, staging, outDir, names, copy) {
104
+ mkdirSync(outDir, { recursive: true });
105
+ for (const name of names) {
106
+ copy(join(staging, environmentName(name), `${name}.mjs`), join(outDir, `${name}.mjs`));
107
+ }
108
+ copyManifests(root, outDir, names, copy);
109
+ }
110
+ // replaceFile copies from over to by a rename, so a reader that polls outDir (funcdctl dev) never loads half a file.
111
+ // The temporary file is a dot-file beside the target: the rename stays on one filesystem, and funcdctl dev's
112
+ // directory scan skips it.
113
+ function replaceFile(from, to) {
114
+ const tmp = join(dirname(to), `.${basename(to)}.${process.pid}.tmp`);
115
+ try {
116
+ copyFileSync(from, tmp);
117
+ renameSync(tmp, to);
118
+ }
119
+ catch (err) {
120
+ rmSync(tmp, { force: true });
121
+ throw err;
122
+ }
123
+ }
89
124
  // manifestFor returns the function's manifest: <root>/<name>.funcdctl.yaml, else <root>/funcdctl.yaml (ADR-0124).
90
125
  function manifestFor(root, name) {
91
126
  const stem = join(root, name + manifestSuffix);
@@ -100,12 +135,12 @@ function manifestFor(root, name) {
100
135
  }
101
136
  // copyManifests puts each manifest beside its output as <name>.funcdctl.yaml, unless the output directory is the one
102
137
  // that holds it: there funcdctl's resolver already finds it for <name>.mjs.
103
- function copyManifests(root, outDir, names) {
138
+ function copyManifests(root, outDir, names, copy) {
104
139
  for (const name of names) {
105
140
  const manifest = manifestFor(root, name);
106
141
  if (dirname(manifest) === outDir) {
107
142
  continue;
108
143
  }
109
- copyFileSync(manifest, join(outDir, name + manifestSuffix));
144
+ copy(manifest, join(outDir, name + manifestSuffix));
110
145
  }
111
146
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funcd-dev/vite-plugin",
3
- "version": "0.14.0",
3
+ "version": "0.16.0",
4
4
  "type": "module",
5
5
  "description": "Vite plugin that bundles each funcd function into one self-contained ES module",
6
6
  "license": "Apache-2.0",