What’s the purpose of the HTML “nomodule” attribute for script elements if the default is text/javascript?

前端 未结 3 628
囚心锁ツ
囚心锁ツ 2020-12-29 01:22

I am not clearly understanding why the nomodule attribute exists in the new browsers that support ES6 modules.

In HTML 5, the type attribut

3条回答
  •  臣服心动
    2020-12-29 01:33

    nomodule attribute

    As other answers mentioned, This Boolean attribute is set to indicate that the script should not be executed in browsers that support ES2015 modules — in effect, this can be used to serve fallback scripts to older browsers that do not support modular JavaScript code.

    Now let's understand with an example.

    I'm using es6/ES2015 features and need to include core.js to run application in IE browser and other major browser like chrome, firefox, safari are supporting es6/ES2015 features. You can see total vender size will be 150kb for IE browser.

     // around 45kb after gzip
     // around 100kb after gzip 
    

    why should I load core.js & obserpoly.js (45kb+5kb = 50kb) in other browsers. I have used nomodule attribute with my script and it worked like a champ. Now, Core.js and obserPoly.js scripts load in IE browsers only.

     // around 45kb after gzip
     // around 100kb after gzip 
    

提交回复
热议问题