import debug from "~/server/debug"; const d = debug(false)!; /** * Dynamically require some module. This can be useful to import a module which is initialized in the server entrypoint * and you want to import and access it from remix routes. Otherwise, with static imports, Remix would bundle all modules * and local variables of module won't be shared * * One caveat is that we cannot use this in routes directly, otherwise the route will not be defined correctly */ const requireCommon = (moduleName: string) => { let module; if (__dirname.endsWith("/dist")) { // This is /dist/index.js that imports something under app directory. // Remix (which uses rollup?) keeps require() as it is in index.js and places the module to under dist/app/..., so we point to the built module here. module = moduleName.replace("~/", "./app/"); d( `import ${module} from server bundle at ${__dirname} (originally requiring ${moduleName})` ); } else if (__dirname.endsWith("/sockets")) { // Imported from other files under sockets module = moduleName.replace("~/sockets/", "./"); d( `import ${module} from sockets at ${__dirname} (originally requiring ${moduleName})` ); } else { // Imported from Remix bundle module = moduleName.replace("~/", "../dist/app/"); d( `import ${module} from remix bundle at ${__dirname} (originally requiring ${moduleName})` ); } return require(module); }; export default requireCommon;