@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.
92 lines (81 loc) • 3.41 kB
JavaScript
import { resolveLicense } from '../common/license.js';
import { loadConfig } from './config.js';
/** This plugin is used to apply the license to the needle engine.
* @param {"build" | "serve"} command
* @param {import('../types/needleConfig').needleMeta | null | undefined} config
* @param {import('../types/userconfig.js').userSettings} userSettings
* @returns {import('vite').Plugin}
*/
export function needleLicense(command, config, userSettings) {
/** @type {import('../common/license.js').LicenseResult | null | undefined} */
let licenseResult = undefined;
let appliedLicense = false;
return {
name: "needle:license",
enforce: 'pre',
async configResolved() {
let team = userSettings?.license?.team;
if (!team) {
const meta = await loadConfig(null);
if (meta) {
team = meta.license?.team;
}
}
licenseResult = await resolveLicense({
team: team,
accessToken: userSettings?.license?.accessToken,
loglevel: userSettings?.debugLicense === true ? "verbose" : undefined
});
},
async transform(src, id) {
// Vite 4 and 8 handling:
const isNeedleEngineFile = id.includes("engine/engine_license")
|| id.includes("needle-tools_engine")
|| id.includes("@needle-tools")
|| id.includes("needle-engine");
// sometimes the actual license parameter is in a unnamed chunk file
const isViteChunkFile = id.includes("chunk") && id.includes(".vite");
if (isNeedleEngineFile || isViteChunkFile) {
if (!licenseResult) {
return;
}
let modified = false;
// Replace license type
const index = src.indexOf("EzdGPQg");
if (index >= 0) {
const end = src.indexOf(";", index);
if (end >= 0) {
const line = src.substring(index, end);
const replaced = "EzdGPQg = \"" + licenseResult.type + "\"";
src = src.replace(line, replaced);
modified = true;
}
}
// Replace license JWT (same pattern)
if (licenseResult.jwt) {
const jwtIndex = src.indexOf("_$InwX");
if (jwtIndex >= 0) {
const jwtEnd = src.indexOf(";", jwtIndex);
if (jwtEnd >= 0) {
const jwtLine = src.substring(jwtIndex, jwtEnd);
const jwtReplaced = "_$InwX = \"" + licenseResult.jwt + "\"";
src = src.replace(jwtLine, jwtReplaced);
modified = true;
}
}
}
if (modified) {
appliedLicense = true;
return { code: src, map: null }
}
}
},
buildEnd() {
if (!appliedLicense) {
if (process.env.NEEDLE_TEST_ENV) {
console.error("ERR: License was not applied!");
}
}
}
}
}