头图

https://segmentfault.com/a/1190000039346572 This article is written in more detail, but there are some points that confused me in the past and need to be memorized separately.

commonjs

The Node application is composed of modules and adopts the CommonJS module specification. In other words, CommonJs is applied on the node server. If the browser wants to use the CommonJs specification, the browserify library needs to be used for conversion. CommonJs is divided into two parts: moudle object and requeire command. That is to say, the code to be executed with node can only follow the commonjs specification, export using the module.export object, and import using the require method. If you want to write code executed on the node side such as export default / import, you need to configure it separately (find it online). Node provides a module build function and directly uses console.log(module) to see all the attributes
image.png

amd

Because the commonjs specification load dependent resources are loaded synchronously, and the loading process is always stuck, so amd writes a bunch of content that needs to be loaded, and then puts the code to be executed after the load is completed, require.js is the amd specification Practice. The way of use is like this

<script src='../node_modules/requirejs/require.js' data-main='./index'></script>
// data-main属性的作用是,指定网页程序的主模块。在上例中,就是js目录下面的index.js,这个文件会第一个被require.js加载。由于require.js默认的文件后缀名是js,所以可以把index

How to write amd standard lib library

// moduleA.js
  define(function (){
    var add = function (x,y){
      return x+y;
    };
    return {
      add: add
    };
  });

Yes, after the introduction of require.js, a global define method will be provided, so the lib library only needs to use the define method directly. When using it like this

// index.js
  require(['moduleA'], function (moduleA){
    console.log(moduleA)
       //moduleA就是moduleA.js模块传入的函数执行后返回的对象{add:function}
 });

umd

umd

Alas, 😮‍💨, I called for a meeting without waking up from a nap. Tired and don't want to write anymore


黄小波波
31 声望5 粉丝