@funcd-dev/vite-plugin 0.18.0 → 0.19.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.
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # @funcd-dev/vite-plugin
2
2
 
3
3
  A [Vite](https://vite.dev) plugin that bundles each [funcd](https://github.com/pyvvo/funcd) function
4
- into one self-contained ES module for the funcd Node runtime (funcd ADR-0144).
4
+ into one self-contained ES module for the funcd Node runtime (funcd ADR-0144), and puts its contract file
5
+ beside it (funcd ADR-0247).
5
6
 
6
7
  ```bash
7
8
  yarn add -D vite @funcd-dev/vite-plugin
@@ -13,7 +14,7 @@ import { funcd } from '@funcd-dev/vite-plugin';
13
14
  import { defineConfig } from 'vite';
14
15
 
15
16
  export default defineConfig({
16
- plugins: [funcd({ functions: { front: 'src/front.ts', greeter: 'src/greeter.ts' } })],
17
+ plugins: [funcd({ functions: { front: 'src/front/main.ts', greeter: 'src/greeter/main.ts' } })],
17
18
  });
18
19
  ```
19
20
 
@@ -22,16 +23,20 @@ chunk:
22
23
 
23
24
  - `dist/<name>.mjs`: the handler with its dependencies inlined, ESM for Node 22, Node builtins left
24
25
  external, not minified.
25
- - `dist/<name>.funcdctl.yaml`: the function's manifest, copied from `<name>.funcdctl.yaml` or else
26
- `funcdctl.yaml` at the project root. A function with neither fails the build.
26
+ - `dist/<name>.contract.yaml` (or `.json`): the function's contract file, copied from
27
+ `<name>.contract.yaml` or `<name>.contract.json` at the project root. A function with neither, or
28
+ with both, fails the build.
27
29
 
28
- Then `funcdctl push dist/<name>.mjs <ref>` takes the contract and runtime from the manifest beside
29
- the file.
30
+ Then `funcdctl push dist/<name>.mjs <ref>` takes the runtime and the contract from the contract file
31
+ beside the file. The Function's bindings and stores live in its resource file, not here.
32
+
33
+ `funcdctl.yaml` is read nowhere: a project that still has `funcdctl.yaml` or `<name>.funcdctl.yaml`
34
+ fails the build, naming the contract file that replaces it.
30
35
 
31
36
  | Option | Default | What |
32
37
  |---|---|---|
33
- | `functions` | `{ <root dir name>: 'src/handler.ts' }` | function name → handler source, relative to the Vite root |
34
- | `outDir` | `'dist'` | where `<name>.mjs` goes; the project root (`'.'`) works too, and then the manifest is not copied |
38
+ | `functions` | `{ <root dir name>: 'src/main.ts' }` | function name → handler source, relative to the Vite root |
39
+ | `outDir` | `'dist'` | where `<name>.mjs` goes; the project root (`'.'`) works too, and then the contract file is not copied |
35
40
 
36
41
  Each function builds into a staging directory under Vite's cache directory, and the plugin copies the
37
42
  result into `outDir`, so a shared output directory is never emptied.
@@ -40,12 +45,11 @@ result into `outDir`, so a shared output directory is never emptied.
40
45
 
41
46
  `vite build --watch` keeps `outDir` current, one function at a time (funcd ADR-0244):
42
47
 
43
- - When a function's rebuild ends, its `<name>.mjs` and manifest are written to `outDir`; the other
48
+ - When a function's rebuild ends, its `<name>.mjs` and contract file are written to `outDir`; the other
44
49
  functions' files are not touched. Each file is written beside its target and renamed over it, so a
45
50
  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.
51
+ - Saving a function's contract file rebuilds that function only. A contract file created while the
52
+ watch runs takes effect at that function's next rebuild.
49
53
  - A failed rebuild prints the error and keeps the function's last good files; the watch goes on.
50
54
 
51
55
  Beside it, `funcdctl dev` reloads each function whose files change, so a saved handler answers with
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { Plugin } from 'vite';
2
2
  export interface FuncdPluginOptions {
3
- /** Function name → handler source, relative to the Vite root. Default: { [basename(root)]: 'src/handler.ts' }. */
3
+ /** Function name → handler source, relative to the Vite root. Default: { [basename(root)]: 'src/main.ts' }. */
4
4
  functions?: Record<string, string>;
5
- /** Directory for `<name>.mjs` (and the copied manifest), relative to the Vite root. Default: 'dist'. */
5
+ /** Directory for `<name>.mjs` (and the copied contract file), relative to the Vite root. Default: 'dist'. */
6
6
  outDir?: string;
7
7
  }
8
8
  /** The Vite environment name for a function name (Vite allows only [A-Za-z0-9_$]). */
package/dist/index.js CHANGED
@@ -1,14 +1,15 @@
1
1
  // The funcd Vite plugin (funcd ADR-0144): it bundles each function into one self-contained ES module that the
2
- // nodejs22 shim loads, and puts the function's funcdctl.yaml where `funcdctl push` resolves it. Each function is its
3
- // own Vite environment, because one build with several inputs splits shared code into chunks. Each environment builds
2
+ // nodejs22 shim loads, and copies the function's contract file beside it, where `funcdctl push` reads it (funcd
3
+ // ADR-0247). Each function is its 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
5
  // outDir once per function, and warns when outDir is the project root. Under `vite build --watch` (funcd ADR-0244)
6
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.
7
+ // environment's write ends instead, and its contract file is watched too.
8
8
  import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync } from 'node:fs';
9
9
  import { builtinModules } from 'node:module';
10
10
  import { basename, dirname, join, resolve } from 'node:path';
11
11
  const prefix = 'funcd_';
12
+ const contractSuffixes = ['.contract.yaml', '.contract.json'];
12
13
  const manifestSuffix = '.funcdctl.yaml';
13
14
  const genericManifest = 'funcdctl.yaml';
14
15
  // Bundled CommonJS calls require() for Node builtins; an ES module has none, so give it a real one.
@@ -29,7 +30,7 @@ export function funcd(options = {}) {
29
30
  config(userConfig) {
30
31
  root = resolve(userConfig.root ?? process.cwd());
31
32
  staging = resolve(root, userConfig.cacheDir ?? 'node_modules/.vite', 'funcd');
32
- functions = options.functions ?? { [basename(root)]: 'src/handler.ts' };
33
+ functions = options.functions ?? { [basename(root)]: 'src/main.ts' };
33
34
  const names = Object.keys(functions);
34
35
  if (names.length === 0) {
35
36
  throw new Error('funcd: no function to build (options.functions is empty)');
@@ -43,7 +44,7 @@ export function funcd(options = {}) {
43
44
  throw new Error(`funcd: functions "${other}" and "${name}" map to the same Vite environment "${env}"`);
44
45
  }
45
46
  byEnvironment.set(env, name);
46
- manifestFor(root, name); // fail before building when a function has no manifest
47
+ contractFor(root, name); // fail before building when a function has no contract file
47
48
  environments[env] = {
48
49
  consumer: 'server',
49
50
  resolve: { noExternal: true },
@@ -87,7 +88,7 @@ export function funcd(options = {}) {
87
88
  buildStart() {
88
89
  const name = byEnvironment.get(this.environment.name);
89
90
  if (name !== undefined) {
90
- this.addWatchFile(manifestFor(root, name));
91
+ this.addWatchFile(contractFor(root, name));
91
92
  }
92
93
  },
93
94
  writeBundle() {
@@ -99,13 +100,13 @@ export function funcd(options = {}) {
99
100
  };
100
101
  return [plugin];
101
102
  }
102
- // deliver copies each function's staged <name>.mjs and its manifest into outDir.
103
+ // deliver copies each function's staged <name>.mjs and its contract file into outDir.
103
104
  function deliver(root, staging, outDir, names, copy) {
104
105
  mkdirSync(outDir, { recursive: true });
105
106
  for (const name of names) {
106
107
  copy(join(staging, environmentName(name), `${name}.mjs`), join(outDir, `${name}.mjs`));
107
108
  }
108
- copyManifests(root, outDir, names, copy);
109
+ copyContracts(root, outDir, names, copy);
109
110
  }
110
111
  // replaceFile copies from over to by a rename, so a reader that polls outDir (funcdctl dev) never loads half a file.
111
112
  // The temporary file is a dot-file beside the target: the rename stays on one filesystem, and funcdctl dev's
@@ -121,26 +122,40 @@ function replaceFile(from, to) {
121
122
  throw err;
122
123
  }
123
124
  }
124
- // manifestFor returns the function's manifest: <root>/<name>.funcdctl.yaml, else <root>/funcdctl.yaml (ADR-0124).
125
- function manifestFor(root, name) {
126
- const stem = join(root, name + manifestSuffix);
127
- if (existsSync(stem)) {
128
- return stem;
125
+ // contractFor returns the function's contract file, <root>/<name>.contract.yaml or .json (funcd ADR-0247). A function
126
+ // with neither, or with both, fails, and so does a project that still has a funcdctl.yaml, which nothing reads now.
127
+ function contractFor(root, name) {
128
+ const manifests = [join(root, name + manifestSuffix), join(root, genericManifest)].filter((p) => existsSync(p));
129
+ if (manifests.length > 0) {
130
+ throw new Error(`funcd: ${manifests.join(' and ')} is no longer read (funcd ADR-0247): put function "${name}"'s runtime, ` +
131
+ `protocol and contract in ${join(root, name + contractSuffixes[0])}, and its bindings in its Function resource file`);
129
132
  }
130
- const generic = join(root, genericManifest);
131
- if (existsSync(generic)) {
132
- return generic;
133
+ const [yaml, json] = contractSuffixes.map((suffix) => join(root, name + suffix));
134
+ const found = [yaml, json].filter((p) => existsSync(p));
135
+ if (found.length === 0) {
136
+ throw new Error(`funcd: function "${name}" has no contract file: neither ${yaml} nor ${json} exists`);
133
137
  }
134
- throw new Error(`funcd: function "${name}" has no manifest: neither ${stem} nor ${generic} exists`);
138
+ if (found.length > 1) {
139
+ throw new Error(`funcd: function "${name}" has two contract files, ${yaml} and ${json}: keep one`);
140
+ }
141
+ return found[0];
135
142
  }
136
- // copyManifests puts each manifest beside its output as <name>.funcdctl.yaml, unless the output directory is the one
137
- // that holds it: there funcdctl's resolver already finds it for <name>.mjs.
138
- function copyManifests(root, outDir, names, copy) {
143
+ // copyContracts puts each contract file beside its output, unless the output directory is the one that holds it, and
144
+ // removes a copy of the other variant there, which push would refuse beside it.
145
+ function copyContracts(root, outDir, names, copy) {
139
146
  for (const name of names) {
140
- const manifest = manifestFor(root, name);
141
- if (dirname(manifest) === outDir) {
147
+ const contract = contractFor(root, name);
148
+ if (dirname(contract) === outDir) {
142
149
  continue;
143
150
  }
144
- copy(manifest, join(outDir, name + manifestSuffix));
151
+ for (const suffix of contractSuffixes) {
152
+ const target = join(outDir, name + suffix);
153
+ if (contract.endsWith(suffix)) {
154
+ copy(contract, target);
155
+ }
156
+ else {
157
+ rmSync(target, { force: true });
158
+ }
159
+ }
145
160
  }
146
161
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funcd-dev/vite-plugin",
3
- "version": "0.18.0",
3
+ "version": "0.19.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",