forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
81 lines (68 loc) · 2.22 KB
/
index.ts
File metadata and controls
81 lines (68 loc) · 2.22 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { pickBy } from 'lodash-es';
import setScreen from '../status-screen';
import { getDependencyVersions } from '../version-resolving';
import dependenciesToQuery from './dependencies-to-query';
import { fetchDependencies } from './fetch-dependencies';
let loadedDependencyCombination: string | null = null;
let manifest = null;
export type NPMDependencies = {
[dependency: string]: string;
};
/**
* If there is a URL to a file we need to fetch the dependencies dynamically, at least
* for the first version. In the future we might want to consider a hybrid version where
* we only fetch the dynamic files for dependencies with a url as version. But this is a good
* start.
*/
function shouldFetchDynamically(dependencies: NPMDependencies) {
return Object.keys(dependencies).some(depName =>
dependencies[depName].includes('http')
);
}
/**
* This fetches the manifest and dependencies from the
* @param {*} dependencies
*/
export async function loadDependencies(
dependencies: NPMDependencies,
{
disableExternalConnection = false,
resolutions = undefined,
showFullScreen = false,
} = {}
) {
let isNewCombination = false;
if (Object.keys(dependencies).length !== 0) {
// We filter out all @types, as they are not of any worth to the bundler
const dependenciesWithoutTypings = pickBy(
dependencies,
(val, key) => !(key.includes && key.includes('@types'))
);
const depQuery = dependenciesToQuery(dependenciesWithoutTypings);
if (loadedDependencyCombination !== depQuery) {
isNewCombination = true;
const fetchDynamically =
disableExternalConnection ||
shouldFetchDynamically(dependenciesWithoutTypings);
const fetchFunction = fetchDynamically
? getDependencyVersions
: fetchDependencies;
const data = await fetchFunction(
dependenciesWithoutTypings,
resolutions,
showFullScreen
);
// Mark that the last requested url is this
loadedDependencyCombination = depQuery;
manifest = data;
setScreen({
type: 'loading',
text: 'Transpiling Modules...',
showFullScreen,
});
}
} else {
manifest = null;
}
return { manifest, isNewCombination };
}