Skip to content

Commit 7129ffd

Browse files
author
Ives van Hoorne
committed
Add errors to sandpack provider
1 parent f370bd5 commit 7129ffd

File tree

5 files changed

+42
-43
lines changed

5 files changed

+42
-43
lines changed

packages/react-sandpack/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-smooshpack",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"description": "",
55
"keywords": [],
66
"license": "SEE LICENSE.MD IN ROOT",
@@ -77,7 +77,7 @@
7777
"react-broadcast": "^0.6.2",
7878
"react-codemirror2": "^4.0.1",
7979
"rollup-plugin-scss": "^0.4.0",
80-
"smooshpack": "^0.0.10"
80+
"smooshpack": "^0.0.13"
8181
},
8282
"peerDependencies": {
8383
"react": "^16.0.0"

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ import { Broadcast } from 'react-broadcast';
33
import { Manager, generatePackageJSON } from 'smooshpack';
44
import { listen } from 'codesandbox-api';
55

6-
import { IFile, IFiles, IManagerState, ISandpackContext } from '../../types';
6+
import {
7+
IFile,
8+
IFiles,
9+
IManagerState,
10+
ISandpackContext,
11+
IModuleError,
12+
} from '../../types';
713

814
export interface State {
915
files: IFiles;
1016
browserPath: string;
1117
openedPath: string;
1218
iframe: HTMLIFrameElement | null;
1319
managerState: IManagerState | undefined;
20+
errors: Array<IModuleError>;
1421
}
1522

1623
export interface Props {
@@ -62,14 +69,20 @@ export default class SandpackProvider extends React.PureComponent<
6269
openedPath: props.entry || '/index.js',
6370
iframe: null,
6471
managerState: undefined,
72+
errors: [],
6573
};
6674

6775
this.listener = listen(this.handleMessage);
6876
}
6977

70-
handleMessage = (message: any) => {
71-
if (message.type === 'success') {
72-
this.setState({ managerState: message.state });
78+
handleMessage = (m: any) => {
79+
if (m.type === 'success') {
80+
this.setState({ managerState: m.state, errors: [] });
81+
} else if (m.type === 'action' && m.action === 'show-error') {
82+
const { title, path, message, line, column } = m;
83+
this.setState({
84+
errors: [...this.state.errors, { title, path, message, line, column }],
85+
});
7386
}
7487
};
7588

@@ -171,10 +184,19 @@ export default class SandpackProvider extends React.PureComponent<
171184
};
172185

173186
_getSandpackState = (): ISandpackContext => {
174-
const { iframe, files, browserPath, openedPath, managerState } = this.state;
187+
const {
188+
iframe,
189+
files,
190+
browserPath,
191+
openedPath,
192+
managerState,
193+
errors,
194+
} = this.state;
195+
175196
return {
176197
files,
177198
openedPath,
199+
errors,
178200
managerState,
179201
openFile: this.openFile,
180202
browserFrame: iframe,

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

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,7 @@ export interface Props {
1515
className?: string;
1616
}
1717

18-
export interface State {
19-
error?: {
20-
title: string;
21-
message: string;
22-
path: string;
23-
line: number;
24-
column: number;
25-
};
26-
}
27-
28-
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-
18+
export default class TranspiledCodeView extends React.Component<Props> {
5119
getTranspiledCode(sandpack: ISandpackContext) {
5220
const { openedPath, managerState } = sandpack;
5321
if (managerState == null) {
@@ -82,9 +50,9 @@ export default class TranspiledCodeView extends React.Component<Props, State> {
8250
}}
8351
value={this.getTranspiledCode(sandpack) || '// loading...'}
8452
/>
85-
{this.state.error && (
53+
{sandpack.errors.length && (
8654
<div className={cn('TranspiledCodeView', 'error')}>
87-
{this.state.error.message}
55+
{sandpack.errors[0].message}
8856
</div>
8957
)}
9058
</div>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ export interface IModuleSource {
1717
sourceMap: Object | undefined;
1818
}
1919

20+
export interface IModuleError {
21+
title: string;
22+
message: string;
23+
path: string;
24+
line: number;
25+
column: number;
26+
}
27+
2028
export interface ITranspiledModule {
2129
module: IModule;
2230
query: string;
@@ -50,6 +58,7 @@ export interface ISandpackContext {
5058
managerState: IManagerState | undefined;
5159
bundlerURL: string | undefined;
5260
openedPath: string;
61+
errors: Array<IModuleError>;
5362
files: IFiles;
5463
openFile: (path: string) => void;
5564
updateFiles: (files: IFiles) => void;

packages/sandpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "smooshpack",
3-
"version": "0.0.10",
3+
"version": "0.0.13",
44
"description": "",
55
"keywords": [],
66
"main": "dist/sandpack.umd.js",

0 commit comments

Comments
 (0)