how to define dependencies: <script>-tag, CommonJS, AMD,ES6, …
<script>
-tag
Common problems:
- Conflicts in the global object.
- Order of loading is important.
- Developers have to resolve dependencies of modules/libraries.
- In big projects the list can get really long and difficult to manage.
CommonJS: synchronous require
base: synchronous require
&& exported interface module.exports
. It used server-side by node.js
.
Common problems:
- Blocking calls do not apply well on networks. Network requests are asynchronous.
- No parallel require of multiple modules.
AMD: asynchronous require
base: require
&& define
. It used by require.js
Pros:
- Fits the asynchronous request style in network.
- Parallel loading of multiple modules.
Cons:
- Coding overhead. More difficult to read and write.
- Seems to be some kind of workaround.
ES6 Modules
base: import
&& export
&& module
Pros:
- Static analysis is easy.
- Future-proof as ES standard.
Cons
- Native browser support will take time.
- Very few modules in this style.