Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are loading a JavaScript ES6 module via <script src=""> and importing all exports in a <script> tag the same?

Can't understand the difference between the two examples that I just read at the end of the Deno manual section on the deno bundle command:

Bundles can also be loaded in the web browser. The bundle is a self-contained ES module, and so the attribute of type must be set to "module". For example:

<script type="module" src="website.bundle.js"></script>

Or you could import it into another ES module to consume:

<script type="module">
  import * as website from "website.bundle.js";
</script>

I was under the impression that both forms achieve the same effect (i.e., "fetched and executed immediately, before the browser continues to parse the page"), and the latter is used when a script follows or one wants to narrow down what is imported (e.g., as seen in this answer).

  • Section 16.6.1.2 Modules of the Exploring ES6 book appears to agree with this assessment.

  • Reddit thread Difference Es6 import module vs script src="" also seems to confirm this: "Instead of dumping an entire library into your global scope you an instead only include what you need and actually use."


This may be considered a duplicate of other questions (see bottom for the list) but those answers didn't help me much, and the ancillary sources also didn't seem to reveal if my assumption is correct. (On the other hand, it is more than possible that I overlooked something obvious and would have to work on my reading comprehension skills...)

  • Load javascript as an ES6 module OR via a script tag in the page (tangentially related)
  • What is the difference between a script tag with src and a script tag that requires a module? (looked promising but not about ES6 modules...)
  • Classic scripts v/s module scripts in Javascript
  • WHATWG: Adding JavaScript modules to the web platform
  • Import js from script tag in HTML file. Possible?
  • What is the difference between importing js library or reference it in html <script> tag (not about ES6 modules)
  • Should I reference files in a `script` tag when using ES6 modules in a browser
  • ES6 import vs <script src> in html
like image 203
toraritte Avatar asked Apr 04 '21 22:04

toraritte


People also ask

Does ES6 import export?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

How do ES6 modules work?

A module is nothing more than a chunk of JavaScript code written in a file. By default, variables and functions of a module are not available for use. Variables and functions within a module should be exported so that they can be accessed from within other files. Modules in ES6 work only in strict mode.

Is module exports ES6?

All CommonJS and AMD modules are presented to ES6 as having a default export, which is the same thing that you would get if you asked require() for that module—that is, the exports object. ES6 modules were designed to let you export multiple things, but for existing CommonJS modules, the default export is all you get.

What is ES6 import and export in JavaScript?

ES6 | Import and Export. The ES6 is a JavaScript standard. With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import.

How to create modules in JavaScript with ES6?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

How to import a JavaScript script into an HTML file?

First of all, you need to include type="module" in the <script> element, to declare this script as a module. To import the main.js script, we use this: < script type = " module " src = " main.js " > </ script > You can also embed the module's script directly into the HTML file by placing the JavaScript code within the body of the <script> element:

What is the import and export syntax in JavaScript?

Basically the import and export syntax is used everywhere where we write JavaScript and then transcompile and bundle it to “old-school” javascript. But the time when it can only be used in conjunction with compilers like Babel is over. Meanwhile Node.js also supports the so-called ES6 modules, and in the browser we can use them if we want to.


1 Answers

I was under the impression that both forms achieve the same effect

Yes, both of these will have the same effect

(i.e., "fetched and executed immediately, before the browser continues to parse the page"),

No, that any <script with type="module" will defer by default, so the loading will not block parsing. All deferred scripts are then executed in the order they appear, after parsing and before DOMContentLoaded fires.

and the latter is used when a script follows or one wants to narrow down what is imported (e.g., as seen in this answer).

Which one you want to use also depends on what work is done in the bundle. If the bundle only contains libraries, and doesn't create any side effects (ie, interacting with the page, rendering, etc) then you will probably want to import it so that you can use the functions.

If it does have side-effects (ie. a react app that renders to the DOM), and is self-contained, then just including the tag will be enough to get it started

like image 131
coagmano Avatar answered Oct 16 '22 17:10

coagmano