@null3d/vite-plugin 0.0.0-stage → 0.0.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 (50) hide show
  1. package/LICENSE-APACHE +176 -0
  2. package/LICENSE-MIT +21 -0
  3. package/THIRD-PARTY-NOTICES.txt +398 -0
  4. package/dist/shader-compiler.stamp.json +4 -0
  5. package/dist/shader-compiler.wasm +0 -0
  6. package/lib/assets.d.ts +94 -0
  7. package/lib/assets.js +189 -0
  8. package/lib/client.d.ts +21 -0
  9. package/lib/compile-pool.d.ts +25 -0
  10. package/lib/compile-pool.js +90 -0
  11. package/lib/compile-worker.js +11 -0
  12. package/lib/compiler-calls.d.ts +26 -0
  13. package/lib/compiler-calls.js +97 -0
  14. package/lib/compiler-stamp.d.ts +8 -0
  15. package/lib/compiler-stamp.js +34 -0
  16. package/lib/declarations.d.ts +10 -0
  17. package/lib/declarations.js +80 -0
  18. package/lib/engine-shaders-local.d.ts +6 -0
  19. package/lib/engine-shaders-local.js +75 -0
  20. package/lib/engine-shaders.d.ts +61 -0
  21. package/lib/engine-shaders.js +170 -0
  22. package/lib/hash-pool.d.ts +24 -0
  23. package/lib/hash-pool.js +84 -0
  24. package/lib/hash-worker.js +53 -0
  25. package/lib/hot.d.ts +53 -0
  26. package/lib/hot.js +101 -0
  27. package/lib/index.d.ts +95 -0
  28. package/lib/index.js +895 -0
  29. package/lib/material-builds.d.ts +91 -0
  30. package/lib/material-builds.js +236 -0
  31. package/lib/offline.d.ts +92 -0
  32. package/lib/offline.js +304 -0
  33. package/lib/pack.d.ts +30 -0
  34. package/lib/pack.js +112 -0
  35. package/lib/package-files.d.ts +12 -0
  36. package/lib/package-files.js +39 -0
  37. package/lib/paragraphs.d.ts +19 -0
  38. package/lib/paragraphs.js +131 -0
  39. package/lib/shader-cache.d.ts +59 -0
  40. package/lib/shader-cache.js +213 -0
  41. package/lib/shader-compiler.d.ts +258 -0
  42. package/lib/shader-compiler.js +169 -0
  43. package/lib/shader-types.d.ts +261 -0
  44. package/lib/shader-types.js +4 -0
  45. package/lib/wgsl.d.ts +118 -0
  46. package/lib/wgsl.js +446 -0
  47. package/lib/worker-preload.d.ts +17 -0
  48. package/lib/worker-preload.js +65 -0
  49. package/package.json +69 -5
  50. package/README.md +0 -3
package/lib/hot.js ADDED
@@ -0,0 +1,101 @@
1
+ import { findTaggedWgsl } from "./wgsl.js";
2
+ /** The event that carries hot updates, on Vite's channel and on the page's global object. */
3
+ export const WGSL_UPDATE_EVENT = 'null3d:wgsl';
4
+ /** The module that hands hot updates to the engines on a page, which the dev server injects. */
5
+ export const HOT_CLIENT = 'virtual:null3d/hot';
6
+ /** The module id of the hot client, which no other plugin loads. */
7
+ export const HOT_CLIENT_ID = `\0${HOT_CLIENT}`;
8
+ /** The hot client's name or its module id, as Vite's hook filters match them. */
9
+ export const HOT_CLIENT_ADDRESS = /^\0?virtual:null3d\/hot$/;
10
+ /** The hot client's code. It also closes Vite's overlay, which may show an earlier WGSL error. */
11
+ export const HOT_CLIENT_CODE = `if (import.meta.hot) {
12
+ import.meta.hot.on(${JSON.stringify(WGSL_UPDATE_EVENT)}, ({ updates }) => {
13
+ for (const overlay of document.querySelectorAll('vite-error-overlay')) overlay.close();
14
+ globalThis.dispatchEvent(new CustomEvent(${JSON.stringify(WGSL_UPDATE_EVENT)}, { detail: updates }));
15
+ console.debug('[null3D] hot updated: ' + updates.map((update) => update.key).join(', '));
16
+ });
17
+ }
18
+ `;
19
+ /**
20
+ * The key of a project's WGSL: a `.wgsl` file's path from the project's root, or that of a script
21
+ * with the place of a tagged literal among the script's literals, from 0.
22
+ */
23
+ export function hotKey(path, literal) {
24
+ return literal === undefined ? path : `${path}#${literal}`;
25
+ }
26
+ /**
27
+ * What a running page keeps of compiled WGSL, as text: everything of a custom material but its
28
+ * builds. A material whose new WGSL keeps it can swap its shader. Null for a whole shader, which
29
+ * code outside the engine uses as it likes, and for a custom effect or tone curve, which the
30
+ * engine does not swap. Only a reload brings either a new one.
31
+ */
32
+ export function contractOf(shader) {
33
+ if (shader.kind !== 'material')
34
+ return null;
35
+ const { uniforms, textures, locations, attributes, baseColor, objectIds, sceneDepth, alpha } = shader;
36
+ // A vertex offset gives the material the builds of its shadow casters, and reading an index or
37
+ // an id makes the engine keep the table of object ranges. Reading the scene's depth makes it
38
+ // bind the copy. The engine fixes each when it creates the material.
39
+ const casts = shader.functions.includes('vertexOffset');
40
+ return JSON.stringify({
41
+ uniforms,
42
+ textures,
43
+ locations,
44
+ attributes,
45
+ baseColor,
46
+ casts,
47
+ objectIds,
48
+ sceneDepth,
49
+ // A surface that sets or reads the alpha gives the material the cut-out builds, and the
50
+ // engine fixes those and the draw list's mask when it creates it.
51
+ alpha,
52
+ // A draw shader's pipelines declare no vertex buffer, so a change to or from one reloads.
53
+ meshless: shader.meshless === true,
54
+ });
55
+ }
56
+ /**
57
+ * The code of a script with the text of its tagged literals left out, so two scripts whose code
58
+ * differs only in their WGSL give the same text.
59
+ */
60
+ function frameOf(code, literals) {
61
+ let frame = '';
62
+ let at = 0;
63
+ for (const { start, end } of literals) {
64
+ frame += `${code.slice(at, start)}\`\``;
65
+ at = end;
66
+ }
67
+ return frame + code.slice(at);
68
+ }
69
+ /**
70
+ * The tagged literals of a script whose WGSL differs between two versions of its code, or null
71
+ * when the code differs elsewhere too, so the script itself must run again.
72
+ */
73
+ export function changedLiterals(before, after, file) {
74
+ const old = findTaggedWgsl(before, file);
75
+ const now = findTaggedWgsl(after, file);
76
+ if (old.length === 0 || old.length !== now.length)
77
+ return null;
78
+ if (frameOf(before, old) !== frameOf(after, now))
79
+ return null;
80
+ return now.flatMap((literal, index) => literal.source === old[index]?.source ? [] : [{ index, literal }]);
81
+ }
82
+ /** What the dev server knows of the WGSL that pages run, to judge each change. */
83
+ export class HotState {
84
+ /** The contract of each key's WGSL as the page runs it. */
85
+ contracts = new Map();
86
+ /** The code of each script with tagged WGSL, as the page runs it, by file. */
87
+ scripts = new Map();
88
+ /** Notes the WGSL that the page runs under a key. */
89
+ remember(key, shader) {
90
+ this.contracts.set(key, contractOf(shader));
91
+ }
92
+ /**
93
+ * True when the page can swap the WGSL under a key for `shader` without a reload: both are
94
+ * custom materials with the same contract.
95
+ */
96
+ swaps(key, shader) {
97
+ const before = this.contracts.get(key);
98
+ const after = contractOf(shader);
99
+ return before !== undefined && before !== null && before === after;
100
+ }
101
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,95 @@
1
+ import type { Connect, HtmlTagDescriptor, Plugin, Rollup } from 'vite';
2
+ import { type AssetOptions } from './assets.js';
3
+ export type { AssetOptions } from './assets.js';
4
+ export { FILES_LIST, type OfflineFiles } from './offline.js';
5
+ export type * from './shader-types.js';
6
+ /** Headers that make a page cross-origin isolated, which shared memory and worker threads need. */
7
+ export declare const ISOLATION_HEADERS: Readonly<Record<string, string>>;
8
+ /** Where `bun run dev-cert` writes the local HTTPS certificate. */
9
+ export declare const DEV_CERT_DIR = "target/dev-cert";
10
+ export interface Null3dPluginOptions {
11
+ /**
12
+ * Serve HTTPS on the local network with the certificate in `certDir`, such as one made with
13
+ * mkcert. Phones and tablets reached over the network need HTTPS for shared memory and WebGPU.
14
+ */
15
+ https?: boolean;
16
+ /** Directory that holds `cert.pem` and `key.pem`; the default is `target/dev-cert` under the project root. */
17
+ certDir?: string;
18
+ /**
19
+ * Write a TypeScript declaration beside each `.wgsl` file that a module imports, such as
20
+ * `glow.wgsl.d.ts` beside `glow.wgsl`, with the types of the file's uniforms. The default is
21
+ * true. Set it to false in a project without TypeScript.
22
+ */
23
+ wgslDeclarations?: boolean;
24
+ /**
25
+ * Options of the asset tool for the models that modules import with `?optimized`, such as
26
+ * `import city from './city.glb?optimized'`. The import gives the optimized model's address,
27
+ * which `assets.loadGltf` takes. The tool comes from `@null3d/cli`, which the project installs.
28
+ */
29
+ assets?: AssetOptions;
30
+ /**
31
+ * Let the page's address set the engine's test switches, such as `?gpu=` and `?hold=`, in
32
+ * production builds too. Development builds always read them. The default is false, so a link
33
+ * cannot change how a shipped game runs. Turn it on for builds of test and benchmark pages.
34
+ */
35
+ urlSwitches?: boolean;
36
+ }
37
+ /** Sets the isolation headers on every response, including `.wasm` files and worker scripts. */
38
+ export declare const isolationMiddleware: Connect.NextHandleFunction;
39
+ /**
40
+ * Lets the browser keep hashed build files, as a host should. Each engine thread loads its own copy
41
+ * of its script and the core's loader; without this, the browser checks each copy with the server
42
+ * in turn, one round trip per thread.
43
+ */
44
+ export declare const immutableAssetsMiddleware: Connect.NextHandleFunction;
45
+ /** The engine core's files, which every production build ships. */
46
+ export declare const CORE_FILES: string[];
47
+ /**
48
+ * The engine's early script, which starts the core's download as soon as a page's HTML arrives.
49
+ * A production build ships it as a file of its own, and each page whose scripts load the core gets
50
+ * a script tag for it.
51
+ */
52
+ export declare const EARLY_CORE_MODULE = "@null3d/engine/early-core";
53
+ /**
54
+ * The script tag of the early script for a built page, or undefined when the page's scripts do not
55
+ * load the engine core. The core files are the WebAssembly files that the early script names, and a
56
+ * page loads the core when one of its chunks names one of them too. `htmlFile` is the page's path in
57
+ * the build, and `base` the build's public base.
58
+ */
59
+ export declare function earlyCoreTag(early: Rollup.OutputChunk, entry: Rollup.OutputChunk, bundle: Rollup.OutputBundle, htmlFile: string, base: string): HtmlTagDescriptor | undefined;
60
+ /** The file that a production build writes beside the page, with every package's notices. */
61
+ export declare const NOTICES_FILE = "null3d-third-party-notices.txt";
62
+ /**
63
+ * The third-party notices of the null3D packages that the project depends on: the engine's first,
64
+ * then each add-on's, by name. The tools' notices stay out, since a page ships none of their code.
65
+ * Null when none has notices, as in a project without the engine.
66
+ */
67
+ export declare function thirdPartyNotices(root: string): string | null;
68
+ /** Vite's setting that decides which assets become data: addresses. */
69
+ type InlineLimit = number | ((file: string, content: Buffer) => boolean | undefined);
70
+ /**
71
+ * Vite's inline limit with one change: no file of a null3D package becomes a data: address. The
72
+ * engine loads its workers' scripts, its WebAssembly and its shader files by address, and a strict
73
+ * Content-Security-Policy blocks a data: address, as does a worker's origin. `own` is the
74
+ * project's setting, which every other file keeps.
75
+ */
76
+ export declare function inlineLimit(own: InlineLimit | undefined, isPackageFile?: (file: string) => boolean): InlineLimit;
77
+ /**
78
+ * Empties the preload lists of the chunks that a worker imports. Vite's preload helper adds a link
79
+ * to the page's head for each file that a dynamic import needs, and it reads `document`, which a
80
+ * worker has not. A sketch module and a task module run in workers, with every chunk they import,
81
+ * so their lists go: an import still fetches its own file, as any worker's import does.
82
+ */
83
+ export declare function emptyWorkerPreloads(bundle: Rollup.OutputBundle, seeds: readonly string[]): void;
84
+ /**
85
+ * The core files missing from the installed engine package, or null when the project does not
86
+ * install the engine.
87
+ */
88
+ export declare function missingCoreFiles(root: string): string[] | null;
89
+ /**
90
+ * The null3D Vite plugin: isolation headers on the dev and preview servers, optional HTTPS, WGSL
91
+ * compiled for WebGPU and WebGL2 in dev and in builds, hot updates of WGSL on the dev server, and a
92
+ * production build that compiles each sketch module, ships the engine core and starts its download
93
+ * from each page that loads it.
94
+ */
95
+ export default function null3d(options?: Null3dPluginOptions): Plugin[];