-
The preparatory work
Two files, use separately
CommonJSandES ModuleThe export method is as follows: -
I met
modeBecause the principle of modularity needs to be used
modeProperty so here’s a quick look at itmodeBy selecting either Development, Production, or None to set the mode parameter, you can enable webPack’s built-in optimizations for that environment. The default value is production
The difference between the three:
The default is yes
productionTherefore, the packaged file is the code after being compressed and uglified, as shown in the figure below:It is not convenient for us to explore the source code, so we can switch to
developmentThe code for the schema to be successfully packaged is as follows:This is convenient for us to read the source code
-
CommonJSModularization implementation principleWrite a simple file to use
CommonJSImport the method and then call it, and finally change the packaged entry file to this file:You can see the following:
Delete all comments at this point, and the code after formatting the space is as follows:
From the source of the outermost layer can be seen to wrap an immediate execution of the function, code block inside there are some immediate execution of the function, next delete all the immediate execution of the function again look at the source:
The source code is commented as follows:
The specific flow chart is as follows:
-
ES ModuleModularization implementation principleTo prepare work as above, create one with
ES ModuleExport files, and create a file and then use itES ModuleImport, then package the draft file, remove the corresponding comments and format the code as follows:The specific process is as follows:
-
CommonJSloadingES ModuleThe principle ofThe principle here is the same as
ES ModuleThe principle is basically the same, except when the method is called using the structure of the way to get the method, andES ModuleThe method is called by an object calling a property, the specific source code is as follows: -
ES ModuleloadingCommonJSThe principle ofHere and
CommonJSAlmost the same, basically usingCommonJSPrinciple to achieve, in the last call is not the use of deconstruction, but through the property of the way the specific source code is as follows: