-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chore) - remove closure-compiler (#1570)
* remove closure-compiler, the current rollup plugin isn't really maintained and we were seeing frequent issues with metro and bundles compiled by closure * add changeset * Update curvy-bobcats-fry.md * Remove hoist options from Terser * Remove version from ephemeral package.json * Remove babel-plugin-closure-elimination * Add transformation to clean up function expressions Buble/Babel (and transformers in general) like to transform arrow functions to function expressions on variable declarations. At the toplevel of modules we can clean this up by replacing them with function declarations, which leads to cleaner minifier output. * Expand function expression transformer to more safe cases Co-authored-by: Phil Pluckthun <[email protected]>
- Loading branch information
1 parent
398007e
commit 25e6c5b
Showing
7 changed files
with
135 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
'@urql/exchange-auth': patch | ||
'@urql/exchange-execute': patch | ||
'@urql/exchange-graphcache': patch | ||
'@urql/exchange-multipart-fetch': patch | ||
'@urql/exchange-persisted-fetch': patch | ||
'@urql/exchange-populate': patch | ||
'@urql/exchange-refocus': patch | ||
'@urql/exchange-request-policy': patch | ||
'@urql/exchange-retry': patch | ||
'@urql/exchange-suspense': patch | ||
'@urql/core': patch | ||
'@urql/introspection': patch | ||
'next-urql': patch | ||
'@urql/preact': patch | ||
'urql': patch | ||
'@urql/storybook-addon': patch | ||
'@urql/svelte': patch | ||
'@urql/vue': patch | ||
--- | ||
|
||
Remove closure-compiler from the build step |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** Babel plugin for cleaning up arrow function transpilation, which turns function expressions assigned to variable decalators into function declarations when it's safe to do so. */ | ||
const functionExpressionCleanup = ({ types: t }) => { | ||
/** Checks whether this block has only safe conditions up until the given node. */ | ||
const isSafeUntil = (block, until) => { | ||
let body = []; | ||
if (t.isIfStatement(block)) { | ||
body = block.consequent; | ||
if (block.alternate && !isSafeUntil(block.alternate, until)) { | ||
return false; | ||
} | ||
} else if (t.isBlockStatement(block)) { | ||
body = block.body; | ||
} | ||
|
||
for (let i = 0, l = body.length; i < l; i++) { | ||
let node = body[i]; | ||
if (t.isIfStatement(node)) { | ||
// An if statement is safe if it also is safe throughout | ||
if (!isSafeUntil(node, until)) return false; | ||
} else if ( | ||
!t.isVariableDeclaration(node) && | ||
!t.isFunctionDeclaration(node) && | ||
!(t.isExpressionStatement(node) && t.isAssignmentExpression(node.expression)) | ||
) { | ||
// only variable declarations and function declarations are safe | ||
// assignments are fine too, since we're later checking the binding for "constantViolations" | ||
return false; | ||
} else if (node === until) { | ||
return true; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
return { | ||
visitor: { | ||
FunctionExpression(path) { | ||
if (!t.isVariableDeclarator(path.parent)) { | ||
// Must be on a variable declarator | ||
return; | ||
} | ||
|
||
if ( | ||
t.isFunctionDeclaration(path.parentPath.scope.block) || | ||
t.isFunctionExpression(path.parentPath.scope.block) | ||
) { | ||
// When the function expression is nested inside another function, it may be safe | ||
// to turn this into a declaration, if it's only preceded by variable declarations | ||
// and assignments (potentially even nested in if-statements) | ||
if (!isSafeUntil(path.parentPath.scope.block.body, path.parentPath.parent)) | ||
return; | ||
} else if (!t.isProgram(path.parentPath.scope.block)) { | ||
return; | ||
} | ||
|
||
const binding = path.scope.getBinding(path.parent.id.name); | ||
|
||
if ( | ||
(binding.constantViolations && binding.constantViolations.length) || | ||
binding.referencePaths.some(path => | ||
!t.isCallExpression(path.parentPath.node) && | ||
!t.isProgram(path.parentPath.node)) | ||
) { | ||
// The declaration must not be reassigned and it must only be referenced as plain calls | ||
return; | ||
} | ||
|
||
const fn = t.functionDeclaration( | ||
path.parent.id, | ||
path.node.params, | ||
path.node.body, | ||
path.node.generator, | ||
path.node.async, | ||
); | ||
|
||
// We insert after other variable declarators to not rely on hoisting and for readability | ||
path.parentPath.parentPath.insertAfter( | ||
// If the variabe is exported then the function declaration must also be exported. | ||
t.isExportNamedDeclaration(path.parentPath.parentPath.parent) | ||
? t.exportNamedDeclaration(fn) | ||
: fn | ||
); | ||
|
||
if (path.parentPath.parent.declarations.length <= 1) { | ||
path.parentPath.parentPath.remove(); | ||
} else { | ||
path.remove(); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
export default functionExpressionCleanup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.