Modularization

Modularization is a popular technique used in modern programming languages that allows programs to be built from a series of smaller libraries, or modules. Writing programs that use modules encourages programmers to write code that conforms to the design principle called Separation of Concerns. The basic principle of Separation of Concerns is that we should program against a defined interface. This means that, the code that is implementing this interface can be refactored, improved, enhanced, or even completely replaced without the rest of the program being affected. This also helps when testing our code, since the code that is providing the implementation of an interface can easily be stubbed, or mocked out in a test scenario.

JavaScript, prior to ECMAScript 6, did not have a concept of modules. Popular frameworks and libraries, such as Node and Require, implemented their own module loading syntax libraries to fill this gap. Unfortunately, two different approaches to module loading, and in particular the module loading syntax, were adopted by the JavaScript community. These two syntax styles were known as CommonJS (used in Node), and AMD, or Asynchronous Module Definition (used in Require). Fortunately, TypeScript has always supported both CommonJS and AMD module syntax.

Now that the ECMAScript 6 module syntax has been published, TypeScript has adopted and implemented it, and will automatically generate the correct module syntax for either CommonJS or AMD, based on a single compiler option.

In this chapter, we will explore what modules are, take a look at the ECMAScript 6 syntax for modules, and highlight the differences between CommonJS and AMD module syntax. We will then take a closer look at how Require uses AMD module syntax, and how SystemJS allows for using CommonJS module syntax in a browser. We will then explore CommonJS modules with regard to Node, and build a simple Node application using the Express framework. Following on from building a Node application, we will explore the world of cloud providers, and show how to build and deploy an application that does not need a server in order to run.

We will be covering the following topics in this chapter:

  • Module basics
  • AMD module loading
  • SystemJS module loading
  • Using Express with Node
  • Amazon Web Services Lambda functions
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.143.181