Skip to content

Commit 88a5b7b

Browse files
authored
Download dependencies seperately and merge in bundler (codesandbox#3880)
* Download dependencies seperately and merge in bundler * Fix loading dependencies with a dash in the version Fixes codesandbox#3782 * Clean up fetching packages * Add new packager to service worker
1 parent 3a39cac commit 88a5b7b

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

packages/app/config/webpack.prod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ module.exports = merge(commonConfig, {
286286
},
287287
},
288288
{
289-
urlPattern: /prod-packager-packages\.csb\.dev/,
289+
urlPattern: /prod-packager-packages\.codesandbox\.io/,
290290
handler: 'fastest',
291291
options: {
292292
cache: {

packages/app/src/sandbox/eval/utils/get-dependency-name.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ describe('getDependencyName', () => {
2222
'@angular/core/4.4.3'
2323
);
2424
});
25+
26+
it('can find a simple dependency name with beta tag', () => {
27+
expect(getDependencyName('reakit/1.0.0-beta.16/lib/Component')).toBe(
28+
'reakit/1.0.0-beta.16'
29+
);
30+
});
2531
});

packages/app/src/sandbox/eval/utils/get-dependency-name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function getDependencyName(path: string) {
55
if (path.startsWith('@')) {
66
dependencyName += `/${dependencyParts.shift()}`;
77
}
8-
if (dependencyParts[0] && /^\d+\.\d+\.\d+$/.test(dependencyParts[0])) {
8+
if (dependencyParts[0] && /^\d+\.\d+\.\d+.*$/.test(dependencyParts[0])) {
99
// Make sure to include the aliased version if it's part of it
1010
dependencyName += `/${dependencyParts.shift()}`;
1111
}

packages/app/src/sandbox/npm/fetch-dependencies.ts

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _debug from '@codesandbox/common/lib/utils/debug';
22
import { getAbsoluteDependencies } from '@codesandbox/common/lib/utils/dependencies';
33
import { actions, dispatch } from 'codesandbox-api';
4+
import { mergeDependencies } from 'sandbox/version-resolving/merge-dependency';
45

56
import setScreen from '../status-screen';
67
import delay from '../utils/delay';
@@ -60,7 +61,7 @@ function callApi(url: string, method = 'GET') {
6061
*
6162
* @param {string} query The dependencies to call
6263
*/
63-
async function requestPackager(url, method = 'GET') {
64+
async function requestPackager(url: string, method = 'GET') {
6465
let retries = 0;
6566

6667
// eslint-disable-next-line no-constant-condition
@@ -85,17 +86,8 @@ async function requestPackager(url, method = 'GET') {
8586
}
8687
}
8788

88-
function dependenciesToBucketPath(dependencies: Object) {
89-
return `v${VERSION}/combinations/${Object.keys(dependencies)
90-
.sort()
91-
.map(
92-
// Paths starting with slashes don't work with cloudfront, even escaped. So we remove the slashes
93-
dep =>
94-
`${encodeURIComponent(dep.replace('/', '-').replace('@', ''))}@${
95-
dependencies[dep]
96-
}`
97-
)
98-
.join('%2B')}.json`;
89+
function dependencyToPackagePath(name: string, version: string) {
90+
return `v${VERSION}/packages/${name}/${version}.json`;
9991
}
10092

10193
/**
@@ -115,44 +107,50 @@ async function getDependencies(
115107
dependencies: Object,
116108
showLoadingFullScreen: boolean
117109
) {
118-
const absoluteDependencies = await getAbsoluteDependencies(
119-
removeSpacesFromDependencies(dependencies)
120-
);
121-
const dependencyUrl = dependenciesToQuery(absoluteDependencies);
122-
const bucketDependencyUrl = dependenciesToBucketPath(absoluteDependencies);
123-
124110
setScreen({
125111
type: 'loading',
126112
text: 'Downloading Dependencies...',
127113
showFullScreen: showLoadingFullScreen,
128114
});
129115

130-
try {
131-
const bucketManifest = await callApi(
132-
`${BUCKET_URL}/${bucketDependencyUrl}`
133-
);
134-
return bucketManifest;
135-
} catch (e) {
136-
setScreen({
137-
type: 'loading',
138-
text: 'Resolving Dependencies...',
139-
showFullScreen: showLoadingFullScreen,
140-
});
141-
142-
// The dep has not been generated yet...
143-
const { url } = await requestPackager(
144-
`${PACKAGER_URL}/${dependencyUrl}`,
145-
'POST'
146-
);
147-
148-
setScreen({
149-
type: 'loading',
150-
text: 'Downloading Dependencies...',
151-
showFullScreen: showLoadingFullScreen,
152-
});
153-
154-
return requestPackager(`${BUCKET_URL}/${url}`);
155-
}
116+
const absoluteDependencies = await getAbsoluteDependencies(
117+
removeSpacesFromDependencies(dependencies)
118+
);
119+
120+
const deps = await Promise.all(
121+
Object.keys(absoluteDependencies).map(async name => {
122+
const version = absoluteDependencies[name];
123+
const dep = { [name]: version };
124+
125+
const dependencyUrl = dependenciesToQuery(dep);
126+
const bucketDependencyUrl = dependencyToPackagePath(name, version);
127+
const fullUrl = `${BUCKET_URL}/${bucketDependencyUrl}`;
128+
129+
try {
130+
const bucketManifest = await callApi(fullUrl);
131+
return bucketManifest;
132+
} catch (e) {
133+
setScreen({
134+
type: 'loading',
135+
text: 'Resolving Dependencies...',
136+
showFullScreen: showLoadingFullScreen,
137+
});
138+
139+
// The dep has not been generated yet...
140+
await requestPackager(`${PACKAGER_URL}/${dependencyUrl}`, 'POST');
141+
142+
setScreen({
143+
type: 'loading',
144+
text: 'Downloading Dependencies...',
145+
showFullScreen: showLoadingFullScreen,
146+
});
147+
148+
return requestPackager(fullUrl);
149+
}
150+
})
151+
);
152+
153+
return mergeDependencies(deps);
156154
}
157155

158156
export async function fetchDependencies(

0 commit comments

Comments
 (0)