Skip to content

Commit aaca7de

Browse files
committed
Make exports in sandbox code no-ops
1 parent 7e09cf7 commit aaca7de

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

code/game_levels.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,5 @@ var GAME_LEVELS = [
136136
" "]
137137
];
138138

139-
if (typeof module != "undefined" && module.exports)
139+
if (typeof module != "undefined" && module.exports && (typeof window == "undefined" || window.exports != exports))
140140
module.exports = GAME_LEVELS;

code/jacques_journal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ const JOURNAL = [
9393

9494
// This makes sure the data is exported in node.js —
9595
// `require('./path/to/jaques_journal.js')` will get you the array.
96-
if (typeof module != "undefined" && module.exports)
96+
if (typeof module != "undefined" && module.exports && (typeof window == "undefined" || window.exports != exports))
9797
module.exports = JOURNAL;

code/scripts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,5 +1117,5 @@ const SCRIPTS = [
11171117

11181118
// This makes sure the data is exported in node.js —
11191119
// `require('./path/to/jaques_journal.js')` will get you the array.
1120-
if (typeof module != "undefined" && module.exports)
1120+
if (typeof module != "undefined" && module.exports && (typeof window == "undefined" || window.exports != exports))
11211121
module.exports = SCRIPTS;

html/js/sandbox.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@
214214
}
215215

216216
win.require = name => this.require(name)
217+
win.module = {exports: {}}
218+
win.exports = win.module.exports
217219
}
218220

219221
resizeFrame() {
@@ -293,6 +295,12 @@
293295
ForInStatement: loop,
294296
WhileStatement: loop,
295297
DoWhileStatement: loop,
298+
CallExpression(node) {
299+
if (node.callee.type == "Identifier" && node.callee.name == "require" &&
300+
node.arguments.length == 1 && node.arguments[0].type == "Literal" &&
301+
typeof node.arguments[0].value == "string" && !dependencies.includes(node.arguments[0].value))
302+
dependencies.push(node.arguments[0].value)
303+
},
296304
ImportDeclaration(node) {
297305
dependencies.push(node.source.value)
298306
let req = "require(" + node.source.raw + ")", text
@@ -318,11 +326,22 @@
318326
}
319327
patches.push({from: node.start, to: node.end, text: text + ";"})
320328
},
321-
CallExpression(node) {
322-
if (node.callee.type == "Identifier" && node.callee.name == "require" &&
323-
node.arguments.length == 1 && node.arguments[0].type == "Literal" &&
324-
typeof node.arguments[0].value == "string" && !dependencies.includes(node.arguments[0].value))
325-
dependencies.push(node.arguments[0].value)
329+
ExportNamedDeclaration(node) {
330+
if (node.source || !node.declaration)
331+
patches.push({from: node.start, to: node.end, text: ""})
332+
else
333+
patches.push({from: node.start, to: node.declaration.start, text: ""})
334+
},
335+
ExportDefaultDeclaration(node) {
336+
if (/Declaration/.test(node.declaration.type)) {
337+
patches.push({from: node.start, to: node.declaration.start, text: ""})
338+
} else {
339+
patches.push({from: node.start, to: node.declaration.start, text: ";("},
340+
{from: node.declaration.end, text: ")"})
341+
}
342+
},
343+
ExportAllDeclaration: function(node) {
344+
patches.push({from: node.start, to: node.end, text: ""})
326345
}
327346
})
328347

@@ -349,7 +368,6 @@
349368
pos = patch.to || patch.from
350369
}
351370
out += code.slice(pos, code.length)
352-
console.log(out)
353371
out += "\n//# sourceURL=code" + randomID()
354372
return {code: (strict ? '"use strict";' : "") + out, dependencies}
355373
}

0 commit comments

Comments
 (0)