-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathremix-config.ts
62 lines (54 loc) · 1.76 KB
/
remix-config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
joinPathFragments,
readProjectConfiguration,
Tree,
workspaceRoot,
} from '@nx/devkit';
import type { AppConfig } from '@remix-run/dev';
import { createContext, SourceTextModule } from 'vm';
export function getRemixConfigPath(tree: Tree, projectName: string) {
const project = readProjectConfiguration(tree, projectName);
if (!project) throw new Error(`Project does not exist: ${projectName}`);
for (const ext of ['.mjs', '.cjs', '.js']) {
const configPath = joinPathFragments(project.root, `remix.config${ext}`);
if (tree.exists(configPath)) {
return configPath;
}
}
}
export function getRemixConfigPathFromProjectRoot(
tree: Tree,
projectRoot: string
) {
let pathToRemixConfig: string;
for (const ext of ['.js', '.mjs', '.cjs']) {
pathToRemixConfig = joinPathFragments(projectRoot, `remix.config${ext}`);
if (tree.exists(pathToRemixConfig)) {
return pathToRemixConfig;
}
}
throw new Error(
`Could not find a Remix Config File. Please ensure a "remix.config.js" file exists at the root of your project.`
);
}
const _remixConfigCache: Record<string, AppConfig> = {};
export async function getRemixConfigValues(tree: Tree, projectName: string) {
const remixConfigPath = joinPathFragments(
workspaceRoot,
getRemixConfigPath(tree, projectName)
);
const cacheKey = `${projectName}/${remixConfigPath}`;
let appConfig = _remixConfigCache[cacheKey];
if (!appConfig) {
try {
const importedConfig = await Function(
`return import("${remixConfigPath}?t=${Date.now()}")`
)();
appConfig = (importedConfig?.default || importedConfig) as AppConfig;
} catch {
appConfig = require(remixConfigPath);
}
_remixConfigCache[cacheKey] = appConfig;
}
return appConfig;
}