@null3d/vite-plugin 0.1.2 → 0.1.3
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/dist/shader-compiler.stamp.json +1 -1
- package/dist/shader-compiler.wasm +0 -0
- package/lib/index.js +70 -48
- package/lib/paragraphs.d.ts +81 -10
- package/lib/paragraphs.js +289 -46
- package/lib/shader-list.js +1 -1
- package/package.json +3 -3
|
Binary file
|
package/lib/index.js
CHANGED
|
@@ -16,9 +16,9 @@ import { MATERIAL_BUILDS_ADDRESS, MaterialBuilds, ModuleBuilds } from "./materia
|
|
|
16
16
|
import { engineModule, FILES_LIST, featuresOf, listedFiles, missingWorkerAssets, offlineFiles, workerFilesPlugin, } from "./offline.js";
|
|
17
17
|
import { narrowShaderPack, ShaderPack, UNPACK } from "./pack.js";
|
|
18
18
|
import { null3dPackages } from "./package-files.js";
|
|
19
|
-
import { emitParagraphTables,
|
|
19
|
+
import { emitParagraphTables, rewriteCopyFile, tableImportsOfFile, } from "./paragraphs.js";
|
|
20
20
|
import { ShaderCache, shaderCacheOff } from "./shader-cache.js";
|
|
21
|
-
import { builtWords, holdWord, moduleWords,
|
|
21
|
+
import { builtWords, holdWord, moduleWords, narrowShaderModule, ShaderList, shaderMarkersModule, shippedTargets, targetOfModule, UNLISTED_MODULE, UnlistedWords, wordText, } from "./shader-list.js";
|
|
22
22
|
import { compileLiteral, compileTaggedWgsl, compileWgslFile, WGSL_TAG, } from "./wgsl.js";
|
|
23
23
|
import { directImports, workerPreloadPlugin } from "./worker-preload.js";
|
|
24
24
|
export { FILES_LIST } from "./offline.js";
|
|
@@ -440,7 +440,7 @@ export default function null3d(options = {}) {
|
|
|
440
440
|
const bytes = join(narrowedFolder, name);
|
|
441
441
|
if (!existsSync(bytes))
|
|
442
442
|
writeFileSync(bytes, text);
|
|
443
|
-
return { name, bytes, source: source ?? bytes };
|
|
443
|
+
return { name, stem, bytes, source: source ?? bytes };
|
|
444
444
|
};
|
|
445
445
|
/**
|
|
446
446
|
* The copy of a generated shader file. A file of a GPU path that this build does not ship gives
|
|
@@ -461,13 +461,13 @@ export default function null3d(options = {}) {
|
|
|
461
461
|
}
|
|
462
462
|
const list = shaderList;
|
|
463
463
|
if (list === undefined)
|
|
464
|
-
return { name: `${stem}-${await hasher.hash(file)}.js`, bytes: file, source: file };
|
|
464
|
+
return { name: `${stem}-${await hasher.hash(file)}.js`, stem, bytes: file, source: file };
|
|
465
465
|
const { hash, words } = await hasher.scan(file);
|
|
466
466
|
for (const word of words)
|
|
467
467
|
holdWord(heldWords, word);
|
|
468
468
|
const kept = words.filter((word) => list.holds(word)).length;
|
|
469
469
|
if (kept === words.length)
|
|
470
|
-
return { name: `${stem}-${hash}.js`, bytes: file, source: file };
|
|
470
|
+
return { name: `${stem}-${hash}.js`, stem, bytes: file, source: file };
|
|
471
471
|
if (kept === 0)
|
|
472
472
|
return narrowedCopy(UNLISTED_STEM, UNLISTED_MODULE);
|
|
473
473
|
const narrowed = narrowShaderModule(readFileSync(file, 'utf8'), (word) => list.holds(word), file);
|
|
@@ -573,7 +573,6 @@ export default function null3d(options = {}) {
|
|
|
573
573
|
streamedShaderAssets,
|
|
574
574
|
workerFilesPlugin(workerFiles),
|
|
575
575
|
workerPreloadPlugin(),
|
|
576
|
-
shaderParagraphsPlugin(),
|
|
577
576
|
{
|
|
578
577
|
// A worker build keeps these plugins alone, so the engine's generated
|
|
579
578
|
// modules resolve here as they do on the page.
|
|
@@ -902,6 +901,68 @@ export default function null3d(options = {}) {
|
|
|
902
901
|
emptyWorkerPreloads(bundle, [...workerChunks].map((ref) => this.getFileName(ref)));
|
|
903
902
|
/** True for a build with a list that reached the engine's shader set. */
|
|
904
903
|
const listing = shaderList !== undefined && copiedShaders.size > 0;
|
|
904
|
+
// Each table takes its final content and name first, from every module that imports
|
|
905
|
+
// one: the copies that the streamed shader step made, and the bundle's own assets.
|
|
906
|
+
// Its name hashes that content, so a changed paragraph always has a new address.
|
|
907
|
+
const moduleCopies = [...copiedShaders.values()].map((copy) => ({
|
|
908
|
+
file: copy.bytes,
|
|
909
|
+
source: copy.source,
|
|
910
|
+
folder: assetsDir,
|
|
911
|
+
module: projectPath(root, copy.source),
|
|
912
|
+
}));
|
|
913
|
+
const paragraphs = emitParagraphTables.call(this, bundle, {
|
|
914
|
+
root,
|
|
915
|
+
narrow: shaderList !== undefined,
|
|
916
|
+
workerFiles,
|
|
917
|
+
copies: moduleCopies,
|
|
918
|
+
});
|
|
919
|
+
// A worker's copied file reaches this bundle without its build's records, so a
|
|
920
|
+
// renamed entry takes the record that the worker build kept for it.
|
|
921
|
+
for (const [from, to] of paragraphs.renamed) {
|
|
922
|
+
const worker = workerFiles.get(from);
|
|
923
|
+
if (worker === undefined)
|
|
924
|
+
continue;
|
|
925
|
+
workerFiles.delete(from);
|
|
926
|
+
workerFiles.set(to, worker);
|
|
927
|
+
}
|
|
928
|
+
// A copied module that imports a table takes the name of its final bytes, after the
|
|
929
|
+
// table's new address replaces the old one. The import head is read once and rewritten,
|
|
930
|
+
// and the rest of the source streams to a temp file. A module that imports no table
|
|
931
|
+
// keeps the name that the pool gave its bytes.
|
|
932
|
+
const tableNameOf = (source, stem) => {
|
|
933
|
+
const table = paragraphs.plan.get(join(dirname(source), `${stem}.js`));
|
|
934
|
+
if (table === undefined)
|
|
935
|
+
this.error(`null3D: ${source} imports ${stem}.js, which the build did not plan. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
936
|
+
return table.name;
|
|
937
|
+
};
|
|
938
|
+
const renamedCopies = new Map();
|
|
939
|
+
const copies = new Map();
|
|
940
|
+
const loads = new Map();
|
|
941
|
+
for (const [name, copy] of copiedShaders) {
|
|
942
|
+
const imports = tableImportsOfFile(copy.bytes);
|
|
943
|
+
if (imports.length === 0) {
|
|
944
|
+
copies.set(name, copy);
|
|
945
|
+
continue;
|
|
946
|
+
}
|
|
947
|
+
const names = new Map(imports.map(({ stem }) => [stem, tableNameOf(copy.source, stem)]));
|
|
948
|
+
narrowedFolder ??= mkdtempSync(join(tmpdir(), 'null3d-shaders-'));
|
|
949
|
+
const temp = join(narrowedFolder, `rewrite-${name}`);
|
|
950
|
+
const hash = rewriteCopyFile(copy.bytes, temp, names) ??
|
|
951
|
+
this.error(`null3D: ${projectPath(root, copy.source)} names no import, so the build cannot give it a table address. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
952
|
+
const finalName = `${copy.stem}-${hash}.js`;
|
|
953
|
+
copies.set(finalName, { ...copy, name: finalName, bytes: temp });
|
|
954
|
+
renamedCopies.set(name, finalName);
|
|
955
|
+
loads.set(finalName, [
|
|
956
|
+
...new Set(imports.map(({ stem }) => assetPath(tableNameOf(copy.source, stem)))),
|
|
957
|
+
]);
|
|
958
|
+
}
|
|
959
|
+
copiedShaders.clear();
|
|
960
|
+
for (const [name, copy] of copies)
|
|
961
|
+
copiedShaders.set(name, copy);
|
|
962
|
+
for (const table of paragraphs.tables.values()) {
|
|
963
|
+
copiedTables.files++;
|
|
964
|
+
copiedTables.bytes += table.source.length;
|
|
965
|
+
}
|
|
905
966
|
// Each file that names a copy now knows where it sits, so the mark becomes the
|
|
906
967
|
// address from there to the copy's own place in the output. A worker's files reach
|
|
907
968
|
// this bundle as the worker built them, so they take the same pass.
|
|
@@ -921,7 +982,8 @@ export default function null3d(options = {}) {
|
|
|
921
982
|
continue;
|
|
922
983
|
const from = posix.dirname(file.fileName);
|
|
923
984
|
const rewritten = text.replaceAll(/__null3d_shader__([^"'`\s)]+)/g, (_match, name) => {
|
|
924
|
-
const
|
|
985
|
+
const final = renamedCopies.get(name) ?? name;
|
|
986
|
+
const address = posix.relative(from, assetPath(final));
|
|
925
987
|
return address.startsWith('.') ? address : `./${address}`;
|
|
926
988
|
});
|
|
927
989
|
if (file.type === 'chunk')
|
|
@@ -934,48 +996,8 @@ export default function null3d(options = {}) {
|
|
|
934
996
|
const missing = listing ? (shaderList?.missing(heldWords) ?? []) : [];
|
|
935
997
|
if (missing.length > 0)
|
|
936
998
|
this.error(`null3D: the shader list ${shaderList?.file} names ${missing.map(wordText).join(', ')}, which no shader build of this project holds: none of the engine's shader set and none of its own shaders. Record the list again against this version of the engine with bunx @null3d/cli shaders record, or remove the word. A set that ${ENGINE_SHADERS_ENV} names must hold every listed word.`);
|
|
937
|
-
const tables = emitParagraphTables.call(this, bundle, root);
|
|
938
|
-
// The copied device modules are not in the bundle, so the tables of shared
|
|
939
|
-
// paragraphs that they import are emitted from here, once each, and each copy's
|
|
940
|
-
// module names them as its loads. Every build that copies the files needs them,
|
|
941
|
-
// whatever it is for.
|
|
942
|
-
const emittedTables = new Set([...tables.values()].flat());
|
|
943
|
-
const loads = new Map();
|
|
944
|
-
/** The file of each table that the copies import, by its place in the output. */
|
|
945
|
-
const tableFiles = new Map();
|
|
946
|
-
/** The names that the copies import from each of those tables. */
|
|
947
|
-
const tableNames = new Map();
|
|
948
|
-
for (const [name, copy] of copiedShaders) {
|
|
949
|
-
const own = [];
|
|
950
|
-
for (const { stem, names } of tableImportsOfFile(copy.bytes)) {
|
|
951
|
-
const table = assetPath(`${stem}.js`);
|
|
952
|
-
own.push(table);
|
|
953
|
-
if (emittedTables.has(table))
|
|
954
|
-
continue;
|
|
955
|
-
if (!tableFiles.has(table)) {
|
|
956
|
-
const file = join(dirname(copy.source), `${stem}.js`);
|
|
957
|
-
if (!existsSync(file))
|
|
958
|
-
this.error(`null3D: ${assetPath(name)} imports ${stem}.js, which the engine's shader modules do not hold. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
959
|
-
tableFiles.set(table, file);
|
|
960
|
-
tableNames.set(table, new Set());
|
|
961
|
-
}
|
|
962
|
-
for (const imported of names)
|
|
963
|
-
tableNames.get(table)?.add(imported);
|
|
964
|
-
}
|
|
965
|
-
loads.set(name, own);
|
|
966
|
-
}
|
|
967
|
-
// With a list, a table keeps only the paragraphs that the narrowed copies import.
|
|
968
|
-
for (const [table, file] of tableFiles) {
|
|
969
|
-
const whole = readFileSync(file);
|
|
970
|
-
const source = shaderList === undefined
|
|
971
|
-
? whole
|
|
972
|
-
: Buffer.from(narrowParagraphTable(whole.toString('utf8'), tableNames.get(table) ?? new Set(), file));
|
|
973
|
-
copiedTables.files++;
|
|
974
|
-
copiedTables.bytes += source.length;
|
|
975
|
-
this.emitFile({ type: 'asset', fileName: table, source });
|
|
976
|
-
}
|
|
977
999
|
if (buildingPages) {
|
|
978
|
-
const listed = listedFiles(bundle, workerFiles, packageOf, root,
|
|
1000
|
+
const listed = listedFiles(bundle, workerFiles, packageOf, root, paragraphs.files);
|
|
979
1001
|
// The copied shader files are not in the bundle, so the list of files for offline
|
|
980
1002
|
// play takes them here, by the module each one holds.
|
|
981
1003
|
for (const [name, copy] of copiedShaders) {
|
package/lib/paragraphs.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Rollup } from 'vite';
|
|
2
2
|
/** One import of a table of shared paragraphs: the table's file name without `.js`, and its names. */
|
|
3
3
|
export interface TableImport {
|
|
4
4
|
readonly stem: string;
|
|
@@ -10,16 +10,87 @@ export interface TableImport {
|
|
|
10
10
|
* in memory.
|
|
11
11
|
*/
|
|
12
12
|
export declare function tableImportsOfFile(file: string): TableImport[];
|
|
13
|
+
/** The hash that a file's final bytes give it: the first eight hexadecimal digits of SHA-256. */
|
|
14
|
+
export declare function contentHash(source: string | Uint8Array): string;
|
|
13
15
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
16
|
+
* A module that imports tables of shared paragraphs, as the collector reads it. The names live in
|
|
17
|
+
* the shared union while the module is read, so a build never keeps a list of names for each of the
|
|
18
|
+
* thousands of copied modules.
|
|
17
19
|
*/
|
|
18
|
-
export
|
|
20
|
+
export interface TableModuleFile {
|
|
21
|
+
/** The module's file on disk, whose head names its tables. */
|
|
22
|
+
readonly file: string;
|
|
23
|
+
/** Its source file, beside which its tables sit. */
|
|
24
|
+
readonly source: string;
|
|
25
|
+
/** The folder of the build's output that holds the module, where the table must sit. */
|
|
26
|
+
readonly folder: string;
|
|
27
|
+
/** Its name in a build error. */
|
|
28
|
+
readonly module: string;
|
|
29
|
+
}
|
|
30
|
+
/** A table that lacks a name that its importers take. The caller names the modules. */
|
|
31
|
+
export declare class TableExportError extends Error {
|
|
32
|
+
readonly path: string;
|
|
33
|
+
readonly missing: readonly string[];
|
|
34
|
+
constructor(path: string, missing: readonly string[]);
|
|
35
|
+
}
|
|
36
|
+
/** The names that the importing modules take from each table, by the table's source file. */
|
|
37
|
+
export type TableNames = ReadonlyMap<string, ReadonlySet<string>>;
|
|
38
|
+
/** A table of shared paragraphs, as the build emits it. */
|
|
39
|
+
export interface PlannedTable {
|
|
40
|
+
/** Its content-addressed file name, such as `shader-paragraphs-wgsl-1a2b3c4d.js`. */
|
|
41
|
+
readonly name: string;
|
|
42
|
+
/** Its final bytes, which its name hashes. */
|
|
43
|
+
readonly source: Buffer;
|
|
44
|
+
}
|
|
45
|
+
/** Each table's final content and name, by the source file that the table comes from. */
|
|
46
|
+
export type TablePlan = ReadonlyMap<string, PlannedTable>;
|
|
47
|
+
/**
|
|
48
|
+
* Works out each table's final content and name before any module that imports it takes a name. A
|
|
49
|
+
* build with a list of permutation words keeps only the paragraphs that the importing modules
|
|
50
|
+
* name, so a table's content follows the union of those names. A build without a list keeps the
|
|
51
|
+
* table whole. Throws a `TableExportError` when a table lacks a name that its importers take.
|
|
52
|
+
*/
|
|
53
|
+
export declare function planParagraphTables(tables: TableNames, narrow: boolean): TablePlan;
|
|
54
|
+
/**
|
|
55
|
+
* The text of a device module with each table import pointing at the table's content-addressed
|
|
56
|
+
* file. `names` maps each table's stem, such as `shader-paragraphs-wgsl`, to its final file name.
|
|
57
|
+
*/
|
|
58
|
+
export declare function rewriteTableImports(text: string, names: ReadonlyMap<string, string>): string;
|
|
59
|
+
/**
|
|
60
|
+
* Writes the module at `source` to `target` with its table imports pointing at their hashed files,
|
|
61
|
+
* and gives the hash of the bytes written. It reads only the import head, writes that rewritten,
|
|
62
|
+
* and copies the rest of the source in chunks, so a large module never sits in memory whole.
|
|
63
|
+
* Undefined for a file that names no import, which the caller copies as it is.
|
|
64
|
+
*/
|
|
65
|
+
export declare function rewriteCopyFile(source: string, target: string, names: ReadonlyMap<string, string>): string | undefined;
|
|
66
|
+
/** What a build must give `emitParagraphTables` beyond the bundle it reads. */
|
|
67
|
+
export interface ParagraphTableOptions {
|
|
68
|
+
/** The project's root, which a relative source name resolves against. */
|
|
69
|
+
readonly root?: string;
|
|
70
|
+
/** True for a build with a list of permutation words, which keeps only the named paragraphs. */
|
|
71
|
+
readonly narrow?: boolean;
|
|
72
|
+
/** What each worker build made, which names the source of a worker's copied file. */
|
|
73
|
+
readonly workerFiles?: ReadonlyMap<string, {
|
|
74
|
+
readonly moduleIds: readonly string[];
|
|
75
|
+
}>;
|
|
76
|
+
/** The modules that the bundle lacks, such as the shader files that the build copies itself. */
|
|
77
|
+
readonly copies?: readonly TableModuleFile[];
|
|
78
|
+
}
|
|
79
|
+
/** What `emitParagraphTables` gives the caller. */
|
|
80
|
+
export interface ParagraphTables {
|
|
81
|
+
/** The tables that each bundle file names, by the file's final output name. */
|
|
82
|
+
readonly files: Map<string, string[]>;
|
|
83
|
+
/** Each table's final content and name, by the source file that it comes from. */
|
|
84
|
+
readonly plan: TablePlan;
|
|
85
|
+
/** The tables that the build emits, by each table's place in the output. */
|
|
86
|
+
readonly tables: Map<string, PlannedTable>;
|
|
87
|
+
/** The bundle files that this call renamed, by the name each had before. */
|
|
88
|
+
readonly renamed: Map<string, string>;
|
|
89
|
+
}
|
|
19
90
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
91
|
+
* Emits each shared table of shader paragraphs beside the modules that import it, gives the files
|
|
92
|
+
* of the tables that each bundle file names, and names each bundle asset that imports one by the
|
|
93
|
+
* hash of its final bytes. A build's bundle is the same after a second call, because a table is
|
|
94
|
+
* emitted once for each folder it belongs in.
|
|
24
95
|
*/
|
|
25
|
-
export declare function
|
|
96
|
+
export declare function emitParagraphTables(this: Rollup.PluginContext, bundle: Rollup.OutputBundle, options?: ParagraphTableOptions): ParagraphTables;
|
package/lib/paragraphs.js
CHANGED
|
@@ -8,13 +8,28 @@
|
|
|
8
8
|
// device module's imports and emits the tables they name, in the folder the bundler put the module
|
|
9
9
|
// in, at the names the imports use. The tables then sit beside every module that imports them,
|
|
10
10
|
// whatever the project's assets folder and asset naming pattern are.
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
//
|
|
12
|
+
// Each table, and each module that imports one, takes a name that carries the hash of its final
|
|
13
|
+
// bytes. A host may then keep every file under a given name for a year, and a changed table or a
|
|
14
|
+
// changed module always has a new address. The table's content depends on the union of the names
|
|
15
|
+
// that its importers take, so the build works out every table before it names the modules.
|
|
16
|
+
import { createHash } from 'node:crypto';
|
|
17
|
+
import { closeSync, existsSync, openSync, readFileSync, readSync, statSync, writeSync, } from 'node:fs';
|
|
18
|
+
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from 'node:path';
|
|
19
|
+
import { narrowParagraphTable } from "./shader-list.js";
|
|
20
|
+
/** The prefix of a table's file name, before its kind. */
|
|
21
|
+
const TABLE_PREFIX = 'shader-paragraphs-';
|
|
13
22
|
/**
|
|
14
23
|
* An import of a table of shared paragraphs, at the top of a device module of its kind, with the
|
|
15
|
-
* names it takes from the table.
|
|
24
|
+
* names it takes from the table. A table that the build already named by its hash reads the same,
|
|
25
|
+
* so a module that the build reads again keeps its imports. The dot before the hash reads too, so
|
|
26
|
+
* that a file the build wrote before the dash form keeps its imports.
|
|
16
27
|
*/
|
|
17
|
-
const PARAGRAPH_IMPORT = /import\s*\{([^}]*)\}\s*from '\.\/(shader-paragraphs-[a-z0-9-]
|
|
28
|
+
const PARAGRAPH_IMPORT = /import\s*\{([^}]*)\}\s*from '\.\/(shader-paragraphs-[a-z0-9-]+?)(?:[.-][0-9a-f]{8})?\.js';/g;
|
|
29
|
+
/** The source of an import statement's relative address, for the rewrite below. */
|
|
30
|
+
const TABLE_SPECIFIER = /(from\s*'\.\/)(shader-paragraphs-[a-z0-9-]+?)(?:[.-][0-9a-f]{8})?\.js'/g;
|
|
31
|
+
/** A name that a table of shared paragraphs exports. */
|
|
32
|
+
const TABLE_EXPORT = /^export\s+(?:const|let|var|function|class)\s+([A-Za-z_$][\w$]*)/gm;
|
|
18
33
|
/**
|
|
19
34
|
* How much of a copied file this reads at first. The shader build writes the imports among the
|
|
20
35
|
* first lines, and a device module is hundreds of kilobytes, so the rest is never read. A device
|
|
@@ -54,10 +69,6 @@ function tableImports(read, length) {
|
|
|
54
69
|
window = Math.min(window * 2, length);
|
|
55
70
|
}
|
|
56
71
|
}
|
|
57
|
-
/** The names of the paragraph tables that a device module imports. */
|
|
58
|
-
function tableStems(read, length) {
|
|
59
|
-
return tableImports(read, length).map((entry) => entry.stem);
|
|
60
|
-
}
|
|
61
72
|
/**
|
|
62
73
|
* The tables that a device module on disk imports, with the names it takes from each. The file is
|
|
63
74
|
* read from its start only, so a build covers the whole generated set without holding any of it
|
|
@@ -78,13 +89,129 @@ export function tableImportsOfFile(file) {
|
|
|
78
89
|
closeSync(fd);
|
|
79
90
|
}
|
|
80
91
|
}
|
|
92
|
+
/** The hash that a file's final bytes give it: the first eight hexadecimal digits of SHA-256. */
|
|
93
|
+
export function contentHash(source) {
|
|
94
|
+
return createHash('sha256').update(source).digest('hex').slice(0, 8);
|
|
95
|
+
}
|
|
96
|
+
/** The kind of a table: its file name without the shared prefix and `.js`, such as `wgsl-morph`. */
|
|
97
|
+
function kindOf(path) {
|
|
98
|
+
return basename(path, '.js').slice(TABLE_PREFIX.length);
|
|
99
|
+
}
|
|
100
|
+
/** A table that lacks a name that its importers take. The caller names the modules. */
|
|
101
|
+
export class TableExportError extends Error {
|
|
102
|
+
path;
|
|
103
|
+
missing;
|
|
104
|
+
constructor(path, missing) {
|
|
105
|
+
super(`${basename(path)} does not export ${missing.join(', ')}`);
|
|
106
|
+
this.path = path;
|
|
107
|
+
this.missing = missing;
|
|
108
|
+
this.name = 'TableExportError';
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Works out each table's final content and name before any module that imports it takes a name. A
|
|
113
|
+
* build with a list of permutation words keeps only the paragraphs that the importing modules
|
|
114
|
+
* name, so a table's content follows the union of those names. A build without a list keeps the
|
|
115
|
+
* table whole. Throws a `TableExportError` when a table lacks a name that its importers take.
|
|
116
|
+
*/
|
|
117
|
+
export function planParagraphTables(tables, narrow) {
|
|
118
|
+
const plan = new Map();
|
|
119
|
+
for (const [path, names] of tables) {
|
|
120
|
+
const whole = readFileSync(path);
|
|
121
|
+
const text = narrow
|
|
122
|
+
? Buffer.from(narrowParagraphTable(whole.toString('utf8'), names, path))
|
|
123
|
+
: whole;
|
|
124
|
+
const exported = new Set([...text.toString('utf8').matchAll(TABLE_EXPORT)].map((match) => match[1]));
|
|
125
|
+
const missing = [...names].filter((name) => !exported.has(name)).sort();
|
|
126
|
+
if (missing.length > 0)
|
|
127
|
+
throw new TableExportError(path, missing);
|
|
128
|
+
plan.set(path, {
|
|
129
|
+
name: `${TABLE_PREFIX}${kindOf(path)}-${contentHash(text)}.js`,
|
|
130
|
+
source: Buffer.from(text),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return plan;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The text of a device module with each table import pointing at the table's content-addressed
|
|
137
|
+
* file. `names` maps each table's stem, such as `shader-paragraphs-wgsl`, to its final file name.
|
|
138
|
+
*/
|
|
139
|
+
export function rewriteTableImports(text, names) {
|
|
140
|
+
return text.replace(TABLE_SPECIFIER, (match, before, stem) => {
|
|
141
|
+
const name = names.get(stem);
|
|
142
|
+
return name === undefined ? match : `${before}${name}'`;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** How much of a module this copies at a time after its import head. */
|
|
146
|
+
const COPY_CHUNK = 1024 * 1024;
|
|
147
|
+
/**
|
|
148
|
+
* Reads a module's import head from its start, growing the window to the first blank line after
|
|
149
|
+
* the imports. Undefined for a file that names no import, which is no device module. The byte
|
|
150
|
+
* offset lets the caller copy the rest of the source without reading it into memory.
|
|
151
|
+
*/
|
|
152
|
+
function readHead(fd, length) {
|
|
153
|
+
let window = Math.min(IMPORT_WINDOW, length);
|
|
154
|
+
for (;;) {
|
|
155
|
+
const buffer = Buffer.alloc(window);
|
|
156
|
+
const count = readSync(fd, buffer, 0, window, 0);
|
|
157
|
+
const first = buffer.indexOf('import ', 0);
|
|
158
|
+
if (first < 0)
|
|
159
|
+
return undefined;
|
|
160
|
+
const end = buffer.indexOf('\n\n', first);
|
|
161
|
+
if (end >= 0) {
|
|
162
|
+
const stop = end + 2;
|
|
163
|
+
return { text: buffer.subarray(0, stop).toString('utf8'), end: stop };
|
|
164
|
+
}
|
|
165
|
+
if (window >= length)
|
|
166
|
+
return { text: buffer.subarray(0, count).toString('utf8'), end: count };
|
|
167
|
+
window = Math.min(window * 2, length);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Writes the module at `source` to `target` with its table imports pointing at their hashed files,
|
|
172
|
+
* and gives the hash of the bytes written. It reads only the import head, writes that rewritten,
|
|
173
|
+
* and copies the rest of the source in chunks, so a large module never sits in memory whole.
|
|
174
|
+
* Undefined for a file that names no import, which the caller copies as it is.
|
|
175
|
+
*/
|
|
176
|
+
export function rewriteCopyFile(source, target, names) {
|
|
177
|
+
const input = openSync(source, 'r');
|
|
178
|
+
let output;
|
|
179
|
+
try {
|
|
180
|
+
const length = statSync(source).size;
|
|
181
|
+
const head = readHead(input, length);
|
|
182
|
+
if (head === undefined)
|
|
183
|
+
return undefined;
|
|
184
|
+
const rewritten = Buffer.from(rewriteTableImports(head.text, names), 'utf8');
|
|
185
|
+
const hash = createHash('sha256').update(rewritten);
|
|
186
|
+
output = openSync(target, 'w');
|
|
187
|
+
writeSync(output, rewritten);
|
|
188
|
+
const chunk = Buffer.allocUnsafe(Math.min(COPY_CHUNK, Math.max(1, length - head.end)));
|
|
189
|
+
for (let position = head.end; position < length;) {
|
|
190
|
+
const count = readSync(input, chunk, 0, Math.min(chunk.length, length - position), position);
|
|
191
|
+
if (count <= 0)
|
|
192
|
+
break;
|
|
193
|
+
hash.update(chunk.subarray(0, count));
|
|
194
|
+
writeSync(output, chunk, 0, count);
|
|
195
|
+
position += count;
|
|
196
|
+
}
|
|
197
|
+
return hash.digest('hex').slice(0, 8);
|
|
198
|
+
}
|
|
199
|
+
finally {
|
|
200
|
+
if (output !== undefined)
|
|
201
|
+
closeSync(output);
|
|
202
|
+
closeSync(input);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
81
205
|
/**
|
|
82
206
|
* The file of the table that `file` imports, or undefined when the engine's package does not hold
|
|
83
207
|
* it: a table the build did not write, which would otherwise fail as a load in a browser and never
|
|
84
208
|
* in the build.
|
|
85
209
|
*/
|
|
86
|
-
function tablePath(file, stem, root) {
|
|
87
|
-
|
|
210
|
+
function tablePath(file, stem, root, workerFiles) {
|
|
211
|
+
const names = file.originalFileNames.length > 0
|
|
212
|
+
? file.originalFileNames
|
|
213
|
+
: (workerFiles?.get(file.fileName)?.moduleIds ?? []);
|
|
214
|
+
for (const name of names) {
|
|
88
215
|
// A build names a copied file as the page's root sees it, relative or absolute; the table
|
|
89
216
|
// sits beside the file it came from.
|
|
90
217
|
const folder = isAbsolute(name) ? dirname(name) : resolve(root, dirname(name));
|
|
@@ -94,52 +221,168 @@ function tablePath(file, stem, root) {
|
|
|
94
221
|
}
|
|
95
222
|
return undefined;
|
|
96
223
|
}
|
|
224
|
+
/** A source path as a message names it: from the project's root when it is inside it. */
|
|
225
|
+
function displayPath(root, name) {
|
|
226
|
+
const path = isAbsolute(name) ? relative(root, name) : name;
|
|
227
|
+
return path.split(sep).join('/');
|
|
228
|
+
}
|
|
229
|
+
/** A bundle asset's original name without its extension, or its file name when it has none. */
|
|
230
|
+
function assetStem(file) {
|
|
231
|
+
const name = file.name?.length ? file.name : file.fileName;
|
|
232
|
+
return basename(name).replace(/\.[cm]?js$/, '');
|
|
233
|
+
}
|
|
234
|
+
/** Replaces every mention of a renamed file in the bundle's scripts and text assets. */
|
|
235
|
+
function renameReferences(bundle, from, to) {
|
|
236
|
+
for (const file of Object.values(bundle)) {
|
|
237
|
+
if (file.fileName === to)
|
|
238
|
+
continue;
|
|
239
|
+
if (file.type === 'chunk') {
|
|
240
|
+
if (file.code.includes(from))
|
|
241
|
+
file.code = file.code.replaceAll(from, to);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (!/\.(?:[cm]?js|html|css|json)$/.test(file.fileName))
|
|
245
|
+
continue;
|
|
246
|
+
const text = typeof file.source === 'string' ? file.source : new TextDecoder().decode(file.source);
|
|
247
|
+
if (!text.includes(from))
|
|
248
|
+
continue;
|
|
249
|
+
file.source = text.replaceAll(from, to);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
97
252
|
/**
|
|
98
|
-
* Emits each shared table of shader paragraphs beside the
|
|
99
|
-
*
|
|
100
|
-
*
|
|
253
|
+
* Emits each shared table of shader paragraphs beside the modules that import it, gives the files
|
|
254
|
+
* of the tables that each bundle file names, and names each bundle asset that imports one by the
|
|
255
|
+
* hash of its final bytes. A build's bundle is the same after a second call, because a table is
|
|
256
|
+
* emitted once for each folder it belongs in.
|
|
101
257
|
*/
|
|
102
|
-
export function emitParagraphTables(bundle,
|
|
103
|
-
const
|
|
104
|
-
|
|
258
|
+
export function emitParagraphTables(bundle, options = {}) {
|
|
259
|
+
const { root = process.cwd(), narrow = false, workerFiles, copies = [] } = options;
|
|
260
|
+
/** The names that the importers take from each table, by the table's source file. */
|
|
261
|
+
const names = new Map();
|
|
262
|
+
/** The output folders that hold a module that names each table. */
|
|
263
|
+
const folders = new Map();
|
|
264
|
+
const add = (path, imported, folder) => {
|
|
265
|
+
let table = names.get(path);
|
|
266
|
+
if (table === undefined) {
|
|
267
|
+
table = new Set();
|
|
268
|
+
names.set(path, table);
|
|
269
|
+
}
|
|
270
|
+
for (const name of imported)
|
|
271
|
+
table.add(name);
|
|
272
|
+
let where = folders.get(path);
|
|
273
|
+
if (where === undefined) {
|
|
274
|
+
where = new Set();
|
|
275
|
+
folders.set(path, where);
|
|
276
|
+
}
|
|
277
|
+
where.add(folder);
|
|
278
|
+
};
|
|
279
|
+
/** The tables that each bundle asset names, with its source name and the table files. */
|
|
280
|
+
const assets = new Map();
|
|
105
281
|
for (const file of Object.values(bundle)) {
|
|
106
282
|
if (file.type !== 'asset')
|
|
107
283
|
continue;
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
];
|
|
111
|
-
if (stems.length === 0)
|
|
284
|
+
const imports = tableImports((bytes) => headOf(file.source, bytes), file.source.length);
|
|
285
|
+
if (imports.length === 0)
|
|
112
286
|
continue;
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const stem = posix.basename(table, '.js');
|
|
119
|
-
const path = tablePath(file, stem, root);
|
|
287
|
+
const source = file.originalFileNames[0] ?? workerFiles?.get(file.fileName)?.moduleIds[0] ?? file.fileName;
|
|
288
|
+
const module = displayPath(root, source);
|
|
289
|
+
const paths = [];
|
|
290
|
+
for (const { stem, names: imported } of imports) {
|
|
291
|
+
const path = tablePath(file, stem, root, workerFiles);
|
|
120
292
|
if (!path)
|
|
121
|
-
|
|
122
|
-
|
|
293
|
+
this.error(`null3D: ${module} imports ${stem}.js, which the engine's shader modules do not hold. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
294
|
+
paths.push(path);
|
|
295
|
+
add(path, imported, posix.dirname(file.fileName));
|
|
296
|
+
}
|
|
297
|
+
assets.set(file, { module, paths });
|
|
298
|
+
}
|
|
299
|
+
// Each copy's head is read once and its names join the union, which holds each name once.
|
|
300
|
+
for (const copy of copies) {
|
|
301
|
+
for (const { stem, names: imported } of tableImportsOfFile(copy.file)) {
|
|
302
|
+
const path = join(dirname(copy.source), `${stem}.js`);
|
|
303
|
+
if (!existsSync(path))
|
|
304
|
+
this.error(`null3D: ${copy.module} imports ${stem}.js, which the engine's shader modules do not hold. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
305
|
+
add(path, imported, copy.folder);
|
|
123
306
|
}
|
|
124
|
-
tables.set(file.fileName, own);
|
|
125
307
|
}
|
|
126
|
-
|
|
308
|
+
let plan;
|
|
309
|
+
try {
|
|
310
|
+
plan = planParagraphTables(names, narrow);
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
if (!(error instanceof TableExportError))
|
|
314
|
+
throw error;
|
|
315
|
+
this.error(missingExportMessage(error, copies, assets));
|
|
316
|
+
}
|
|
317
|
+
/** The final name of the table that a module's source file names. */
|
|
318
|
+
const nameOf = (path) => {
|
|
319
|
+
const table = plan.get(path);
|
|
320
|
+
if (table === undefined)
|
|
321
|
+
throw new Error(`null3D: the build planned no table for ${basename(path)}.`);
|
|
322
|
+
return table.name;
|
|
323
|
+
};
|
|
324
|
+
// Each table keeps the folder of every module that names it, so a relative import resolves.
|
|
325
|
+
const tables = new Map();
|
|
326
|
+
for (const [path, where] of folders) {
|
|
327
|
+
const table = plan.get(path);
|
|
328
|
+
if (table === undefined)
|
|
329
|
+
continue;
|
|
330
|
+
for (const folder of where) {
|
|
331
|
+
const output = posix.join(folder, table.name);
|
|
332
|
+
if (tables.has(output))
|
|
333
|
+
continue;
|
|
334
|
+
tables.set(output, table);
|
|
335
|
+
this.emitFile({ type: 'asset', fileName: output, source: table.source });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const files = new Map();
|
|
339
|
+
const renamed = new Map();
|
|
340
|
+
const renames = [];
|
|
341
|
+
for (const [file, { paths }] of assets) {
|
|
342
|
+
const names = new Map(paths.map((path) => [basename(path, '.js'), nameOf(path)]));
|
|
343
|
+
const text = typeof file.source === 'string' ? file.source : new TextDecoder().decode(file.source);
|
|
344
|
+
const rewritten = rewriteTableImports(text, names);
|
|
345
|
+
const folder = posix.dirname(file.fileName);
|
|
346
|
+
const loads = paths.map((path) => posix.join(folder, nameOf(path)));
|
|
347
|
+
const source = Buffer.isBuffer(file.source) ? file.source : Buffer.from(file.source);
|
|
348
|
+
const after = Buffer.from(rewritten);
|
|
349
|
+
if (after.equals(source)) {
|
|
350
|
+
files.set(file.fileName, loads);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const to = posix.join(folder, `${assetStem(file)}-${contentHash(after)}.js`);
|
|
354
|
+
files.set(to, loads);
|
|
355
|
+
if (bundle[to] !== undefined)
|
|
356
|
+
this.error(`null3D: two shader files would share the name ${to}. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
357
|
+
renames.push({ from: file.fileName, to, file: { ...file, fileName: to, source: after } });
|
|
358
|
+
}
|
|
359
|
+
for (const { from, to, file } of renames) {
|
|
360
|
+
delete bundle[from];
|
|
361
|
+
bundle[to] = file;
|
|
362
|
+
renamed.set(from, to);
|
|
363
|
+
renameReferences(bundle, from, to);
|
|
364
|
+
}
|
|
365
|
+
return { files, plan, tables, renamed };
|
|
127
366
|
}
|
|
128
367
|
/**
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* through `emitParagraphTables`, and no build emits a table twice.
|
|
368
|
+
* The build error for a table that lacks a name. It reads the modules again, because only a failed
|
|
369
|
+
* build pays for naming them, and a build that keeps a module name for every imported name holds
|
|
370
|
+
* too much memory.
|
|
133
371
|
*/
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
372
|
+
function missingExportMessage(error, copies, assets) {
|
|
373
|
+
const missing = new Set(error.missing);
|
|
374
|
+
const modules = new Set();
|
|
375
|
+
for (const copy of copies) {
|
|
376
|
+
const named = tableImportsOfFile(copy.file).some((entry) => join(dirname(copy.source), `${entry.stem}.js`) === error.path &&
|
|
377
|
+
entry.names.some((name) => missing.has(name)));
|
|
378
|
+
if (named)
|
|
379
|
+
modules.add(copy.module);
|
|
380
|
+
}
|
|
381
|
+
for (const [file, { module, paths }] of assets) {
|
|
382
|
+
const imports = tableImports((bytes) => headOf(file.source, bytes), file.source.length);
|
|
383
|
+
const named = imports.some((entry, index) => paths[index] === error.path && entry.names.some((name) => missing.has(name)));
|
|
384
|
+
if (named)
|
|
385
|
+
modules.add(module);
|
|
386
|
+
}
|
|
387
|
+
return `null3D: ${[...modules].join(', ') || 'a shader module'} imports ${error.missing.join(', ')} from ${basename(error.path)}, and that table does not export ${error.missing.length === 1 ? 'it' : 'them'}. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`;
|
|
145
388
|
}
|
package/lib/shader-list.js
CHANGED
|
@@ -162,7 +162,7 @@ export class UnlistedWords {
|
|
|
162
162
|
this.warned.add(word >>> 0);
|
|
163
163
|
const text = wordText(word);
|
|
164
164
|
return {
|
|
165
|
-
message: `${page} draws with the permutation word ${text}, which the shader list ${this.name} lacks. The dev server draws it, but a production build with this list
|
|
165
|
+
message: `${page} draws with the permutation word ${text}, which the shader list ${this.name} lacks. The dev server draws it, but a production build with this list skips the pipeline or pass that asks for the word and reports it. Record the page again with bunx @null3d/cli shaders record, add the word by hand with bunx @null3d/cli shaders record --extra ${text}, or check the build with bunx @null3d/cli shaders verify.`,
|
|
166
166
|
first,
|
|
167
167
|
};
|
|
168
168
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@null3d/vite-plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Vite plugin for null3D projects: cross-origin isolation headers, HTTPS for testing on phones and tablets, WGSL compiled for WebGPU and WebGL2, glTF models optimized on import, and production builds that compile the sketch module and ship the engine core.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"author": "Ramesh Nair (https://hiddentao.com)",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"prepack": "bun ../../tools/build-package.ts vite-plugin"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@null3d/cli": "0.1.
|
|
55
|
+
"@null3d/cli": "0.1.3",
|
|
56
56
|
"vite": ">=8"
|
|
57
57
|
},
|
|
58
58
|
"peerDependenciesMeta": {
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
}
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@null3d/cli": "0.1.
|
|
64
|
+
"@null3d/cli": "0.1.3",
|
|
65
65
|
"vite": "8.3.1"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|