@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.
62 lines (54 loc) • 2.82 kB
JavaScript
import { builtAssetsDirectory, tryLoadProjectConfig } from './config.js';
/** Modify the glb load path in codegen files.
* This is necessary if the assets directory is not the default (changed by the user in needle.config.json).
* @param {"build" | "serve"} command
* @param {import('../types/needleConfig').needleMeta | null | undefined} config
* @param {import('../types').userSettings} userSettings
* @returns {import('vite').Plugin | undefined}
*/
export function needleTransformCodegen(command, config, userSettings) {
if (config?.noCodegenTransform === true || userSettings?.noCodegenTransform === true) {
return;
}
let codegenDirectory = "src/generated";
const needleConfig = tryLoadProjectConfig();
if (needleConfig?.codegenDirectory?.length)
codegenDirectory = needleConfig.codegenDirectory;
let configuredAssetsDirectory = "assets";
if (needleConfig?.assetsDirectory?.length)
configuredAssetsDirectory = needleConfig.assetsDirectory;
// https://regex101.com/r/Y05z9P/1
// const matchCodegenFilePaths = /\"(.+)\/.+?\.(glb|gltf)/g;
return [
{
name: 'needle-transform-codegen-files',
apply: 'build',
transform(src, id) {
if (id.endsWith(codegenDirectory + "/gen.js")) {
let assetsDir = builtAssetsDirectory();
if (assetsDir !== configuredAssetsDirectory) {
// check if the the assets directory is expected to be relative to the root
// like for example "/assets"
// then we want to add the leading dash also to the new path
// if (configuredAssetsDirectory.startsWith("/")) assetsDir = "/" + assetsDir;
// // are they now the same? if so well we dont have to do anything
// if (assetsDir === configuredAssetsDirectory) {
// return;
// }
console.log(`[needle-transform-files] - Transform codegen paths \"${configuredAssetsDirectory}\" → \"${assetsDir}\"`)
// replace codegen paths, but only in non-import lines
// (import lines use filesystem-relative paths for bundler awareness)
src = src.split('\n').map(line => {
if (line.trimStart().startsWith('import ')) return line;
return line.replaceAll(configuredAssetsDirectory, assetsDir);
}).join('\n');
return {
code: src,
map: null
}
}
}
}
}
];
}