Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary babel polyfills and fix problem with nested worklets. #882

Merged
merged 6 commits into from
Jun 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix plugin
  • Loading branch information
szymon committed Jun 10, 2020
commit a4c5075ecdcb8028488dcfe57b1aa44573a16c7c
88 changes: 62 additions & 26 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ const globals = new Set([
'UIManager',
'requestAnimationFrame',
'_WORKLET',
'arguments',
'_log',
'_updateProps',
'RegExp',
'Error',
]);

function buildWorkletString(t, fun, closureVariables) {
Expand All @@ -52,7 +54,7 @@ function buildWorkletString(t, fun, closureVariables) {
t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern(
closureVariables.map(variable =>
closureVariables.map((variable) =>
t.objectProperty(
t.identifier(variable.name),
t.identifier(variable.name),
Expand Down Expand Up @@ -171,7 +173,7 @@ function processWorkletFunction(t, fun) {
false
),
t.objectExpression(
variables.map(variable =>
variables.map((variable) =>
t.objectProperty(
t.identifier(variable.name),
variable,
Expand Down Expand Up @@ -238,10 +240,6 @@ function processWorkletFunction(t, fun) {
function processIfWorkletNode(t, path) {
const fun = path;

if (path.node.type === 'FuncionDeclaration' && path.node.id.name === 'siakaka') {
console.log(fun.toString());
}

fun.traverse({
DirectiveLiteral(path) {
const value = path.node.value;
Expand All @@ -254,52 +252,90 @@ function processIfWorkletNode(t, path) {
directives &&
directives.length > 0 &&
directives.some(
directive =>
(directive) =>
t.isDirectiveLiteral(directive.value) &&
directive.value.value === 'worklet'
)
) {
processWorkletFunction(t, fun)
processWorkletFunction(t, fun);
}
}
},
});
}

module.exports = function({ types: t }) {
function processWorklets(t, path, processor) {
const name = path.node.callee.name;
if (
objectHooks.has(name) &&
path.get('arguments.0').type === 'ObjectExpression'
) {
const objectPath = path.get('arguments.0.properties.0');
for (let i = 0; i < objectPath.container.length; i++) {
processor(t, objectPath.getSibling(i).get('value'));
}
} else if (functionHooks.has(name)) {
processor(t, path.get('arguments.0'));
}
}

function removeWorkletLabelFromSubtrees(path) {
const parentFunction = path.getFunctionParent();
if (parentFunction != null) {
parentFunction.traverse({
Directive(innerPath) {
if (
innerPath.node.value != path.node &&
innerPath.node.value.value === 'worklet'
) {
innerPath.remove();
}
},
});
}
}

module.exports = function ({ types: t }) {
return {
visitor: {
CallExpression: {
enter(path) {
if (path.get('callee').matchesPattern('Object.assign')) {
// @babel/plugin-transform-object-assign
path.node.callee.object.name = 'random_temp_name';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename to something more descriptive. For example: "Object__DO_NOT_TRANSFORM"

}
},
exit(path) {
const name = path.node.callee.name;
if (
objectHooks.has(name) &&
path.get('arguments.0').type === 'ObjectExpression'
) {
const objectPath = path.get('arguments.0.properties.0');
for (let i = 0; i < objectPath.container.length; i++) {
processWorkletFunction(t, objectPath.getSibling(i).get('value'))
}
} else if (functionHooks.has(name)) {
processWorkletFunction(t, path.get('arguments.0'))
if (path.get('callee').matchesPattern('random_temp_name.assign')) {
// @babel/plugin-transform-object-assign
path.node.callee.object.name = 'Object';
}
}

processWorklets(t, path, processWorkletFunction);
},
},
FunctionDeclaration: {
exit(path) {
processIfWorkletNode(t, path);
}
},
},
FunctionExpression: {
exit(path) {
processIfWorkletNode(t, path);
}
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put all of these under a single visitor the way it used to be:

'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression': {
   enter(path) { ... }

Copy link
Contributor Author

@Szymon20000 Szymon20000 Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

},
ArrowFunctionExpression: {
exit(path) {
processIfWorkletNode(t,path);
}
}
processIfWorkletNode(t, path);
},
},
DirectiveLiteral: {
enter(path) {
if (path.node.value === 'worklet') {
removeWorkletLabelFromSubtrees(path);
}
},
},
},
};
};