Use mini define to implement modular management of front end code
- 2020-05-09 18:07:49
- OfStack
mini-define
A simple front-end modular framework based on require. If you don't want to take the time to learn require.js, or to flip through the long cmd/amd specification, then this mini-define is for you. If you've used sea.js or require.js before then mini-define is more efficient, lighter and easier to use. Project address: github
usage
First define the module
Definition module
1: define the module with define function
1.1 depending on whether there is a dependency, there are two situations:
1.1.1: no dependent modules
define('id',function(){
// put your code here
});
1.1.2: dependent modules
define('id',['modeA','modeB'],function(A,B){
// put your code here
});
1.2 according to whether it is necessary to return the processing result for external use, it can be divided into two situations:
1.2.1 return object:
define('id',function(){
return {
// put your code here
}
});
1.2.2 no object is returned
define('id',function(){
// put your code here
});
2: call the module with require() function
2.1 according to the number of modules requested, there can be two situations:
2.1.1. Call a single module
require('modeId')
2.1.2. Call multiple modules
require(['modeA','modeB']);
2.2 according to whether there is a callback, it can be divided into two situations:
2.2.1 has a callback handler
require('modeId',function(mode){
//put your code here
});
require(['modeA','modeB'],function(A,B){
//put your code here
});
2.2.2 has no callback processing
require('modeId');
The required modules are then referenced in turn on the index.html page
<!-- The core module -->
<script src="lib/core/require.js"></script>
<!-- A module for demonstration -->
<script src="lib/main.js"></script>
<script src="lib/config.js"></script>
<script src="lib/init.js"></script>
Finally, merge and compress the lib directory in the way you like to generate one min.js file. When the application is released, the corresponding index. html also needs to be adjusted 1:
<script src="lib/min.js"></script>
Advantages:
Compared to seajs.js or the original require.js, a hundred or so lines of annotated code are fat and completely bone.
There is absolutely no advanced content, no complex skills, almost no learning cost.