Skip to content

Commit c9847bb

Browse files
author
Ives van Hoorne
committed
Handers
1 parent 3c1c02a commit c9847bb

File tree

13 files changed

+209
-78
lines changed

13 files changed

+209
-78
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -383,57 +383,57 @@ async function compile({
383383
/* don't do anything with this error */
384384
}
385385
}
386-
}
387386

388-
if ((!manager.webpackHMR || firstLoad) && !manager.preset.htmlDisabled) {
389-
if (!managerTranspiledModuleToTranspile.compilation) {
390-
const htmlModule =
391-
modules[
392-
templateDefinition
393-
.getHTMLEntries(configurations)
394-
.find(p => modules[p])
395-
];
396-
397-
const html = htmlModule
398-
? htmlModule.code
399-
: template === 'vue-cli'
400-
? '<div id="app"></div>'
401-
: '<div id="root"></div>';
402-
document.body.innerHTML = html;
387+
if ((!manager.webpackHMR || firstLoad) && !manager.preset.htmlDisabled) {
388+
if (!managerTranspiledModuleToTranspile.compilation) {
389+
const htmlModule =
390+
modules[
391+
templateDefinition
392+
.getHTMLEntries(configurations)
393+
.find(p => modules[p])
394+
];
395+
396+
const html = htmlModule
397+
? htmlModule.code
398+
: template === 'vue-cli'
399+
? '<div id="app"></div>'
400+
: '<div id="root"></div>';
401+
document.body.innerHTML = html;
402+
}
403403
}
404-
}
405404

406-
const tt = Date.now();
407-
const oldHTML = document.body.innerHTML;
408-
const evalled = manager.evaluateModule(managerModuleToTranspile);
409-
debug(`Evaluation time: ${Date.now() - tt}ms`);
410-
const domChanged =
411-
!manager.preset.htmlDisabled && oldHTML !== document.body.innerHTML;
412-
413-
if (
414-
isModuleView &&
415-
!domChanged &&
416-
!managerModuleToTranspile.path.endsWith('.html')
417-
) {
418-
const isReact =
419-
managerModuleToTranspile.code &&
420-
managerModuleToTranspile.code.includes('React');
421-
422-
if (isReact) {
423-
// initiate boilerplates
424-
if (getBoilerplates().length === 0) {
425-
try {
426-
await evalBoilerplates(defaultBoilerplates);
427-
} catch (e) {
428-
console.log("Couldn't load all boilerplates: " + e.message);
429-
}
405+
const tt = Date.now();
406+
const oldHTML = document.body.innerHTML;
407+
const evalled = manager.evaluateModule(managerModuleToTranspile);
408+
debug(`Evaluation time: ${Date.now() - tt}ms`);
409+
const domChanged =
410+
!manager.preset.htmlDisabled && oldHTML !== document.body.innerHTML;
430411

431-
const boilerplate = findBoilerplate(managerModuleToTranspile);
432-
if (boilerplate) {
412+
if (
413+
isModuleView &&
414+
!domChanged &&
415+
!managerModuleToTranspile.path.endsWith('.html')
416+
) {
417+
const isReact =
418+
managerModuleToTranspile.code &&
419+
managerModuleToTranspile.code.includes('React');
420+
421+
if (isReact) {
422+
// initiate boilerplates
423+
if (getBoilerplates().length === 0) {
433424
try {
434-
boilerplate.module.default(evalled);
425+
await evalBoilerplates(defaultBoilerplates);
435426
} catch (e) {
436-
console.error(e);
427+
console.log("Couldn't load all boilerplates: " + e.message);
428+
}
429+
430+
const boilerplate = findBoilerplate(managerModuleToTranspile);
431+
if (boilerplate) {
432+
try {
433+
boilerplate.module.default(evalled);
434+
} catch (e) {
435+
console.error(e);
436+
}
437437
}
438438
}
439439
}
@@ -467,9 +467,15 @@ async function compile({
467467

468468
debug(`Total time: ${Date.now() - startTime}ms`);
469469

470+
const managerState = {
471+
...manager.serialize(),
472+
};
473+
delete managerState.cachedPaths;
474+
managerState.entry = managerModuleToTranspile.path;
475+
470476
dispatch({
471477
type: 'success',
472-
state: manager.serialize(),
478+
state: managerState,
473479
});
474480

475481
manager.save();

packages/react-sandpack/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ compiled
22
storybook-static
33
.rpt2_cache
44
dist
5-
docs
5+
docs
6+
.env

packages/react-sandpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"colors": "^1.1.2",
9999
"commitizen": "^2.9.6",
100100
"coveralls": "^3.0.0",
101-
"cross-env": "^5.0.1",
101+
"cross-env": "^5.1.3",
102102
"cz-conventional-changelog": "^2.0.0",
103103
"gulp": "^3.9.1",
104104
"gulp-concat": "^2.6.1",

packages/react-sandpack/src/components/Navigator/Navigator.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ class Navigator extends React.Component<Props, State> {
2525
super(props);
2626
this.listener = listen(this.handleMessage);
2727

28-
let urlState = this.getUrlState(props.sandpack.sandboxUrl);
29-
30-
if (props.sandpack.browserFrame) {
31-
urlState = this.getUrlState(props.sandpack.browserFrame.src);
32-
}
33-
34-
this.state = urlState;
28+
this.state = {
29+
browserPath: '/',
30+
baseUrl: '',
31+
lastCommittedUrl: '/',
32+
};
3533
}
3634

3735
componentWillUnmount() {
@@ -41,7 +39,6 @@ class Navigator extends React.Component<Props, State> {
4139
handleMessage = (message: any) => {
4240
switch (message.type) {
4341
case 'urlchange': {
44-
console.log(message);
4542
this.setState(this.getUrlState(message.url));
4643
break;
4744
}

packages/react-sandpack/src/components/SandpackProvider/SandpackProvider.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export interface Props {
2424
height?: number | string;
2525
bundlerURL?: string;
2626
skipEval: boolean;
27+
template?:
28+
| 'create-react-app'
29+
| 'create-react-app-typescript'
30+
| 'parcel'
31+
| 'vue-cli'
32+
| 'angular-cli'
33+
| 'preact-cli';
2734
}
2835

2936
export default class SandpackProvider extends React.PureComponent<
@@ -109,6 +116,7 @@ export default class SandpackProvider extends React.PureComponent<
109116
this.props.dependencies,
110117
this.props.entry
111118
),
119+
template: this.props.template,
112120
},
113121
{
114122
skipEval: this.props.skipEval,
@@ -124,15 +132,16 @@ export default class SandpackProvider extends React.PureComponent<
124132
this.setState({ files });
125133

126134
if (this.manager) {
127-
this.manager.updatePreview({ files });
135+
this.manager.updatePreview({ files, template: this.props.template });
128136
}
129137
};
130138

131139
componentDidUpdate(props: Props) {
132140
if (
133141
props.files !== this.props.files ||
134142
props.dependencies !== this.props.dependencies ||
135-
props.entry !== this.props.entry
143+
props.entry !== this.props.entry ||
144+
props.template !== this.props.template
136145
) {
137146
const newFiles = this.createMissingPackageJSON(
138147
this.props.files,

packages/react-sandpack/src/components/TranspiledCodeView/TranspiledCodeView.tsx

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,92 @@ import * as React from 'react';
22
import { listen } from 'codesandbox-api';
33
import { Controlled } from 'react-codemirror2';
44

5+
import classNames from 'classnames';
6+
57
import { ISandpackContext } from '../../types';
68

9+
import cn from '../../utils/cn';
710
import SandpackConsumer from '../SandpackConsumer';
811
import CodeMirrorComponent from '../../helper-components/CodeMirror';
912

1013
export interface Props {
1114
style?: Object;
15+
className?: string;
1216
}
1317

1418
export interface State {
15-
managerState: any;
19+
error?: {
20+
title: string;
21+
message: string;
22+
path: string;
23+
line: number;
24+
column: number;
25+
};
1626
}
1727

1828
export default class TranspiledCodeView extends React.Component<Props, State> {
29+
listener: Function;
30+
31+
constructor(props: Props) {
32+
super(props);
33+
34+
this.listener = listen(this.handleMessage);
35+
this.state = {};
36+
}
37+
38+
handleMessage = (m: any) => {
39+
if (m.type === 'action' && m.action === 'show-error') {
40+
const { title, path, message, line, column } = m;
41+
this.setState({ error: { title, path, message, line, column } });
42+
} else if (m.type === 'success') {
43+
this.setState({ error: undefined });
44+
}
45+
};
46+
47+
componentWillUnmount() {
48+
this.listener();
49+
}
50+
1951
getTranspiledCode(sandpack: ISandpackContext) {
2052
const { openedPath, managerState } = sandpack;
2153
if (managerState == null) {
2254
return null;
2355
}
2456

25-
const tModule = managerState.transpiledModules[openedPath + ':'];
57+
const tModule = managerState.transpiledModules[managerState.entry + ':'];
2658
if (tModule && tModule.source) {
2759
const { compiledCode } = tModule.source;
2860

2961
if (compiledCode) {
3062
return compiledCode;
31-
// .replace(/\/\/# sourceMappingURL.*/, '')
32-
// .replace(/\/\/# sourceURL.*/, '');
3363
}
3464
}
3565

3666
return null;
3767
}
3868

3969
render() {
70+
const className = classNames(
71+
cn('TranspiledCodeView', 'container'),
72+
this.props.className
73+
);
74+
4075
return (
4176
<SandpackConsumer>
4277
{sandpack => (
43-
<CodeMirrorComponent
44-
style={this.props.style}
45-
onBeforeChange={() => {
46-
/* empty */
47-
}}
48-
value={this.getTranspiledCode(sandpack) || '// loading...'}
49-
/>
78+
<div className={className} style={this.props.style}>
79+
<CodeMirrorComponent
80+
onBeforeChange={() => {
81+
/* empty */
82+
}}
83+
value={this.getTranspiledCode(sandpack) || '// loading...'}
84+
/>
85+
{this.state.error && (
86+
<div className={cn('TranspiledCodeView', 'error')}>
87+
{this.state.error.message}
88+
</div>
89+
)}
90+
</div>
5091
)}
5192
</SandpackConsumer>
5293
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.sandpack-TranspiledCodeView__container {
2+
display: flex;
3+
flex-direction: column;
4+
height: 100%;
5+
}
6+
7+
.sandpack-TranspiledCodeView__error {
8+
font-family: Menlo, Source Code Pro, monospace;
9+
background-color: #ba3537;
10+
color: white;
11+
12+
padding: 0.5rem;
13+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
* {
2-
box-sizing: border-box;
1+
.sandpack {
2+
font-family: Helvetica, sans-serif;
3+
4+
* {
5+
box-sizing: border-box;
6+
}
37
}

packages/react-sandpack/src/types.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ export interface ITranspiledModule {
3939
}
4040

4141
export interface IManagerState {
42-
cachedPaths: {
43-
[path: string]: string;
44-
};
42+
entry: string;
4543
transpiledModules: {
4644
[id: string]: ITranspiledModule;
4745
};
@@ -52,6 +50,7 @@ export interface ISandpackContext {
5250
managerState: IManagerState | undefined;
5351
sandboxUrl: string;
5452
openedPath: string;
53+
entry: string;
5554
files: IFiles;
5655
openFile: (path: string) => void;
5756
updateFiles: (files: IFiles) => void;

0 commit comments

Comments
 (0)