@funcd-dev/vite-plugin 0.0.0-bootstrap.0 → 0.4.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 +35 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.js +111 -0
- package/package.json +30 -7
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
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).
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
yarn add -D vite @funcd-dev/vite-plugin
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
// vite.config.ts
|
|
12
|
+
import { funcd } from '@funcd-dev/vite-plugin';
|
|
13
|
+
import { defineConfig } from 'vite';
|
|
14
|
+
|
|
15
|
+
export default defineConfig({
|
|
16
|
+
plugins: [funcd({ functions: { front: 'src/front.ts', greeter: 'src/greeter.ts' } })],
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`vite build` builds each function as its own Vite environment, so no code is split into a shared
|
|
21
|
+
chunk:
|
|
22
|
+
|
|
23
|
+
- `dist/<name>.mjs`: the handler with its dependencies inlined, ESM for Node 22, Node builtins left
|
|
24
|
+
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.
|
|
27
|
+
|
|
28
|
+
Then `funcdctl push dist/<name>.mjs <ref>` takes the contract and runtime from the manifest beside
|
|
29
|
+
the file.
|
|
30
|
+
|
|
31
|
+
| Option | Default | What |
|
|
32
|
+
|---|---|---|
|
|
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 |
|
|
35
|
+
|
|
36
|
+
Each function builds into a staging directory under Vite's cache directory, and the plugin copies the
|
|
37
|
+
result into `outDir`, so a shared output directory is never emptied.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
export interface FuncdPluginOptions {
|
|
3
|
+
/** Function name → handler source, relative to the Vite root. Default: { [basename(root)]: 'src/handler.ts' }. */
|
|
4
|
+
functions?: Record<string, string>;
|
|
5
|
+
/** Directory for `<name>.mjs` (and the copied manifest), relative to the Vite root. Default: 'dist'. */
|
|
6
|
+
outDir?: string;
|
|
7
|
+
}
|
|
8
|
+
/** The Vite environment name for a function name (Vite allows only [A-Za-z0-9_$]). */
|
|
9
|
+
export declare function environmentName(name: string): string;
|
|
10
|
+
/** One Vite environment per function; `vite build` writes `<outDir>/<name>.mjs` for each. */
|
|
11
|
+
export declare function funcd(options?: FuncdPluginOptions): Plugin[];
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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
|
|
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';
|
|
7
|
+
import { builtinModules } from 'node:module';
|
|
8
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
9
|
+
const prefix = 'funcd_';
|
|
10
|
+
const manifestSuffix = '.funcdctl.yaml';
|
|
11
|
+
const genericManifest = 'funcdctl.yaml';
|
|
12
|
+
// Bundled CommonJS calls require() for Node builtins; an ES module has none, so give it a real one.
|
|
13
|
+
const requireBanner = "import { createRequire as __funcdCreateRequire } from 'node:module'; const require = __funcdCreateRequire(import.meta.url);";
|
|
14
|
+
/** The Vite environment name for a function name (Vite allows only [A-Za-z0-9_$]). */
|
|
15
|
+
export function environmentName(name) {
|
|
16
|
+
return prefix + name.replace(/[^A-Za-z0-9_$]/g, '_');
|
|
17
|
+
}
|
|
18
|
+
/** One Vite environment per function; `vite build` writes `<outDir>/<name>.mjs` for each. */
|
|
19
|
+
export function funcd(options = {}) {
|
|
20
|
+
const outDirOption = options.outDir ?? 'dist';
|
|
21
|
+
let functions = {};
|
|
22
|
+
let root = '';
|
|
23
|
+
let staging = '';
|
|
24
|
+
const plugin = {
|
|
25
|
+
name: 'funcd',
|
|
26
|
+
config(userConfig) {
|
|
27
|
+
root = resolve(userConfig.root ?? process.cwd());
|
|
28
|
+
staging = resolve(root, userConfig.cacheDir ?? 'node_modules/.vite', 'funcd');
|
|
29
|
+
functions = options.functions ?? { [basename(root)]: 'src/handler.ts' };
|
|
30
|
+
const names = Object.keys(functions);
|
|
31
|
+
if (names.length === 0) {
|
|
32
|
+
throw new Error('funcd: no function to build (options.functions is empty)');
|
|
33
|
+
}
|
|
34
|
+
const byEnvironment = new Map();
|
|
35
|
+
const environments = {};
|
|
36
|
+
for (const name of names) {
|
|
37
|
+
const env = environmentName(name);
|
|
38
|
+
const other = byEnvironment.get(env);
|
|
39
|
+
if (other !== undefined) {
|
|
40
|
+
throw new Error(`funcd: functions "${other}" and "${name}" map to the same Vite environment "${env}"`);
|
|
41
|
+
}
|
|
42
|
+
byEnvironment.set(env, name);
|
|
43
|
+
manifestFor(root, name); // fail before building when a function has no manifest
|
|
44
|
+
environments[env] = {
|
|
45
|
+
consumer: 'server',
|
|
46
|
+
resolve: { noExternal: true },
|
|
47
|
+
build: {
|
|
48
|
+
outDir: join(staging, env),
|
|
49
|
+
emptyOutDir: true,
|
|
50
|
+
copyPublicDir: false,
|
|
51
|
+
minify: false,
|
|
52
|
+
target: 'node22',
|
|
53
|
+
ssr: true,
|
|
54
|
+
rolldownOptions: {
|
|
55
|
+
input: { [name]: resolve(root, functions[name]) },
|
|
56
|
+
external: [...builtinModules, /^node:/],
|
|
57
|
+
output: {
|
|
58
|
+
format: 'es',
|
|
59
|
+
entryFileNames: '[name].mjs',
|
|
60
|
+
codeSplitting: false,
|
|
61
|
+
banner: requireBanner,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
environments,
|
|
69
|
+
builder: {
|
|
70
|
+
async buildApp(builder) {
|
|
71
|
+
for (const env of Object.values(builder.environments)) {
|
|
72
|
+
if (byEnvironment.has(env.name)) {
|
|
73
|
+
await builder.build(env);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
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`));
|
|
80
|
+
}
|
|
81
|
+
copyManifests(root, outDir, names);
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
return [plugin];
|
|
88
|
+
}
|
|
89
|
+
// manifestFor returns the function's manifest: <root>/<name>.funcdctl.yaml, else <root>/funcdctl.yaml (ADR-0124).
|
|
90
|
+
function manifestFor(root, name) {
|
|
91
|
+
const stem = join(root, name + manifestSuffix);
|
|
92
|
+
if (existsSync(stem)) {
|
|
93
|
+
return stem;
|
|
94
|
+
}
|
|
95
|
+
const generic = join(root, genericManifest);
|
|
96
|
+
if (existsSync(generic)) {
|
|
97
|
+
return generic;
|
|
98
|
+
}
|
|
99
|
+
throw new Error(`funcd: function "${name}" has no manifest: neither ${stem} nor ${generic} exists`);
|
|
100
|
+
}
|
|
101
|
+
// copyManifests puts each manifest beside its output as <name>.funcdctl.yaml, unless the output directory is the one
|
|
102
|
+
// that holds it: there funcdctl's resolver already finds it for <name>.mjs.
|
|
103
|
+
function copyManifests(root, outDir, names) {
|
|
104
|
+
for (const name of names) {
|
|
105
|
+
const manifest = manifestFor(root, name);
|
|
106
|
+
if (dirname(manifest) === outDir) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
copyFileSync(manifest, join(outDir, name + manifestSuffix));
|
|
110
|
+
}
|
|
111
|
+
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@funcd-dev/vite-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Vite plugin that bundles each funcd function into one self-contained ES module",
|
|
5
6
|
"license": "Apache-2.0",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
8
9
|
"url": "git+https://github.com/pyvvo/funcd-typescript.git",
|
|
9
10
|
"directory": "vite-plugin"
|
|
10
11
|
},
|
|
11
|
-
"homepage": "https://github.com/pyvvo/funcd-typescript#readme",
|
|
12
|
+
"homepage": "https://github.com/pyvvo/funcd-typescript/tree/main/vite-plugin#readme",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=22"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
12
25
|
"publishConfig": {
|
|
13
26
|
"access": "public"
|
|
14
27
|
},
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
"scripts": {
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"test": "node --test --experimental-strip-types test/*.test.ts",
|
|
31
|
+
"build": "tsc -p tsconfig.lib.json"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"vite": "^8.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^22.10.0",
|
|
38
|
+
"typescript": "^5.7.0",
|
|
39
|
+
"vite": "^8.3.2"
|
|
40
|
+
}
|
|
41
|
+
}
|