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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline Javascript (<script> tag) always get minified in html #4455

Closed
edify17 opened this issue Apr 8, 2020 · 9 comments
Closed

Inline Javascript (<script> tag) always get minified in html #4455

edify17 opened this issue Apr 8, 2020 · 9 comments

Comments

@edify17
Copy link

edify17 commented Apr 8, 2020

馃悰bug report

Inline Javascript (<script> tag) always get minified in entry/index html
Tried according to htmlnano documentation by giving htmlnano key in package.json as

{
      minifyJs: false
}

But Javascript inside script tag in index.html still minifies.

Issue faced is that I want to escape replacement of single quote to double quotes when Parcel Bundles application.

馃 Expected Behavior

There should be a way to escape JS minification in html files or to not touch quotes during minification.

馃槸 Current Behavior

Not able to escape inline JS minification inside html.

馃敠 Context

Came across this PR #1456, which introduced changes for processing inline styles and scripts, is there way to skip inline JS minification or changing quotes during minification.

馃捇 Code Sample

<script>
     var a = JSON.parse('{"a": 1}');
</script>

Converts to

<script>
     var a = JSON.parse("{"a": 1}");
</script>

post bundling.

Which breaks in browser with error Uncaught SyntaxError: missing ) after argument list as string parsing fails.

馃實 Your Environment

Software Version(s)
Parcel 1.12.4
Node 12.5.0
npm/Yarn npm
Operating System macOS
@mischnic
Copy link
Member

mischnic commented Apr 8, 2020

The minifyJs flag doesn't do anything because Parcel processes the inline JS and re-inserts it after htmlnano ran.

Which breaks in browser with error Uncaught SyntaxError: missing ) after argument list

That should definitely not happen.

@mischnic
Copy link
Member

Your code sample works correctly for me.

@edify17
Copy link
Author

edify17 commented Apr 24, 2020

Hi @mischnic, Are you not getting the error in console or Parcel doesn't change the quotes post bundling?

@mischnic
Copy link
Member

With this input

<script>
     var a = JSON.parse('{"a": 1}');
     console.log(a);
</script>

the output is

<script>var a=JSON.parse('{"a": 1}');console.log(a);</script>

@edify17
Copy link
Author

edify17 commented Apr 24, 2020

My exact code is
var config = JSON.parse('{{{configObject}}}');

which converts to
var a=JSON.parse("{{{configObject}}}")

Code inside {{{}}} is handlebars, which is later on parsed by my server. I do not want Parcel to change the quotes and treat {{{configObject}}} as string.

Expected Output:
var a=JSON.parse('{{{configObject}}}')

@mischnic
Copy link
Member

mischnic commented Apr 24, 2020

I see, this is caused by terser, add this .terserrc:

{
	"output": {"quote_style": 3}
}

(https://github.com/terser/terser#output-options)

@edify17
Copy link
Author

edify17 commented Apr 24, 2020

Tried it, doesn't work for me. Does it work for you?

@mischnic
Copy link
Member

Yes: https://github.com/mischnic/parcel-issue-4455.

I do indeed get:

Expected Output: var a=JSON.parse('{{{configObject}}}')

@edify17
Copy link
Author

edify17 commented Apr 24, 2020

Worked after deleting .cache folder and bundling again.
I have witnessed multiple times that changes do not reflect until I build again after deleting .cache folder.

Anyway, Thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants