mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 18:34:02 +00:00
fix(data): keep node:fs out of the browser-built data modules (#83)
naturalEarthRegions.js and neighborhoodPolygons.js each carried an isNode branch that dynamically imported node:fs to read their bundled JSON packs, because a plain dynamic JSON import needs an import attribute in Node. Vite only warns about externalizing node:fs for the browser, so both warnings survived every production build and the runtime boundary rested on an environment guard. Give both loaders the import attribute instead. Vite bundles the JSON as a module exactly as before (the emitted regions/marine/san-francisco chunks are byte-identical), Node loads the same files under node:test, and the isNode split disappears. Add a source-boundary test so a node: import cannot silently return to a browser-built module. Closes #34
This commit is contained in:
1 parent
ac927de71a
commit
6d83bb6008
4 files changed
+49
-27
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync, readdirSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const SRC_ROOT = fileURLToPath(new URL('.', import.meta.url));
|
||||
|
||||
/** Every browser-built module under src/ — the test files are Node-only. */
|
||||
function browserModules(directory = SRC_ROOT) {
|
||||
const files = [];
|
||||
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
||||
const absolute = path.join(directory, entry.name);
|
||||
if (entry.isDirectory()) files.push(...browserModules(absolute));
|
||||
else if (entry.isFile() && entry.name.endsWith('.js')) files.push(absolute);
|
||||
}
|
||||
return files.sort();
|
||||
}
|
||||
|
||||
test('no browser-built module imports a Node core module', () => {
|
||||
// Vite externalizes `node:*` for the browser and only WARNS, so a stray
|
||||
// import survives the build and turns into a runtime failure the moment the
|
||||
// guard around it is wrong. src/data/naturalEarthRegions.js and
|
||||
// src/data/neighborhoodPolygons.js both carried one to read their bundled
|
||||
// JSON packs under node:test; an import attribute serves both runtimes.
|
||||
const offenders = [];
|
||||
for (const file of browserModules()) {
|
||||
const source = readFileSync(file, 'utf8');
|
||||
// Static `from 'node:fs'` and dynamic `import('node:fs')`, quoted either way.
|
||||
if (/\bfrom\s*['"]node:|\bimport\s*\(\s*(?:\/\*[^*]*\*\/\s*)?['"]node:/.test(source)) {
|
||||
offenders.push(path.relative(SRC_ROOT, file).split(path.sep).join('/'));
|
||||
}
|
||||
}
|
||||
|
||||
assert.deepEqual(offenders, [], `Node core imports reached the browser build: ${offenders.join(', ')}`);
|
||||
});
|
||||
Reference in new issue
Block a user