Join the DZone community and get the full member experience.Join For Free
Separate and Abstract code in its own scope, making it accessible and included again!
If you are a C# developer or a Java developer, we normally break down our code in different classes/files and then import those classes/files to another class (e.g. via a
using statement in C#), and with that, the functionality is available in other classes to be used.
This way, we can structure and organize our code in a cleaner manner. This also promotes reusability, separation of concerns, etc.
Module System Depending on Environment
- Depending on the environment you are in (in a browser, or in a Node application) modules can and have to be written in many different systems, so that it can be used in many different environments.
- Each module system describes a different format in a way that code separation can be created (CommonJs, AMD, ESM).
Module Formats vs Loaders
- Module format is really just the syntax used to define the module. Module Format -> Syntax.
- Using a particular module-format will also require a compatible module-loader. Not all module loaders support all module formats.
AMD vs ComonJS vs ESM
AMD and CommonJS are the popular module formats, and the following details describe their general characteristics, However, the one which we are interested in is ECMAScript modules, and we will use it in conjunction with TypeScript. For AMD and CommonJS, you can search online for details; I just mentioned those here because you will most likely read about those in various other blog posts or articles.
Here is a code example of writing a basic module in typescript and then consuming this module in another module.
Also, the TypeScript website will show you how to set up your development environment and some basic examples as well. In the next post, we will be starting our journey from this point onward. Happy Coding.
Published at DZone with permission of Jawad Hasan Shani. See the original article here.
Opinions expressed by DZone contributors are their own.