-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Refactor tree-shaking rendering #1949
Merged
Merged
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4b9dfc3
Make BlockStatements take care of removing their children
lukastaegert 2436ee1
Extract BlockStatement rendering to be used for Program nodes with
lukastaegert 48f4683
Use new logic for switch statements (which works much better than the
lukastaegert 152eb29
* Properly handle multi-line comments after statements
lukastaegert eae7d96
Refactor and simplify default export rendering
lukastaegert c345fb5
Clean up code, improve performance and resolve #1850
lukastaegert 7781b5f
Resolve #1772
lukastaegert 698f3a2
Remove "statement" concept and other remains of old positioning logic
lukastaegert a395faf
When checking for symbols other than line-breaks, check for both kinds
lukastaegert de9acd0
First draft for variable declarations
lukastaegert ed77a00
* Simplify declaration rendering logic
lukastaegert d2720f6
Prevent trailing white-space
lukastaegert 6bb3829
Remove line-breaks after declaration key-word
lukastaegert 2a570a2
Resolve #1937
lukastaegert 3588a4a
Resolve #1831
lukastaegert 9ff220f
Resolve #1943
lukastaegert 6b30972
Include line-breaks with the previous statement instead of the
lukastaegert 103e16f
Rework declaration handling to associate comments after a comma but
lukastaegert 377fe90
Fix TypeScript test
lukastaegert 495f97a
* Improve some names
lukastaegert acaea3d
* Reintroduce statement concept as an alias
lukastaegert d45a74c
Reduce number of necessary slices by supplying start indices
lukastaegert adeebc2
Only calculate boundaries if necessary
lukastaegert 8ecadf2
Store flags for AST nodes directly on the prototype
lukastaegert 114149f
Improve variable declaration performance
lukastaegert 22aa1fb
* Use charCodeAt where possible to improve performance
lukastaegert bb8b81d
Slightly clean up source map removal
lukastaegert File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -3,10 +3,10 @@ import { UNKNOWN_EXPRESSION } from '../values'; | |
import ExecutionPathOptions from '../ExecutionPathOptions'; | ||
import Scope from '../scopes/Scope'; | ||
import MagicString from 'magic-string'; | ||
import { Node } from './shared/Node'; | ||
import { StatementBase, StatementNode } from './shared/Statement'; | ||
import { Node, StatementBase, StatementNode } from './shared/Node'; | ||
import { NodeType } from './NodeType'; | ||
import { RenderOptions } from '../../Module'; | ||
import { renderStatementList } from '../../utils/renderHelpers'; | ||
|
||
export function isBlockStatement (node: Node): node is BlockStatement { | ||
return node.type === NodeType.BlockStatement; | ||
|
@@ -48,12 +48,8 @@ export default class BlockStatement extends StatementBase { | |
} | ||
|
||
initialiseChildren (_parentScope: Scope) { | ||
let lastNode; | ||
for (const node of this.body) { | ||
node.initialise(this.scope); | ||
|
||
if (lastNode) lastNode.next = node.start; | ||
lastNode = node; | ||
} | ||
} | ||
|
||
|
@@ -63,9 +59,7 @@ export default class BlockStatement extends StatementBase { | |
|
||
render (code: MagicString, options: RenderOptions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should make |
||
if (this.body.length) { | ||
for (const node of this.body) { | ||
node.render(code, options); | ||
} | ||
renderStatementList(this.body, code, this.start + 1, this.end - 1, options); | ||
} else { | ||
super.render(code, options); | ||
} | ||
|
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're no longer using
this.comments
does that mean this property can be removed entirely, including the use of theonComment
hook?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tested this and the answer is yes :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not as it is still required to remove source map comments. This, however, is the only reason why we keep this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh. Can you point me to the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe at some point we should implement our own algorithm to scan for and remove those comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. A sourceMappingURL comment can only be a block comment so it's fine to do something custom. Also it can be based on
lastIndexOf
as a backwards search through the string would be quicker.Here's the code I have used for this previously - https://github.com/systemjs/builder/blob/master/lib/sourcemaps.js#L12.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this example, maybe the issue is not that trivial and we should probably rather trust acorns parsing here and leave it as it is:
rollup/test/form/samples/removes-existing-sourcemap-comments/main.js
Line 3 in 22aa1fb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise we might break the sourceMap handling of other libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's v8's method to do this - https://github.com/v8/v8/blob/8d38c15e04598f9e48e6230327da0e41a2445957/src/inspector/search-util.cc#L16
Note that it doesn't use a lexer at all.
I think it would be much simpler to make this code its own method and deprecate
this.comments
entirely. But happy to make this a separate PR too if you prefer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this further detection is a less stringent problem than removal - and that’s what I got confused here. It makes sense that Rollup has to hold to a higher standard for removal not to break code.
Perhaps we can inline the removal into the parsing code without separately gathering the comments at least.