HtmlUnit / htmlunit

HtmlUnit is a "GUI-Less browser for Java programs".
https://www.htmlunit.org
Apache License 2.0
854 stars 168 forks source link

[js-core] Support of ECMA6 Javascript modules #1

Open hadriensz opened 5 years ago

hadriensz commented 5 years ago

copy of stackoverflow question:

https://stackoverflow.com/questions/52240645/htmlunit-cannot-process-javascript-module?noredirect=1#comment91433014_52240645

When parsing a complex web page with HTMLUnit, with javascript enabled, it refuses to process type="module" scripts. Looking at the source code of HtmlScript it looks like scripts of any type other than "text/javascript" || « text/ecmascript » or « application/javascript" || « application/ecmascript" || "application/x-javascript" are rejected Error is: com.gargoylesoftware.htmlunit.html.HtmlScript isExecutionNeeded WARNING: Script is not JavaScript (type: module, language: ). Skipping execution. Is there a way to have module scripts also executed ? Thanks

To this question RBRi suggested :

Please file a bug report for HtmlUnit or even better provide a patch (github.com/HtmlUnit/htmlunit)

Which I'm doing...

Justification (source : https://spring.io/understanding/javascript-modules, 2015)

In JavaScript, the word "modules" refers to small units of independent, reusable code. They are the foundation of many JavaScript design patterns and are critically necessary when building any non-trivial JavaScript-based application. ECMAScript, the JavaScript standards body, expects to ratify a final specification for modules in ECMAScript version 6 by the end of 2014. ECMAScript 6 modules ("ES6 modules"), however, will likely not be feasible to use in production until 2016 due to the long upgrade cycles of some browsers and operating systems

Complete specification can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

and is discussed here: https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ http://exploringjs.com/es6/ch_modules.html

additionnal considerations:

(1) it should be considered whether to support only ECMA6 or also previous common module technologies such as Asynchronous Module Definition (AMD), node.js / CommonJS Modules or Universal Module Definition (UMD). All of there could be considered as de facto module standards

(2) maybe this issue / feature should be moved to core-js github community. It looks like support for modules (only AMD probably) is already implemented there but not ported to HTMLUnit

(3) since ECMA6 appeared Oracle developed Nashhorn javascript engine. Is there a plan to support Nashhorn if it provides better ECMA6 support and more frequent updates than Rhino ?

niloc132 commented 4 years ago

(3) since ECMA6 appeared Oracle developed Nashhorn javascript engine. Is there a plan to support Nashhorn if it provides better ECMA6 support and more frequent updates than Rhino ?

Possibly worth updating, Nashhorn has now been removed from the JRE - http://openjdk.java.net/jeps/335.

OluwoleOyetoke commented 4 years ago

Any clues on possible workaround for this issue or will there be a patch in the near future?

Jaaap commented 1 year ago

If i read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules and more specifically https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts correctly, it should be pretty straight forward to implement <script type="module"> and import and export.

The main differences will be in the scoping of individual JS files, which i assume is do-able?

I would help our project very much if this feature was added to HtmlUnit.

scottpettyjohn commented 10 months ago

Any movement on this?

rbri commented 10 months ago

@scottpettyjohn sorry not really. Have done a lot of work on Rhino during the last months but there is much more to do. And please keep in mind that i'm doing all the stuff in my spare time.

There is a plan to be more independent from core-js and then try to use another more recent js engine. But this is only a plan - maybe i will start on this at the beginning of next year.

rbri commented 5 months ago

see #755