@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
86 lines (70 loc) • 3.33 kB
JavaScript
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
import { tryGetNeedleEngineVersion } from '../common/version.js';
import { tryGetGenerator } from '../common/generator.js';
import { getMeta } from '../common/config.cjs';
import { alias } from './alias.cjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/** Pass a nextConfig object in to add the needle specific settings.
* Optionally omit nextConfig and it will be created for you.
* @param {import('next').NextConfig} nextConfig
* @param {import('../types').userSettings} userSettings
* @returns {import('next').NextConfig}
*/
export const needleNext = (nextConfig, userSettings) => {
console.log("Apply 🌵 needle next config");
if (!nextConfig)
nextConfig = {
reactStrictMode: true,
};
// add transpile packages
if (!nextConfig.transpilePackages) {
nextConfig.transpilePackages = [];
}
nextConfig.transpilePackages.push("three", "peerjs", "three-mesh-ui");
// add webpack config
if (!nextConfig.webpack) nextConfig.webpack = nextWebPack;
else {
const webpackFn = nextConfig.webpack;
nextConfig.webpack = (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
nextWebPack(config, { buildId, dev, isServer, defaultLoaders, webpack });
return webpackFn(config, { buildId, dev, isServer, defaultLoaders, webpack });
}
}
/** @param {import ('next').NextConfig config } */
function nextWebPack(config, { buildId, dev, isServer, defaultLoaders, webpack }) {
// TODO: get public identifier key from license server
const meta = getMeta();
let useRapier = true;
if (userSettings.useRapier === false) useRapier = false;
else if (meta.useRapier === false) useRapier = false;
// add defines
const webpackModule = userSettings.modules?.webpack;
const definePlugin = webpackModule && new webpackModule.DefinePlugin({
NEEDLE_ENGINE_VERSION: JSON.stringify(tryGetNeedleEngineVersion() ?? "0.0.0"),
NEEDLE_ENGINE_GENERATOR: JSON.stringify(tryGetGenerator() ?? "unknown"),
NEEDLE_USE_RAPIER: JSON.stringify(useRapier),
// TODO globalThis is not solved via DefinePlugin
parcelRequire: undefined,
});
if (!definePlugin) console.log("WARN: no define plugin provided. Did you miss adding the webpack module to the next config? You can pass it to the Needle plugins via `nextConfig.modules = { webpack };`");
else
config.plugins.push(definePlugin);
if (!config.module) config.module = {};
if (!config.module.rules) config.module.rules = [];
// add license plugin
config.module.rules.push({
test: /engine_license\.(ts|js)$/,
loader: resolve(__dirname, 'license.cjs')
});
// add mesh bvh worker transform
config.module.rules.push({
test: /generateMeshBVH.worker\.js$/,
loader: resolve(__dirname, 'meshbvhworker.cjs')
});
alias(config);
return config;
}
return nextConfig;
};