JavaScript Module Loader--coolie

Source: Internet
Author: User
1. JavaScript modularity

JavaScript modularity is a corollary to the increasing workload of JavaScript, and in recent years JavaScript modularity has been a part of the standard and has been introduced in the next generation of JavaScript official standards.

At present, the mainstream modular specification is AMD, COMMONJS and so on, their good specification, elegant interface design, reasonable design pattern is accepted by many front-end development engineers, and also have a lot of modular loader also support them, such as Requirejs, Seajs and so on. 2. JavaScript Module Loader 2.1, Coolie Advantage

Coolie (Coolie) is the author in the recent writing of a module loader, compared to other market module loader, has the following advantages:

Lightweight, good figure, only supports advanced modern browsers, not a wide range of path aliases, like Nodejs, supports only local modules.

Small, minimal interface (2 interfaces) and configuration (2 configurations).

Elegant, reasonable interface and configuration.

Security, try not to invade global variables (2, respectively, define and Coolie), ease of programming.

Convenient, supporting the release of the building tools tailored for it.

Worry, a key to build, without human intervention, there is no many and difficult to understand the construction configuration.


2.2. Local development environment

In the development environment, no explicit module ID and module dependencies, the module path must write complete:

Module entrance:./index.js

The development environment cannot write module ID
define (function (require) {
//module dependent, cannot omit file suffix
var num = require ('./abc.js ');
var text = require (' text!.    /def.txt ');

Alert (num + text);
=> "123 Here is a string of plain text"
});

Dependent module:./abc.js

The development environment cannot write module ID
define (function (Require, exports, module) {
//module export
module.exports = 123;
});

Dependent text:./def.txt

Here is a string of plain text
2.3. Under Production environment:

After publishing, module Portal:./index.js?v=abc123

Define (' 0 ', [' 1 ', ' 2 '], function (a) {
var b = A (' 1 ');
var C = A (' 2 ');

alert (b+c);
=> "123 Here is a string of plain text"
});
Define (' 1 ', [], function (A, B, c) {
c.exports = 123;
});
Define (' 2 ', [], function (A, B, c) {
C.exports = ' Here is a string of plain text ';
});
2.4, Coolie configuration
Coolie.config ({
//* The base path of the entry module, you can also write the absolute path
//' base ' is relative to the ' coolie.js ' path, so as long as the ' coolie.js ' path is unchanged, the configuration file, regardless of which file reference is     It doesn't matter
//optional, default is ' coolie.js ' path
base: './',

//* module file version, used to clear the file cache, often used for publishing to production environment, details View ' coolie ' Publishing tool
For example, the entry file is ' Index.js ', then the actual request path is '/path/to/index.js?_=abc123 '
//optional, default is null
version: ' abc123 '
};

Pay attention.

Base parameter configuration, and sea.js some discrepancies, please note.

The base parameter is relative to the path where the Coolie.js is located.

The base parameter is also the relative path of the portal module.

Version numbers can be empty in the development environment, and the build is automatically updated to clear the cache in the production environment. 2.5, Coolie use

Run the Portal module path, note that there is no callback
//module path relative to ' Config.base '
coolie.use ('./index.js ');

Pay attention.

You must manually invoke the. Use () method.

parameter is the entry module path, not the Portal module name.

For the reusability of the configuration file, when Coolie.js's script specifies the Data-main attribute, the inline attribute has the highest precedence, so the. Use parameter can be empty at this time. such as: <script src= "./coolie.js" data-main= "./index2.js" ></script>, at which point the Portal module is relative to the config.base./index2.js file. This situation will be prompted in the console output. 3. Module Building Tools
3.1. Introduction

Supporting the modular building tool Coolie (Coolie), has been on the NPMJS official line. The author also used SPM3 packaging before, feeling still too complicated, and then self-made APB, and then simply wrote a whole set of things, including module loader and module building tools. Try to keep them in good shape and just do what they are good at and what they are doing. 3.2. Advantages

Compared to other building tools, he has the following advantages:

An elegant, popular and understandable API.

Easy to use, all automatically generate configuration files, each step is described in detail.

secure, without modifying any source files.

The ultimate, all modules, compression of all compressible content, including module ID, module-dependent path, local variables and so on. 3.3, features

For more API instructions, please go to the GitHub project and link below. 3, Link

Coolie Module Loader: Https://github.com/cloudcome/coolie

Coolie Module Building tools: Https://github.com/cloudcome/nodejs-coolie

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.