Skip to content

Commit 5b91f8d

Browse files
authored
Custom template (codesandbox#854)
* Custom template * Bump * Reset compilation when config of modules change * Fix null sandpackconfig * Remove double setup * Bump * Make optimization of cache optional * Bump * Don't save cache on smoosh * BUMP * Add module mockgp * Version bump * Fix package json version resolving * Bump * mock modulke * Fix module * Bump to 0.0.45 * Fix typing problem * Fix new build step * Version bump * Remove console.log * Bump version * Async file resolving * Remote messaging system * Add remote file resolving * Bump version * Remove comments * Allow dynamic import in ESModule dependencies * Also add dynamic syntax to parser opts * Fix custom template resetting for new manager * Bump * Bump * Upgrade codesandbox-api * Fix typings * Fix getCodeSandboxURL * Bump version * Fix merge conflict * Add babel transpiler * New version bump * Update locks * Publish new codesandbox-api * Bump sandpack * Update codesandbox-api * Add button for disabling button * Add option to manager * Bump * Rename vscode-editor * Disable shadows altogether in SSE loading screen
1 parent c1d3de6 commit 5b91f8d

File tree

34 files changed

+1004
-3176
lines changed

34 files changed

+1004
-3176
lines changed

packages/app/config/webpack.common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ module.exports = {
326326
? [
327327
new HtmlWebpackPlugin({
328328
inject: true,
329-
chunks: ['sandbox-startup', 'sandbox'],
329+
chunks: ['sandbox-startup', 'vendors~sandbox', 'sandbox'],
330330
filename: 'frame.html',
331331
template: paths.sandboxHtml,
332332
minify: __PROD__ && {

packages/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
"cerebral": "^4.0.0",
144144
"circular-json": "^0.4.0",
145145
"codemirror": "^5.27.4",
146-
"codesandbox-api": "^0.0.18",
146+
"codesandbox-api": "^0.0.20",
147147
"codesandbox-import-utils": "1.3.5",
148148
"color": "^0.11.4",
149149
"compare-versions": "^3.1.0",

packages/app/src/app/store/actions.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { mainModule, defaultOpenedModule } from './utils/main-module';
99
export function getSandbox({ props, api, path }) {
1010
return api
1111
.get(`/sandboxes/${props.id}`)
12-
.then(data => path.success({ sandbox: data }))
12+
.then(data => {
13+
// data.template = 'custom';
14+
return path.success({ sandbox: data });
15+
})
1316
.catch(error => {
1417
if (error.response.status === 404) {
1518
return path.notFound();

packages/app/src/sandbox/compile.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ const PREINSTALLED_DEPENDENCIES = [
193193
'babel-plugin-transform-vue-jsx',
194194
'babel-plugin-jsx-pragmatic',
195195
'flow-bin',
196+
...BABEL_DEPENDENCIES,
196197
];
197198

198199
function getDependencies(parsedPackage, templateDefinition, configurations) {
@@ -250,13 +251,16 @@ function getDependencies(parsedPackage, templateDefinition, configurations) {
250251
}
251252
});
252253

253-
let preinstalledDependencies = PREINSTALLED_DEPENDENCIES;
254-
if (templateDefinition.name !== 'babel-repl') {
255-
preinstalledDependencies = [
256-
...preinstalledDependencies,
257-
...BABEL_DEPENDENCIES,
258-
];
259-
}
254+
const sandpackConfig =
255+
(configurations.customTemplate &&
256+
configurations.customTemplate.parsed &&
257+
configurations.customTemplate.parsed.sandpack) ||
258+
{};
259+
260+
const preinstalledDependencies =
261+
sandpackConfig.preInstalledDependencies == null
262+
? PREINSTALLED_DEPENDENCIES
263+
: sandpackConfig.preInstalledDependencies;
260264

261265
if (templateDefinition.name === 'reason') {
262266
returnedDependencies = {
@@ -291,12 +295,15 @@ async function updateManager(
291295
managerModules,
292296
manifest,
293297
configurations,
294-
isNewCombination
298+
isNewCombination,
299+
hasFileResolver
295300
) {
296301
let newManager = false;
297302
if (!manager || manager.id !== sandboxId) {
298303
newManager = true;
299-
manager = new Manager(sandboxId, getPreset(template), managerModules);
304+
manager = new Manager(sandboxId, getPreset(template), managerModules, {
305+
hasFileResolver,
306+
});
300307
}
301308

302309
if (isNewCombination || newManager) {
@@ -327,6 +334,8 @@ function getDocumentHeight() {
327334
html.scrollHeight,
328335
html.offsetHeight
329336
);
337+
338+
return height;
330339
}
331340

332341
function sendResize() {
@@ -379,6 +388,8 @@ async function compile({
379388
entry,
380389
showOpenInCodeSandbox = false,
381390
skipEval = false,
391+
hasFileResolver = false,
392+
disableDependencyPreprocessing = false,
382393
}) {
383394
dispatch({
384395
type: 'start',
@@ -438,7 +449,10 @@ async function compile({
438449
templateDefinition,
439450
configurations
440451
);
441-
const { manifest, isNewCombination } = await loadDependencies(dependencies);
452+
const { manifest, isNewCombination } = await loadDependencies(
453+
dependencies,
454+
disableDependencyPreprocessing
455+
);
442456

443457
if (isNewCombination && !firstLoad) {
444458
// Just reset the whole manager if it's a new combination
@@ -449,14 +463,16 @@ async function compile({
449463
}
450464
const t = Date.now();
451465

452-
await updateManager(
453-
sandboxId,
454-
template,
455-
modules,
456-
manifest,
457-
configurations,
458-
isNewCombination
459-
);
466+
const updatedModules =
467+
(await updateManager(
468+
sandboxId,
469+
template,
470+
modules,
471+
manifest,
472+
configurations,
473+
isNewCombination,
474+
hasFileResolver
475+
)) || [];
460476

461477
const possibleEntries = templateDefinition.getEntries(configurations);
462478

@@ -475,6 +491,8 @@ async function compile({
475491
const main = absolute(foundMain);
476492
managerModuleToTranspile = modules[main];
477493

494+
await manager.preset.setup(manager, updatedModules);
495+
478496
dispatch({ type: 'status', status: 'transpiling' });
479497
manager.setStage('transpilation');
480498

@@ -650,7 +668,7 @@ async function compile({
650668

651669
if (manager) {
652670
const managerState = {
653-
...manager.serialize(),
671+
...manager.serialize(false),
654672
};
655673
delete managerState.cachedPaths;
656674
managerState.entry = managerModuleToTranspile

packages/app/src/sandbox/eval/cache.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export async function saveCache(
4646
changes: number,
4747
firstRun: boolean
4848
) {
49+
if (!sandboxId) {
50+
return;
51+
}
52+
4953
const managerState = {
5054
...manager.serialize(),
5155
};

packages/app/src/sandbox/eval/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
cxjs,
1111
babel,
1212
dojo,
13+
custom,
1314
reason,
1415
} from 'common/templates';
1516

@@ -24,6 +25,7 @@ import babelPreset from './presets/babel-repl';
2425
import cxjsPreset from './presets/cxjs';
2526
import reasonPreset from './presets/reason';
2627
import dojoPreset from './presets/dojo';
28+
import customPreset from './presets/custom';
2729

2830
export default function getPreset(template: string) {
2931
switch (template) {
@@ -49,6 +51,8 @@ export default function getPreset(template: string) {
4951
return cxjsPreset();
5052
case dojo.name:
5153
return dojoPreset();
54+
case custom.name:
55+
return customPreset();
5256
default:
5357
return reactPreset();
5458
}

0 commit comments

Comments
 (0)