Modules 1/2

Mohammed Ali - Sep 2 - - Dev Community
  • Now we don't write all JS in a single file and send to the client.
  • Today, we write code into modules which share data between them and are more maintainable.
  • Convention is to use camelCase names for Modules.
  • We can even include 3rd party modules into our own code via npm repos like jquery, react, webpack, babel etc.
  • A final bundle is built from small module files which is deployed to prod server, eventually being sent to clients.
  • Modules are not supported by older browsers
  • For perf reasons, its better to send small js files to browser.
  • Modules ar super-important part of JS, already used in other languages for decades by devs.
  • Modules: reusable piece of code, which encapsulates implementation details of a certain part of our project.
  • Its a standalone file, but it doesn't have to be.
  • Modules can also have imports & exports Native ES6 Modules: There were no modules before ES6. Had to be implemented ourselves or use external libararies. Modules are stored in files, exactly one module per file.
  • We can export values, functions out of modules etc. Whatever we export is called a public API which is exposed for other code to consume.
  • This public API is consumed by importing public code into module and are called dependencies.
  • Simple applns can be written without modules also, but for enterprise scale projects we need modules.
  • Adv:
    -> Makes really easy to compose software as modules are small building blocks that we put together to build complex applications.
    -> Isolating Components: Each feature can be developed in complete isolation and without worrying about the work of other devs or how the entire system works.
    -> Code abstraction: Implement low level code in modules, import these abstractions into other modules, naturally leading to a more organized codebase.
    -> Code reusability: Allow us to reuse code even across multiple projects.

  • Modern JS uses bundling & transpiling.

## Bundling = is a complex process which involves:
a. Eliminate unused Code
b. Combine all modules into single file.
c. Compress the code.
Ex. webpack build tool - requires manual config, hence complex.
Ex. parcel - zero build tool as we don't need to write any setup code.

## Transpiling/Polyfiling:
Convert modern JS to ES5 or older syntax such that older browser supports the application. 
Ex. done using tool called Babel

Entire process results in the final bundle file for production ready to be deployed on server. 
Enter fullscreen mode Exit fullscreen mode
### Scripts are also files but they are different from ES6 modules in the following ways:
Script[used earlier]:
- All top level variables are always global. This leads to global namespace pollution and name-coliision.
- Default mode: Sloppy mode
- Top level 'this' points to window object.
- Import/Export of values is not allowed.
- Linked to html using plain script tag.
- Downloaded by default in sync way, unless async or defer attribute is used.

### ES6 Modules:
- All top-level variables are scoped to module i.e they are private by default. Such a value can be access by outside varible only when its exported by the module else it will be inaccessible for outside world.
- Default mode: Strict mode. Hence with modules no more need to declare strict mode.
- Top level 'this' is 'undefined'
- Allows import/export of values using ES6 syntax of import/export.
- Link to html file using <script type="module"> tag.
- File download always happens in an async way, i.e for module loaded from html or an import one module to another.
Enter fullscreen mode Exit fullscreen mode
## Understanding module import process:
Parsing -> [Asyn module download, Linking import-export, Execute individual module itself] -> Execution overall index.js
- import/export only need to happen at the top level of if-block or any function. Imports are hoisted. Importing values is always the first thing which happens in a module.
- Top-level static imports make imports known before execution. 
- Modules are loaded in async way from the server, importing happens in sync manner. 
- Parsing refers to reading the code without executing it in which imports are hoisted. Modules are imported even before execution. Hence, it enables bundling & code elimation even before fn exection. [V Impt as large projects have 100s of modules, and 3rd party modules also from which we want the small piece and not the entire module]
- Bundlers can then join multiple modules, and then eliminate code. hence, we can easily import/export from code.
- After a module arrives, its parsed and then the module exports are linked to imports in index.js i.e live connection is established between import inside index.js and export statement of the module file. They are not copied. Import is just a reference to the exported value. Hence, when the value changes in the exporting module, it also changes in the importing module too. This concept is unique to ES6 modules, other module system don't work like this.
- Code in the imported modules is executed.
Enter fullscreen mode Exit fullscreen mode

Import-Export-JS1

Import-Export-JS2

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player