Skip to content

Commit ab481a4

Browse files
committed
Add option to control module exports conversion
1 parent 58dd180 commit ab481a4

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

src/core/PaperScript.js

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,27 +188,8 @@ Base.exports.PaperScript = function() {
188188
code = code.substring(0, start) + str + code.substring(end);
189189
}
190190

191-
// Recursively walks the AST and replaces the code of certain nodes
192-
function walkAST(node, parent) {
193-
if (!node)
194-
return;
195-
// The easiest way to walk through the whole AST is to simply loop
196-
// over each property of the node and filter out fields we don't
197-
// need to consider...
198-
for (var key in node) {
199-
if (key === 'range' || key === 'loc')
200-
continue;
201-
var value = node[key];
202-
if (Array.isArray(value)) {
203-
for (var i = 0, l = value.length; i < l; i++)
204-
walkAST(value[i], node);
205-
} else if (value && typeof value === 'object') {
206-
// We cannot use Base.isPlainObject() for these since
207-
// Acorn.js uses its own internal prototypes now.
208-
walkAST(value, node);
209-
}
210-
}
211-
switch (node.type) {
191+
function handleOverloading(node, parent) {
192+
switch (node.type) {
212193
case 'UnaryExpression': // -a
213194
if (node.operator in unaryOperators
214195
&& node.argument.type !== 'Literal') {
@@ -291,6 +272,11 @@ Base.exports.PaperScript = function() {
291272
}
292273
}
293274
break;
275+
}
276+
}
277+
278+
function handleExports(node) {
279+
switch (node.type) {
294280
case 'ExportDefaultDeclaration':
295281
// Convert `export default` to `module.exports = ` statements:
296282
replaceCode({
@@ -328,6 +314,35 @@ Base.exports.PaperScript = function() {
328314
}
329315
}
330316

317+
// Recursively walks the AST and replaces the code of certain nodes
318+
function walkAST(node, parent, paperFeatures) {
319+
if (node) {
320+
// The easiest way to walk through the whole AST is to simply
321+
// loop over each property of the node and filter out fields we
322+
// don't need to consider...
323+
for (var key in node) {
324+
if (key !== 'range' && key !== 'loc') {
325+
var value = node[key];
326+
if (Array.isArray(value)) {
327+
for (var i = 0, l = value.length; i < l; i++) {
328+
walkAST(value[i], node, paperFeatures);
329+
}
330+
} else if (value && typeof value === 'object') {
331+
// Don't use Base.isPlainObject() for these since
332+
// Acorn.js uses its own internal prototypes now.
333+
walkAST(value, node, paperFeatures);
334+
}
335+
}
336+
}
337+
if (paperFeatures.operatorOverloading !== false) {
338+
handleOverloading(node, parent);
339+
}
340+
if (paperFeatures.moduleExports !== false) {
341+
handleExports(node);
342+
}
343+
}
344+
}
345+
331346
// Source-map support:
332347
// Encodes a Variable Length Quantity as a Base64 string.
333348
// See: https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/
@@ -411,7 +426,7 @@ Base.exports.PaperScript = function() {
411426
ranges: true,
412427
preserveParens: true,
413428
sourceType: 'module'
414-
}));
429+
}), null, paperFeatures);
415430
}
416431
if (map) {
417432
if (offsetCode) {

0 commit comments

Comments
 (0)