chriszarate/bookmarkleter

BUG: "Wrap in an IIFE (anonymizing function)." fails if you end with // comment (should be easy fix)

Closed this issue · 7 comments

Looks like the function wrapper code simply needs a carriage return before appending this string: "}();"

For example if my code is this:

var x;
var foo = bar?.baz ?? x;
//

I get this red box error:

unknown: Unexpected token (3:6) 1 | void function () {var x; 2 | var foo = bar?.baz ?? x; > 3 | //}(); | ^

Up until now, personally as a workaround I would just always add "[new line]/**/" to the end of my input

[\n]/**/

But obviously this shouldn't be needed; seems a simple-to-fix issue,
as it appears to be happening due to how the "void function () {" ... wrapper string closes.

I suspect all that is needed to fix is just appending "\n}();" instead of "}();" -- I will see if I can find the exact line myself and create a PR if possible.

Just need to pass comments: false to babel-minify. I can do that in a bit

No need to do anything different with babel-minify -- it was just a string correction, see my proposed commit 74e6e80

Removing comments is a better approach

TBH both would be ideal -- I know that when I looked into the node:require, the Module.wrapper array ends with "\n}();" instead of "}();" so it seems they do that to handle ending comments just like this.

Just need to pass comments: false to babel-minify. I can do that in a bit

https://babeljs.io/docs/babel-minify says comments: [Function | RegExp | Boolean]

So not even sure if you can use value "false" -- seems like by default all comments will be omitted, and I'm guessing this "comments" override lets you KEEP comments just for those 3 particular cases?

UPDATE: the source code seems to imply the default is nuke ALL comments other than a match to this RegExp:
/^\**!|@preserve|@licen[sc]e|@cc_on/
https://github.com/babel/minify/blob/master/packages/babel-minify/src/index.js#L15
So it should have automatically removed that ending // comment by default.

Default is to preserve comments per this line:

https://github.com/babel/minify/blob/master/packages/babel-minify/src/index.js#L51

Via the same line, passing false removes them

Thanks, looks like the comments: false commit makes it so it will always remove comments now -- even if not wrapped in anonymous IIFE.
I didn't realize until now that the previous version had that same functionality, AKA I thought it kept the comments by default, only removing them if wrapped in IIFE.
Sorry for being such a pain today, have a happy Friday!