A static module system declares dependencies and exports with fixed language syntax, using import and export rather than runtime loader calls. This makes a module’s contract visible before execution. section 6.3
Static imports must appear at the top of a file, allowing compilers and tools to map application structure ahead of time. That visibility supports prefetching, preloading, stronger static analysis, and removing unreachable code from packages. section 6.3
Static and dynamic systems differ in how they express loading:
- CommonJS and RequireJS use JavaScript functions such as require to load modules dynamically. section 6.3
- ESM uses declarations such as import { Success } from './Validation.js'; to express dependencies statically. section 6.3
Imported bindings reflect module state
An ESM consumer can call setFeePercent(0.7), after which netTotal(10) returns 7 and feePercent reads 0.7. Direct assignment to the imported feePercent is not the mechanism for changing it. section 6.3
Table 6.1 Loading the Transaction class with different module systems (continued) (view table figure)
requirejs(['domain/Transaction.js'], Transaction => {
|
||
Compare CommonJS, RequireJS, and ESM loading syntax side by side. section 6.3
