You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/ch01.asciidoc
+35-4Lines changed: 35 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ Relying on consistent API shapes is a great way of increasing productivity, give
29
29
30
30
=== 1.2 A Brief History of Modularity
31
31
32
-
When it comes to JavaScript, modularity is a modern concept. In this section we'll quickly revisit and summarize the milestones in how modularity evolved in the world of JavaScript.
32
+
When it comes to JavaScript, modularity is a modern concept. In this section we'll quickly revisit and summarize the milestones in how modularity evolved in the world of JavaScript. This section isn't meant to be a comprehensive list, by any means, but instead it's meant to illustrate the major paradigm changes along the history of JavaScript.
33
33
34
34
==== 1.2.1 Script Tags and Closures
35
35
@@ -143,11 +143,42 @@ Needless to say, the delay in introducing this little-known build tool, combined
143
143
144
144
==== 1.2.3 Node.js and the Advent of CommonJS
145
145
146
-
..
146
+
Among the many innovations hailed by Node.js, one was the CommonJS module system -- or CJS for short. Taking advantage of the fact that Node.js programs had access to the file system, the CommonJS standard is more in line with traditional module loading mechanisms. In CommonJS, each file is a module with its own scope and context. Dependencies are loaded using a synchronous `require` function that can be dynamically invoked at any time in the lifecycle of a module, as illustrated in the next snippet.
147
+
148
+
[source,javascript]
149
+
----
150
+
const mathlib = require('./mathlib')
151
+
----
147
152
148
-
==== 1.2.4 ES6, `import`, and Babel
153
+
Much like RequireJS and AngularJS, CommonJS dependencies are also referred to by a pathname. The main difference is that the boilerplate function and dependency array are now both gone, and the interface from a module could be assigned to a variable binding, or used anywhere a JavaScript expression could be used.
149
154
150
-
..
155
+
Unlike RequireJS or AngularJS, CommonJS was rather strict. In RequireJS and AngularJS you could have many dynamically-defined modules per file, whereas CommonJS had a one-to-one mapping between files and modules. At the same time, RequireJS had several ways of declaring a module and AngularJS had several kinds of factories, services, providers and so on -- besides the fact that its dependency injection mechanism was tightly coupled to the AngularJS framework itself. CommonJS, in contrast, had a single way of declaring modules. Any JavaScript file was a module, calling `require` would load dependencies, and anything assied to `module.exports` was its interface. This enabled better tooling and code introspection -- making it easier for tools to learn the hierarchy of a CommonJS component system.
156
+
157
+
Eventually, Browserify was invented as way of bridging the gap between CommonJS modules for Node.js servers and the browser. Using the `browserify` command-line interface program and providing it with the path to an entry point module, one could combine an unthinkable amount of modules into a single browser-ready bundle. The killer feature of CommonJS, the npm package registry, was decisive in aiding its takeover of the module loading ecosystem.
158
+
159
+
Granted, npm wasn't limited to CommonJS modules or even JavaScript packages, but that was and still is by and large its primary use case. The prospect of having thousands of packages (now over half million and steadily growing) available in your web application at the press of a few fingertips, combined with the ability to reuse large portions of a system on both the Node.js web server and each client's web browser, was too much of a competitive advantage for the other systems to keep up.
160
+
161
+
==== 1.2.4 ES6, `import`, Babel, and Webpack
162
+
163
+
As ES6 became standardized in June of 2015, and with Babel transpiling ES6 into ES5 long before then, a new revolution was quickly approaching. The ES6 specification included a module system native to JavaScript, often referred to as ECMAScript Modules (ESM).
164
+
165
+
ESM is largely influenced by CJS and its predecessors, offering a static declarative API as well as a promise-based dynamic programmative API, as illustrated next.
166
+
167
+
[source,javascript]
168
+
----
169
+
import mathlib from './mathlib'
170
+
import('./mathlib').then(mathlib => {
171
+
// …
172
+
})
173
+
----
174
+
175
+
In ESM, too, every file is a module with its own scope and context. One major advantage in ESM over CJS is how ESM has -- and encourages -- a way of statically importing dependencies. Static imports vastly improve the introspection capabilities of module systems, given they can be analyzed statically and lexically extracted from the abstract syntax tree (AST) of each module in the system. Static imports in ESM are constrained to the topmost level of a module, further simplifying parsing and introspection.
176
+
177
+
In Node.js v8.5.0, ESM module support was introduced behind a flag. Most evergreen browsers also support ESM modules behind flags.
178
+
179
+
Webpack is a successor to Browserify that largely took over in the role of universal module bundler thanks to a broader set of features. Just like in the case of Babel and ES6, Webpack has long supported ESM with both its `import` and `export` statements as well as the dynamic `import()` function. It has made a particularly fruitful adoption of ESM, in no little parts thanks to the introduction of a "code-splitting" mechanism whereby it's able to partition an application into different bundles to improve performance on first load experiences.
180
+
181
+
Given how ESM is native to the language, -- as opposed to CJS -- it can be expected to completely overtake the module ecosystem in a few years time.
0 commit comments