jQuery source code uses Require.js, but the final file doesn't?

后端 未结 2 1437
無奈伤痛
無奈伤痛 2020-12-09 12:49

Uncompressed jQuery file: http://code.jquery.com/jquery-2.0.3.js jQuery Source code: https://github.com/jquery/jquery/blob/master/src/core.js

What are they doing to m

2条回答
  •  青春惊慌失措
    2020-12-09 13:19

    Short answer:

    You have to create your own custom build procedure.

    Long answer

    jQuery's build procedure works only because jQuery defines its modules according to a pattern that allows a convert function to transform the source into a distributed file that does not use define. If anyone wants to replicate what jQuery does, there's no shortcut: 1) the modules have to be designed according to a pattern which will allow stripping out the define calls, and 2) you have to have a custom conversion function. That's what jQuery does. The entire logic that combines the jQuery modules into one file is in build/tasks/build.js.

    This file defines a custom configuration that it passes to r.js. The important option are:

    • out which is set to "dist/jquery.js". This is the single file produced by the optimization.

    • wrap.startFile which is set to "src/intro.js". This file will be prepended to dist/jquery.js.

    • wrap.endFile which is set to "src/outro.js". This file will be appended to dist/jquery.js.

    • onBuildWrite which is set to convert. This is a custom function.

    The convert function is called every time r.js wants to output a module into the final output file. The output of that function is what r.js writes to the final file. It does the following:

    • If a module is from the var/ directory, the module will be transformed as follows. Let's take the case of src/var/toString.js:

      define([
          "./class2type"
          ], function( class2type ) {
          return class2type.toString;
      });
      

      It will become:

      var toString = class2type.toString;
      
    • Otherwise, the define(...) call is replace with the contents of the callback passed to define, the final return statement is stripped and any assignments to exports are stripped.

    I've omitted details that do not specifically pertain to your question.

提交回复
热议问题