Skip to content

Commit 0a6c6bf

Browse files
author
Ives van Hoorne
committed
Improvements to config handling
1 parent 51fd903 commit 0a6c6bf

File tree

9 files changed

+103
-22
lines changed

9 files changed

+103
-22
lines changed

packages/app/config/webpack.common.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ module.exports = {
217217
],
218218
},
219219

220+
// To make jsonlint work
221+
externals: ['file', 'system'],
222+
220223
resolve: {
221224
mainFields: ['browser', 'module', 'jsnext:main', 'main'],
222225
modules: ['node_modules', 'src', 'standalone-packages'],

packages/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"cerebral": "^4.0.0",
116116
"circular-json": "^0.4.0",
117117
"codemirror": "^5.27.4",
118-
"codesandbox-api": "^0.0.14",
118+
"codesandbox-api": "^0.0.17",
119119
"codesandbox-import-utils": "^1.2.3",
120120
"color": "^0.11.4",
121121
"compare-versions": "^3.1.0",

packages/app/src/sandbox/compile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,15 @@ async function compile({
312312
.filter(x => x.error);
313313

314314
if (errors.length) {
315-
throw new Error(
315+
const e = new Error(
316316
`We weren't able to parse: '${errors[0].path}': ${
317317
errors[0].error.message
318318
}`
319319
);
320+
321+
e.fileName = errors[0].path;
322+
323+
throw e;
320324
}
321325

322326
const packageJSON = modules['/package.json'];

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

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,25 @@ function buildErrorMessage(e: TModuleError) {
5757
};
5858
}
5959

60+
const wrappedResolveModule = (manager, path) => {
61+
try {
62+
return manager && manager.resolveTranspiledModule(path, '/');
63+
} catch (e) {
64+
return null;
65+
}
66+
};
67+
6068
function buildDynamicError(ref: ErrorRecord) {
6169
const manager = getCurrentManager();
6270

63-
const relevantFrame = ref.enhancedFrames.find(r => {
64-
try {
65-
return (
66-
manager &&
67-
!!manager.resolveTranspiledModule(
68-
(r._originalFileName || r.fileName || '')
69-
.replace(location.origin, '')
70-
.replace('file://', ''),
71-
'/'
72-
)
73-
);
74-
} catch (e) {
75-
/* don't do anything */
76-
return false;
77-
}
78-
});
71+
const relevantFrame = ref.enhancedFrames.find(r =>
72+
wrappedResolveModule(
73+
manager,
74+
(r._originalFileName || r.fileName || '')
75+
.replace(location.origin, '')
76+
.replace('file://', '')
77+
)
78+
);
7979

8080
if (relevantFrame && manager) {
8181
const fileName = relevantFrame._originalFileName || relevantFrame.fileName;
@@ -102,11 +102,19 @@ function buildDynamicError(ref: ErrorRecord) {
102102
}
103103
} else {
104104
const error: TModuleError = ref.error;
105-
const tModule = error.tModule;
105+
const tModule =
106+
error.tModule ||
107+
wrappedResolveModule(
108+
manager,
109+
(error.fileName || '')
110+
.replace(location.origin, '')
111+
.replace('file://', '')
112+
);
106113

107114
if (tModule) {
108115
const newError = {
109116
...buildErrorMessage(error),
117+
path: error.fileName,
110118
type: 'action',
111119
action: 'show-error',
112120
};
@@ -121,6 +129,7 @@ function buildDynamicError(ref: ErrorRecord) {
121129
/* eslint-disable no-underscore-dangle */
122130
export default function showError(ref: ErrorRecord) {
123131
const errorToSend = buildDynamicError(ref);
132+
124133
if (errorToSend) {
125134
dispatch(
126135
actions.error.show(errorToSend.title, errorToSend.message, {
@@ -130,5 +139,13 @@ export default function showError(ref: ErrorRecord) {
130139
payload: errorToSend.payload,
131140
})
132141
);
142+
} else {
143+
// Show based on error
144+
actions.error.show(ref.error.name, ref.error.message, {
145+
line: ref.error.lineNumber,
146+
column: ref.error.columnNumber,
147+
path: ref.error.fileName,
148+
payload: {},
149+
});
133150
}
134151
}

packages/app/src/sandbox/eval/transpilers/babel/babel-parser.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@ const DEFAULT_BABEL_CONFIG = {
2929
/**
3030
* Parses the .babelrc if it exists, if it doesn't it will return a default config
3131
*/
32-
export default function getBabelConfig(config?: Object, path: string) {
32+
export default function getBabelConfig(
33+
config?: Object,
34+
loaderOptions,
35+
path: string
36+
) {
3337
const resolvedConfig = config || DEFAULT_BABEL_CONFIG;
3438

39+
if (loaderOptions.disableCodeSandboxPlugins) {
40+
return resolvedConfig;
41+
}
42+
3543
return {
3644
...resolvedConfig,
3745
sourceMaps: 'inline',

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ class BabelTranspiler extends WorkerTranspiler {
4545
foundConfig = loaderContext.options.configurations.babel.parsed;
4646
}
4747

48-
const babelConfig = getBabelConfig(foundConfig, path);
48+
const babelConfig = getBabelConfig(
49+
foundConfig,
50+
loaderContext.options,
51+
path
52+
);
4953

5054
this.queueTask(
5155
{

packages/common/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"image-extensions": "^1.1.0",
7+
"jsonlint": "^1.6.3",
78
"react-tippy": "^0.14.0",
89
"styled-components": "^3.2.1"
910
}

packages/common/templates/configuration/parse.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import type { ConfigurationFile } from 'common/templates/configuration/types';
33
import type { Sandbox } from 'common/types';
44

5+
import { parse } from 'jsonlint';
6+
57
type ConfigurationFiles = {
68
[path: string]: ConfigurationFile,
79
};
@@ -41,7 +43,7 @@ export default function parseConfigurations(
4143
path,
4244
};
4345
try {
44-
const parsed = JSON.parse(code);
46+
const parsed = parse(code);
4547

4648
configurations[configurationFile.type] = {
4749
...baseObject,

yarn.lock

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,10 @@ JSONStream@^1.0.4:
683683
jsonparse "^1.2.0"
684684
through ">=2.2.7 <3"
685685

686+
JSV@^4.0.x:
687+
version "4.0.2"
688+
resolved "https://registry.yarnpkg.com/JSV/-/JSV-4.0.2.tgz#d077f6825571f82132f9dffaed587b4029feff57"
689+
686690
abab@^1.0.3, abab@^1.0.4:
687691
version "1.0.4"
688692
resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.4.tgz#5faad9c2c07f60dd76770f71cf025b62a63cfd4e"
@@ -953,6 +957,10 @@ ansi-styles@^3.0.0, ansi-styles@^3.1.0, ansi-styles@^3.2.0:
953957
dependencies:
954958
color-convert "^1.9.0"
955959

960+
ansi-styles@~1.0.0:
961+
version "1.0.0"
962+
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-1.0.0.tgz#cb102df1c56f5123eab8b67cd7b98027a0279178"
963+
956964
957965
version "0.1.0"
958966
resolved "https://registry.yarnpkg.com/ansi-wrap/-/ansi-wrap-0.1.0.tgz#a82250ddb0015e9a27ca82e82ea603bbfa45efaf"
@@ -3581,6 +3589,14 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0, chalk@^2.3.1:
35813589
escape-string-regexp "^1.0.5"
35823590
supports-color "^5.2.0"
35833591

3592+
chalk@~0.4.0:
3593+
version "0.4.0"
3594+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-0.4.0.tgz#5199a3ddcd0c1efe23bc08c1b027b06176e0c64f"
3595+
dependencies:
3596+
ansi-styles "~1.0.0"
3597+
has-color "~0.1.0"
3598+
strip-ansi "~0.1.0"
3599+
35843600
character-entities-html4@^1.0.0:
35853601
version "1.1.1"
35863602
resolved "https://registry.yarnpkg.com/character-entities-html4/-/character-entities-html4-1.1.1.tgz#359a2a4a0f7e29d3dc2ac99bdbe21ee39438ea50"
@@ -8220,6 +8236,10 @@ has-binary2@~1.0.2:
82208236
dependencies:
82218237
isarray "2.0.1"
82228238

8239+
has-color@~0.1.0:
8240+
version "0.1.7"
8241+
resolved "https://registry.yarnpkg.com/has-color/-/has-color-0.1.7.tgz#67144a5260c34fc3cca677d041daf52fe7b78b2f"
8242+
82238243
82248244
version "1.1.0"
82258245
resolved "https://registry.yarnpkg.com/has-cors/-/has-cors-1.1.0.tgz#5e474793f7ea9843d1bb99c23eef49ff126fff39"
@@ -10650,6 +10670,13 @@ jsonify@~0.0.0:
1065010670
version "0.0.0"
1065110671
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
1065210672

10673+
jsonlint@^1.6.3:
10674+
version "1.6.3"
10675+
resolved "https://registry.yarnpkg.com/jsonlint/-/jsonlint-1.6.3.tgz#cb5e31efc0b78291d0d862fbef05900adf212988"
10676+
dependencies:
10677+
JSV "^4.0.x"
10678+
nomnom "^1.5.x"
10679+
1065310680
jsonparse@^1.2.0:
1065410681
version "1.3.1"
1065510682
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
@@ -12518,6 +12545,13 @@ nodemon@^1.11.0:
1251812545
undefsafe "0.0.3"
1251912546
update-notifier "^2.2.0"
1252012547

12548+
nomnom@^1.5.x:
12549+
version "1.8.1"
12550+
resolved "https://registry.yarnpkg.com/nomnom/-/nomnom-1.8.1.tgz#2151f722472ba79e50a76fc125bb8c8f2e4dc2a7"
12551+
dependencies:
12552+
chalk "~0.4.0"
12553+
underscore "~1.6.0"
12554+
1252112555
1252212556
version "0.0.0"
1252312557
resolved "https://registry.yarnpkg.com/noms/-/noms-0.0.0.tgz#da8ebd9f3af9d6760919b27d9cdc8092a7332859"
@@ -17195,6 +17229,10 @@ strip-ansi@^4.0.0:
1719517229
dependencies:
1719617230
ansi-regex "^3.0.0"
1719717231

17232+
strip-ansi@~0.1.0:
17233+
version "0.1.1"
17234+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-0.1.1.tgz#39e8a98d044d150660abe4a6808acf70bb7bc991"
17235+
1719817236
strip-bom-stream@^1.0.0:
1719917237
version "1.0.0"
1720017238
resolved "https://registry.yarnpkg.com/strip-bom-stream/-/strip-bom-stream-1.0.0.tgz#e7144398577d51a6bed0fa1994fa05f43fd988ee"
@@ -18229,6 +18267,10 @@ underscore@^1.7.0:
1822918267
version "1.8.3"
1823018268
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.8.3.tgz#4f3fb53b106e6097fcf9cb4109f2a5e9bdfa5022"
1823118269

18270+
underscore@~1.6.0:
18271+
version "1.6.0"
18272+
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.6.0.tgz#8b38b10cacdef63337b8b24e4ff86d45aea529a8"
18273+
1823218274
unherit@^1.0.4:
1823318275
version "1.1.0"
1823418276
resolved "https://registry.yarnpkg.com/unherit/-/unherit-1.1.0.tgz#6b9aaedfbf73df1756ad9e316dd981885840cd7d"

0 commit comments

Comments
 (0)