Skip to content

Commit 8ffb3ac

Browse files
author
Ives van Hoorne
committed
Use webpackbin as bundler
1 parent 4f5f7f3 commit 8ffb3ac

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

src/app/store/entities/reducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import moduleReducer from './sandboxes/modules/reducer';
99
import directoryReducer from './sandboxes/directories/reducer';
1010
import userReducer from './sandboxes/users/reducer';
1111

12-
const d = _debug('cw:app:store:reducers:entities');
12+
const d = _debug('cs:app:store:reducers:entities');
1313

1414
const entityReducers = {
1515
sandboxes: sandboxReducer,

src/app/store/entities/sandboxes/actions.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// @flow
22
import { push } from 'react-router-redux';
3+
import type { Module, Directory } from 'common/types';
34

45
import { createAPIActions, doRequest } from '../../api/actions';
56
import { normalizeResult } from '../actions';
67
import notificationActions from '../../notifications/actions';
78
import entity from './entity';
89
import fetchBundle from './bundle-loader';
9-
import type { Module } from './modules/entity';
1010
import moduleEntity from './modules/entity';
1111
import moduleActions from './modules/actions';
12-
import type { Directory } from './directories/entity';
1312
import directoryEntity from './directories/entity';
1413
import directoryActions from './directories/actions';
1514
import { singleSandboxSelector } from './selectors';
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1+
import { singleSandboxSelector } from 'app/store/entities/sandboxes/selectors';
2+
13
import callApi from '../../services/api';
2-
import delay from '../../services/delay';
4+
5+
const getDependencyString = npmDependencies => {
6+
return Object.keys(npmDependencies)
7+
.map(dep => `${dep}@${npmDependencies[dep]}`)
8+
.join('+');
9+
};
310

411
export default function fetch(actions, id: string) {
5-
return async (dispatch: Function) => {
12+
return async (dispatch: Function, getState: Function) => {
613
dispatch({ type: actions.REQUEST, initial: true, id });
7-
const firstResult = await callApi('/bundler/bundle', null, {
8-
method: 'POST',
9-
body: { id },
10-
});
11-
dispatch({ type: actions.SUCCESS, result: firstResult });
1214

13-
if (firstResult.manifest) {
14-
return firstResult;
15-
}
15+
const sandbox = singleSandboxSelector(getState(), { id });
16+
const dependencyString = getDependencyString(sandbox.npmDependencies);
17+
18+
const baseUrl = `https://cdn.jsdelivr.net/webpack/v1/${dependencyString}`;
1619

17-
while (true) {
18-
await delay(1000);
19-
const result = await callApi(`/bundler/bundle/${firstResult.hash}`);
20+
const manifest = await callApi(`${baseUrl}/manifest.json`);
21+
22+
const result = {
23+
manifest,
24+
url: `${baseUrl}/dll.js`,
25+
};
26+
27+
dispatch({
28+
type: actions.SUCCESS,
29+
result,
30+
});
2031

21-
if (result.manifest) {
22-
return result;
23-
}
24-
}
32+
return result;
2533
};
2634
}

src/app/store/services/api.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { camelizeKeys, decamelizeKeys } from 'humps';
33
import axios from 'axios';
44

5-
import getJwt from '../user/utils/jwt';
65
import { optionsToParameterizedUrl } from '../../utils/url-generator';
76

87
const API_ROOT = '/api/v1/';
@@ -14,6 +13,12 @@ export type BodyType = {
1413
jwt: ?string,
1514
};
1615

16+
const getUrl = (url: string) => {
17+
if (url.startsWith('https://')) return url;
18+
19+
return url.split('')[0] === '/' ? url : `${API_ROOT}${url}`;
20+
};
21+
1722
/**
1823
* Sends a request to the API and returns a promise with camelized response
1924
*/
@@ -29,9 +34,7 @@ export default (async function callApi(
2934
if (!endpoint) throw new Error('No endpoint is given');
3035

3136
// If it is an absolute url.
32-
const url = endpoint.split('')[0] === '/'
33-
? endpoint
34-
: `${API_ROOT}${endpoint}`;
37+
const url = getUrl(endpoint);
3538

3639
const options = { url, method };
3740

src/sandbox/eval/js.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,14 @@ export default function evaluateJS(
5959
// eslint-disable-line no-unused-vars
6060
if (/^\w/.test(path)) {
6161
// So it must be a dependency
62-
const dependencyManifest = manifest[path] || manifest[`${path}.js`];
63-
if (dependencyManifest) {
64-
return window.dependencies(dependencyManifest.id);
62+
const dependencyModuleIdString = manifest.externals[path] ||
63+
manifest.externals[`${path}.js`];
64+
65+
const dependencyModuleId = dependencyModuleIdString.match(
66+
/dll_bundle\((.*)\)/
67+
)[1];
68+
if (dependencyModuleId) {
69+
return window.dll_bundle(dependencyModuleId);
6570
} else {
6671
throw new DependencyNotFoundError(path);
6772
}

src/sandbox/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ let url = null;
2020

2121
async function addDependencyBundle() {
2222
if (url !== '') {
23+
window.dll_bundle = null;
2324
const script = document.createElement('script');
2425
script.setAttribute('src', url);
2526
script.setAttribute('async', false);
2627
document.head.appendChild(script);
2728

28-
while (window.dependencies == null) {
29+
while (window.dll_bundle == null) {
2930
await delay(100);
3031
}
3132
}

0 commit comments

Comments
 (0)