Parcel minimizes JavaScript in script tags by renaming functions, but doesn't change the original function names in fields like oninput.
parcel build index.html
Minimize JavaScript and rename exampleFunction to a. Rename references to exampleFunction to a as well.
Just minimizes exampleFunction, not any references. This causes errors saying how that function doesn't exist.
I defined a function in a script tag that I wanted to refer to later in oninput fields on HTML tags. Since this doesn't work, I can't minimize my JavaScript with Parcel.
<!DOCTYPE html>
<html>
<head>
<script>
function exampleFunction(string) {
console.log(string.toUpperCase());
}
</script>
</head>
<body>
<input type="text" oninput="exampleFunction(this.value)">
</body>
</html>
Open up the web inspector console and type some characters in the text box.
Now do the same with the result of parcel build index.html
Here's the compressed code from parcel (I beautified it):
<!DOCTYPE html>
<html>
<head>
<script>function o(o){console.log(o.toUpperCase())}</script>
</head>
<body> <input type="text" oninput="exampleFunction(this.value)"> </body>
</html>
As you can see, the oninput field was left unchanged, causing it to error.
| Software | Version |
| ---------------- | ---------- |
| Parcel | 1.10.1|
I just briefly scanned over the source of parcel and I think it does not process inline javascript expressions put inside tags like <a> or <input> inside an html asset. It seems only to look for script tags inside html assets. Maybe I will have a deeper look when I have some time.
However I found a temporary workaround, which is to just explicitly attach it to window (or any other global, according to your environment):
<!DOCTYPE html>
<html>
<head>
<script>
window.exampleFunction = function exampleFunction(string) {
console.log(string.toUpperCase());
}
</script>
</head>
<body>
<input type="text" onInput="exampleFunction(this.value)">
</body>
</html>
parcel build minifies)<!DOCTYPE html>
<html>
<head>
<script>
window.exampleFunction = function(o) {
console.log(o.toUpperCase())
}
;
</script>
</head>
<body>
<input type="text" oninput="exampleFunction(this.value)">
</body>
</html>
Now it works!
Hope the workaround helps you.
Issue is still present in Parcel v1.12.4
Issue is still present.
@pizzafox I assume this has actually been fixed in Parcel 2 as we now process inline scripts properly
Issue is still present in Parcel v2.0.0-nightly.339+1f558665.
The output has now changed to the following (formatted):
<!DOCTYPE html>
<html>
<head>
<script>
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6IiIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwidmVyc2lvbiI6MywiZmlsZSI6ImluZGV4LkhBU0hfUkVGXzA1ZmIzNmVkNWI2YTU1NTFmNWQ2OTk1YmU4OTI2YzE3LmpzLm1hcCJ9
</script>
</head>
<body>
<input type="text" oninput="exampleFunction(this.value)" />
</body>
</html>
Most helpful comment
I just briefly scanned over the source of parcel and I think it does not process inline javascript expressions put inside tags like
<a>or<input>inside an html asset. It seems only to look forscripttags insidehtmlassets. Maybe I will have a deeper look when I have some time.However I found a temporary workaround, which is to just explicitly attach it to
window(or any other global, according to your environment):Workaround code
Transformed code (I beautified it because
parcel buildminifies)Now it works!
Hope the workaround helps you.