mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Add option to control module exports conversion
This commit is contained in:
parent
58dd180def
commit
ab481a497b
1 changed files with 37 additions and 22 deletions
|
@ -188,27 +188,8 @@ Base.exports.PaperScript = function() {
|
||||||
code = code.substring(0, start) + str + code.substring(end);
|
code = code.substring(0, start) + str + code.substring(end);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursively walks the AST and replaces the code of certain nodes
|
function handleOverloading(node, parent) {
|
||||||
function walkAST(node, parent) {
|
switch (node.type) {
|
||||||
if (!node)
|
|
||||||
return;
|
|
||||||
// The easiest way to walk through the whole AST is to simply loop
|
|
||||||
// over each property of the node and filter out fields we don't
|
|
||||||
// need to consider...
|
|
||||||
for (var key in node) {
|
|
||||||
if (key === 'range' || key === 'loc')
|
|
||||||
continue;
|
|
||||||
var value = node[key];
|
|
||||||
if (Array.isArray(value)) {
|
|
||||||
for (var i = 0, l = value.length; i < l; i++)
|
|
||||||
walkAST(value[i], node);
|
|
||||||
} else if (value && typeof value === 'object') {
|
|
||||||
// We cannot use Base.isPlainObject() for these since
|
|
||||||
// Acorn.js uses its own internal prototypes now.
|
|
||||||
walkAST(value, node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
switch (node.type) {
|
|
||||||
case 'UnaryExpression': // -a
|
case 'UnaryExpression': // -a
|
||||||
if (node.operator in unaryOperators
|
if (node.operator in unaryOperators
|
||||||
&& node.argument.type !== 'Literal') {
|
&& node.argument.type !== 'Literal') {
|
||||||
|
@ -291,6 +272,11 @@ Base.exports.PaperScript = function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleExports(node) {
|
||||||
|
switch (node.type) {
|
||||||
case 'ExportDefaultDeclaration':
|
case 'ExportDefaultDeclaration':
|
||||||
// Convert `export default` to `module.exports = ` statements:
|
// Convert `export default` to `module.exports = ` statements:
|
||||||
replaceCode({
|
replaceCode({
|
||||||
|
@ -328,6 +314,35 @@ Base.exports.PaperScript = function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Recursively walks the AST and replaces the code of certain nodes
|
||||||
|
function walkAST(node, parent, paperFeatures) {
|
||||||
|
if (node) {
|
||||||
|
// The easiest way to walk through the whole AST is to simply
|
||||||
|
// loop over each property of the node and filter out fields we
|
||||||
|
// don't need to consider...
|
||||||
|
for (var key in node) {
|
||||||
|
if (key !== 'range' && key !== 'loc') {
|
||||||
|
var value = node[key];
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
for (var i = 0, l = value.length; i < l; i++) {
|
||||||
|
walkAST(value[i], node, paperFeatures);
|
||||||
|
}
|
||||||
|
} else if (value && typeof value === 'object') {
|
||||||
|
// Don't use Base.isPlainObject() for these since
|
||||||
|
// Acorn.js uses its own internal prototypes now.
|
||||||
|
walkAST(value, node, paperFeatures);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (paperFeatures.operatorOverloading !== false) {
|
||||||
|
handleOverloading(node, parent);
|
||||||
|
}
|
||||||
|
if (paperFeatures.moduleExports !== false) {
|
||||||
|
handleExports(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Source-map support:
|
// Source-map support:
|
||||||
// Encodes a Variable Length Quantity as a Base64 string.
|
// Encodes a Variable Length Quantity as a Base64 string.
|
||||||
// See: https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/
|
// See: https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/
|
||||||
|
@ -411,7 +426,7 @@ Base.exports.PaperScript = function() {
|
||||||
ranges: true,
|
ranges: true,
|
||||||
preserveParens: true,
|
preserveParens: true,
|
||||||
sourceType: 'module'
|
sourceType: 'module'
|
||||||
}));
|
}), null, paperFeatures);
|
||||||
}
|
}
|
||||||
if (map) {
|
if (map) {
|
||||||
if (offsetCode) {
|
if (offsetCode) {
|
||||||
|
|
Loading…
Reference in a new issue