Skip to content

Commit 4564469

Browse files
committed
Place examples in the actual chapter
1 parent 9c26236 commit 4564469

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

chapters/ch09.asciidoc

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A while later, the RequireJS library was born. It provided a way of defining the
77

88
Other complexity management mechanisms exist, such as the dependency injection mechanism in AngularJS, where you define named components using functions where you can, in turn, specify other named component dependencies. AngularJS carries the load of dependency injection on your behalf, so you only have to name components and specify dependencies.
99

10-
CommonJS surfaced as an alternative to RequireJS, and it was swiftly popularized by Node.js soon afterwards. In this chapter we'll take a look at CommonJS, which is still heavily in use today. We'll then cover the module system introduced to native JavaScript in ES6, and lastly we'll explore interoperability between CommonJS and native JavaScript modules -- also known as "ES modules".
10+
CommonJS (CJS) surfaced as an alternative to RequireJS, and it was swiftly popularized by Node.js soon afterwards. In this chapter we'll take a look at CommonJS, which is still heavily in use today. We'll then cover the module system introduced to native JavaScript in ES6, and lastly we'll explore interoperability between CommonJS and native JavaScript modules -- also known as ECMAScript modules (ESM).
1111

1212
=== 9.1 CommonJS
1313

@@ -49,7 +49,7 @@ node app
4949
# [1, 2]
5050
----
5151

52-
The +require+ function in CommonJS modules can be treated dynamically, just like any other JavaScript function. This aspect of +require+ is sometimes leveraged to dynamically +require+ different modules that conform to one interface. As an example, let's conjure up a +templates+ directory with a number of view template functions. Our templates will take a model and return an HTML string.
52+
The +require+ function in CJS can be treated dynamically, just like any other JavaScript function. This aspect of +require+ is sometimes leveraged to dynamically +require+ different modules that conform to one interface. As an example, let's conjure up a +templates+ directory with a number of view template functions. Our templates will take a model and return an HTML string.
5353

5454
The template found in the following code snippet renders an item of a grocery shopping list by reading its attributes from a +model+ object.
5555

@@ -145,7 +145,7 @@ console.log(render(`list`, [{
145145

146146
image::../images/c09g03-dynamic-render.png["Printing different views through a normalized render function."]
147147

148-
Moving on, you'll notice that ES6 modules are heavily influenced by CommonJS. In the next few sections we'll look at +export+ and +import+ statements, and learn how ES6 modules are compatible with CommonJS.
148+
Moving on, you'll notice that ES6 modules are heavily influenced by CommonJS. In the next few sections we'll look at +export+ and +import+ statements, and learn how ESM is compatible with CJS.
149149

150150
=== 9.2 JavaScript Modules
151151

@@ -197,7 +197,7 @@ module.exports = [`hello`, 'world']
197197
module.exports = function hello () {}
198198
----
199199

200-
ES6 modules are files that may expose an API through +export+ statements. Declarations in ES6 modules are scoped to that module, just like we observed about CommonJS. Any variables declared inside a module aren't available to other modules unless they're explicitly exported as part of that module's API and then imported in the module that wants to access them.
200+
ES6 modules are files that may expose an API through +export+ statements. Declarations in ESM are scoped to the local module, just like we observed about CommonJS. Any variables declared inside a module aren't available to other modules unless they're explicitly exported as part of that module's API and then imported in the module that wants to access them.
201201

202202
===== Exporting a Default Binding
203203

@@ -233,7 +233,7 @@ function initialize () {
233233
initialize()
234234
----
235235

236-
In contrast with CommonJS, +export+ statements in ES6 modules can only be placed at the top level. "Top-level only" +export+ statements is a good constraint to have, as there aren't many good reasons to dynamically define and expose an API based on method calls. This limitation also helps compilers and static analysis tools parse ES6 modules.
236+
In contrast with CJS, +export+ statements in ESM can only be placed at the top level. "Top-level only" +export+ statements is a good constraint to have, as there aren't many good reasons to dynamically define and expose an API based on method calls. This limitation also helps compilers and static analysis tools parse ES6 modules.
237237

238238
[source,javascript]
239239
----
@@ -243,19 +243,19 @@ function initialize () {
243243
initialize()
244244
----
245245

246-
There are a few other ways of exposing an API in ES6 modules, besides +export default+ statements.
246+
There are a few other ways of exposing an API in ESM, besides +export default+ statements.
247247

248248
===== Named Exports
249249

250-
In CommonJS modules, when you want to expose multiple values, you don't necessarily need to explicitly export an object containing every one of those values. You could simply add properties onto the implicit +module.exports+ object. There's still a single binding being exported, containing all properties the +module.exports+ object ends up holding. While the following example exports two individual values, both are exposed as properties on the exported object.
250+
When you want to expose multiple values from CJS modules you don't necessarily need to explicitly export an object containing every one of those values. You could simply add properties onto the implicit +module.exports+ object. There's still a single binding being exported, containing all properties the +module.exports+ object ends up holding. While the following example exports two individual values, both are exposed as properties on the exported object.
251251

252252
[source,javascript]
253253
----
254254
module.exports.counter = 0
255255
module.exports.count = () => counter++
256256
----
257257

258-
We can replicate this behavior in ES6 modules by using the named exports syntax. Instead of assigning properties to an implicit +module.exports+ object like with CommonJS, in ES6 you declare the bindings you want to +export+, as shown in the following code snippet.
258+
We can replicate this behavior in ESM by using the named exports syntax. Instead of assigning properties to an implicit +module.exports+ object like with CommonJS, in ES6 you declare the bindings you want to +export+, as shown in the following code snippet.
259259

260260
[source,javascript]
261261
----
@@ -273,7 +273,7 @@ export counter // SyntaxError
273273
export count
274274
----
275275

276-
By being rigid in how its declarative module syntax works, ES6 modules favor static analysis once again at the expense of flexibility. Flexibility inevitably comes at the cost of added complexity, which is a good reason not to offer flexible interfaces.
276+
By being rigid in how its declarative module syntax works, ESM favors static analysis, once again at the expense of flexibility. Flexibility inevitably comes at the cost of added complexity, which is a good reason not to offer flexible interfaces.
277277

278278
===== Exporting Lists
279279

@@ -348,7 +348,7 @@ You can give aliases to named exports, as they pass through your module. If the
348348
export { increment as add } from './counter'
349349
----
350350

351-
Our modules could also expose every single named export found in another module by using a wildcard, as shown in the next snippet. Note that this wouldn't include the default binding exported by the +counter+ module.
351+
An ESM module could also expose every single named export found in another module by using a wildcard, as shown in the next snippet. Note that this wouldn't include the default binding exported by the +counter+ module.
352352

353353
[source,javascript]
354354
----
@@ -446,7 +446,19 @@ You can also explicitly name the +default+ binding, which needs an alias.
446446
import { default as counter, increment } from './counter'
447447
----
448448

449-
We also have namespace imports.
449+
The following example demonstrates how ESM semantics differ from those of CJS. Remember: we're exporting and importing bindings, and not direct references. For practical purposes, you can think of the +counter+ binding found in the next example as a property getter that reaches into the +counter+ module and returns its local +counter+ variable.
450+
451+
[source,javascript]
452+
----
453+
import counter, { increment } from './counter'
454+
console.log(counter) // <- 0
455+
increment()
456+
console.log(counter) // <- 1
457+
increment()
458+
console.log(counter) // <- 2
459+
----
460+
461+
Lastly, there are also namespace imports.
450462

451463
===== Wildcard +import+ statements
452464

0 commit comments

Comments
 (0)