@vitessce/neuroglancer-workers 4.1.2 → 4.2.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 +21 -8
- package/dist/index.js +15 -36
- package/package.json +5 -10
- package/scripts/build.mjs +194 -0
- package/dist/index.js.map +0 -1
- package/src/index.js +0 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitessce/neuroglancer-workers",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"author": "HIDIVE Lab at HMS",
|
|
5
5
|
"homepage": "http://vitessce.io",
|
|
6
6
|
"repository": {
|
|
@@ -17,21 +17,16 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"
|
|
20
|
+
"scripts",
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
|
-
"dependencies": {
|
|
24
|
-
"@janelia-flyem/neuroglancer": "2.37.5"
|
|
25
|
-
},
|
|
26
23
|
"devDependencies": {
|
|
27
24
|
"@rollup/plugin-commonjs": "^23.0.0",
|
|
28
25
|
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
29
|
-
"
|
|
30
|
-
"rollup": "^2.0.0"
|
|
31
|
-
"rollup-plugin-web-worker-loader": "^1.0.0"
|
|
26
|
+
"neuroglancer": "2.41.2",
|
|
27
|
+
"rollup": "^2.0.0"
|
|
32
28
|
},
|
|
33
29
|
"scripts": {
|
|
34
|
-
"build": "
|
|
35
|
-
"build-lib": "rollup --config ../workers/rollup.config.js"
|
|
30
|
+
"build": "node scripts/build.mjs"
|
|
36
31
|
}
|
|
37
32
|
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// Builds self-contained, inlined Neuroglancer worker scripts.
|
|
2
|
+
//
|
|
3
|
+
// Mainline Neuroglancer creates its workers with
|
|
4
|
+
// new Worker(new URL('./chunk_worker.bundle.js', import.meta.url), { type: 'module' })
|
|
5
|
+
// and loads WebAssembly decoders with
|
|
6
|
+
// fetch(new URL('./x.wasm', import.meta.url))
|
|
7
|
+
// which requires every consumer's bundler to emit those files next to the
|
|
8
|
+
// bundle (and breaks for cross-origin/CDN usage, e.g. vitessce-python).
|
|
9
|
+
//
|
|
10
|
+
// Instead, we bundle each worker entry point (plus all of its dependencies,
|
|
11
|
+
// WASM included) into a single classic-worker IIFE string, and export a
|
|
12
|
+
// factory that starts the worker from a Blob URL. The async-computation
|
|
13
|
+
// worker is spawned from inside the chunk worker (a nested worker), so its
|
|
14
|
+
// code is embedded in the chunk worker bundle.
|
|
15
|
+
//
|
|
16
|
+
// Output: dist/index.js exporting `ChunkWorker`.
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
readFileSync, writeFileSync, mkdirSync, rmSync, realpathSync,
|
|
20
|
+
} from 'node:fs';
|
|
21
|
+
import { dirname, resolve, join } from 'node:path';
|
|
22
|
+
import { fileURLToPath } from 'node:url';
|
|
23
|
+
import { rollup } from 'rollup';
|
|
24
|
+
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|
25
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
26
|
+
|
|
27
|
+
const pkgDir = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
28
|
+
// neuroglancer's "exports" does not expose ./package.json, so locate it directly.
|
|
29
|
+
const ngDir = realpathSync(join(pkgDir, 'node_modules', 'neuroglancer'));
|
|
30
|
+
const ngPkg = JSON.parse(readFileSync(join(ngDir, 'package.json'), 'utf8'));
|
|
31
|
+
|
|
32
|
+
const ASYNC_WORKER_VIRTUAL_ID = '\0vitessce-ng-async-worker';
|
|
33
|
+
|
|
34
|
+
// Resolve Neuroglancer's package-internal subpath imports ("#src/...", "#datasource/...").
|
|
35
|
+
// Picks the "default" condition, i.e. all datasources/kvstores enabled.
|
|
36
|
+
function resolveSubpathImport(specifier) {
|
|
37
|
+
const { imports } = ngPkg;
|
|
38
|
+
const pick = (target) => {
|
|
39
|
+
if (typeof target === 'string') return target;
|
|
40
|
+
if (target && typeof target === 'object') return pick(target.default);
|
|
41
|
+
return undefined;
|
|
42
|
+
};
|
|
43
|
+
if (specifier in imports) {
|
|
44
|
+
const t = pick(imports[specifier]);
|
|
45
|
+
return t && join(ngDir, t);
|
|
46
|
+
}
|
|
47
|
+
// Longest wildcard pattern match, e.g. "#src/*.js" -> "./lib/*.js".
|
|
48
|
+
const patterns = Object.keys(imports)
|
|
49
|
+
.filter(k => k.includes('*'))
|
|
50
|
+
.sort((a, b) => b.length - a.length);
|
|
51
|
+
for (const key of patterns) {
|
|
52
|
+
const [prefix, suffix] = key.split('*');
|
|
53
|
+
if (specifier.startsWith(prefix) && specifier.endsWith(suffix)
|
|
54
|
+
&& specifier.length >= prefix.length + suffix.length) {
|
|
55
|
+
const middle = specifier.slice(prefix.length, specifier.length - suffix.length);
|
|
56
|
+
const t = pick(imports[key]);
|
|
57
|
+
return t && join(ngDir, t.replace('*', middle));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function neuroglancerSubpathImports() {
|
|
64
|
+
return {
|
|
65
|
+
name: 'neuroglancer-subpath-imports',
|
|
66
|
+
resolveId(source) {
|
|
67
|
+
if (!source.startsWith('#')) return null;
|
|
68
|
+
const resolved = resolveSubpathImport(source);
|
|
69
|
+
if (!resolved) throw new Error(`Unresolved Neuroglancer subpath import: ${source}`);
|
|
70
|
+
return resolved;
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Workers never render DOM; drop CSS and raw-SVG imports if any are reachable.
|
|
76
|
+
function ignoreAssets() {
|
|
77
|
+
return {
|
|
78
|
+
name: 'ignore-assets',
|
|
79
|
+
resolveId(source) {
|
|
80
|
+
if (source.endsWith('.css') || /\.svg\?raw$/.test(source)) return `\0empty:${source}`;
|
|
81
|
+
return null;
|
|
82
|
+
},
|
|
83
|
+
load(id) {
|
|
84
|
+
if (id.startsWith('\0empty:')) return id.endsWith('.css') ? '' : 'export default "";';
|
|
85
|
+
return null;
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// fetch(new URL('./x.wasm', import.meta.url)) -> fetch('data:application/wasm;base64,...')
|
|
91
|
+
function inlineWasmUrls() {
|
|
92
|
+
const re = /new URL\(\s*(["'])([^"']+\.wasm)\1\s*,\s*import\.meta\.url\s*\)/g;
|
|
93
|
+
return {
|
|
94
|
+
name: 'inline-wasm-urls',
|
|
95
|
+
transform(code, id) {
|
|
96
|
+
if (!code.includes('.wasm') || !code.includes('import.meta.url')) return null;
|
|
97
|
+
let changed = false;
|
|
98
|
+
const out = code.replace(re, (_m, _q, rel) => {
|
|
99
|
+
const wasmPath = resolve(dirname(id), rel);
|
|
100
|
+
const b64 = readFileSync(wasmPath).toString('base64');
|
|
101
|
+
changed = true;
|
|
102
|
+
return JSON.stringify(`data:application/wasm;base64,${b64}`);
|
|
103
|
+
});
|
|
104
|
+
return changed ? { code: out, map: null } : null;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Replace the nested async-computation worker construction in
|
|
110
|
+
// async_computation/request.js with a Blob-URL worker from the inlined code.
|
|
111
|
+
function inlineAsyncComputationWorker(asyncWorkerCode) {
|
|
112
|
+
const re = /new Worker\(\s*(?:\/\*[^*]*\*\/\s*)?new URL\(\s*["']\.\.\/async_computation\.bundle\.js["']\s*,\s*import\.meta\.url\s*\)\s*,\s*\{\s*type:\s*["']module["']\s*\}\s*\)/;
|
|
113
|
+
return {
|
|
114
|
+
name: 'inline-async-computation-worker',
|
|
115
|
+
resolveId(source) {
|
|
116
|
+
return source === ASYNC_WORKER_VIRTUAL_ID ? source : null;
|
|
117
|
+
},
|
|
118
|
+
load(id) {
|
|
119
|
+
if (id !== ASYNC_WORKER_VIRTUAL_ID) return null;
|
|
120
|
+
return `
|
|
121
|
+
const code = ${JSON.stringify(asyncWorkerCode)};
|
|
122
|
+
let url;
|
|
123
|
+
export function createAsyncComputationWorker() {
|
|
124
|
+
if (!url) url = URL.createObjectURL(new Blob([code], { type: 'text/javascript' }));
|
|
125
|
+
return new Worker(url);
|
|
126
|
+
}`;
|
|
127
|
+
},
|
|
128
|
+
transform(code, id) {
|
|
129
|
+
if (!id.endsWith(join('async_computation', 'request.js'))) return null;
|
|
130
|
+
if (!re.test(code)) {
|
|
131
|
+
throw new Error('Could not find the async computation worker construction in request.js; Neuroglancer changed?');
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
code: `import { createAsyncComputationWorker } from ${JSON.stringify(ASYNC_WORKER_VIRTUAL_ID)};\n${code.replace(re, 'createAsyncComputationWorker()')}`,
|
|
135
|
+
map: null,
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function bundleWorker(entry, extraPlugins = []) {
|
|
142
|
+
const bundle = await rollup({
|
|
143
|
+
input: join(ngDir, 'lib', entry),
|
|
144
|
+
plugins: [
|
|
145
|
+
...extraPlugins,
|
|
146
|
+
neuroglancerSubpathImports(),
|
|
147
|
+
ignoreAssets(),
|
|
148
|
+
inlineWasmUrls(),
|
|
149
|
+
nodeResolve({ browser: true, exportConditions: ['browser', 'import', 'default'] }),
|
|
150
|
+
commonjs(),
|
|
151
|
+
],
|
|
152
|
+
onwarn(warning, warn) {
|
|
153
|
+
if (warning.code === 'CIRCULAR_DEPENDENCY' || warning.code === 'THIS_IS_UNDEFINED') return;
|
|
154
|
+
warn(warning);
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
const { output } = await bundle.generate({ format: 'iife', inlineDynamicImports: true });
|
|
158
|
+
await bundle.close();
|
|
159
|
+
const { code } = output[0];
|
|
160
|
+
if (code.includes('import.meta')) {
|
|
161
|
+
throw new Error(`${entry}: bundled worker still references import.meta; something was not inlined.`);
|
|
162
|
+
}
|
|
163
|
+
return code;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const asyncWorkerCode = await bundleWorker('async_computation.bundle.js');
|
|
167
|
+
const chunkWorkerCode = await bundleWorker(
|
|
168
|
+
'chunk_worker.bundle.js',
|
|
169
|
+
[inlineAsyncComputationWorker(asyncWorkerCode)],
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const distDir = join(pkgDir, 'dist');
|
|
173
|
+
rmSync(distDir, { recursive: true, force: true });
|
|
174
|
+
mkdirSync(distDir, { recursive: true });
|
|
175
|
+
writeFileSync(join(distDir, 'index.js'), `// Generated by scripts/build.mjs from neuroglancer@${ngPkg.version}. Do not edit.
|
|
176
|
+
const chunkWorkerCode = ${JSON.stringify(chunkWorkerCode)};
|
|
177
|
+
let chunkWorkerUrl;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Start Neuroglancer's chunk worker from inlined code.
|
|
181
|
+
* Usable with or without \`new\`; returns a Worker.
|
|
182
|
+
* The chunk worker spawns its own async-computation workers (nested),
|
|
183
|
+
* which are terminated along with it.
|
|
184
|
+
* @returns {Worker}
|
|
185
|
+
*/
|
|
186
|
+
export function ChunkWorker() {
|
|
187
|
+
if (!chunkWorkerUrl) {
|
|
188
|
+
chunkWorkerUrl = URL.createObjectURL(new Blob([chunkWorkerCode], { type: 'text/javascript' }));
|
|
189
|
+
}
|
|
190
|
+
return new Worker(chunkWorkerUrl);
|
|
191
|
+
}
|
|
192
|
+
`);
|
|
193
|
+
const kb = n => `${(n / 1024).toFixed(0)} KiB`;
|
|
194
|
+
console.log(`neuroglancer@${ngPkg.version}: async worker ${kb(asyncWorkerCode.length)}, chunk worker ${kb(chunkWorkerCode.length)}`);
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/src/index.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import/no-unresolved */
|
|
2
|
-
/* eslint-disable max-len */
|
|
3
|
-
export { default as ChunkWorker } from 'web-worker:@janelia-flyem/neuroglancer/dist/module/chunk_worker.bundle.js';
|
|
4
|
-
|
|
5
|
-
// AsyncComputationWorker supports the loading of meshes with sharded format
|
|
6
|
-
export { default as AsyncComputationWorker } from 'web-worker:@janelia-flyem/neuroglancer/dist/module/async_computation.bundle.js';
|