SEAJS Module Compression problem

Source: Internet
Author: User

When optimizing the project code, you want to use SEAJS to modularize the code. Look at the official 5-minute tutorial, feel very good, also did not think about it has been developed, there is no problem. And so a colleague said the code to pack a test on the device to find out how to run, depressed.

So the single-step test, found that the module has not been added. Look at the seajs of the original code, understand what is going on.

There are two approaches to define module parsing, one from Deps in define (ID, deps, Factory) and one for parsing define code from require. Take a look at the code:

1Module.define =function(ID, deps, factory) {2   varArgslen =Arguments.length3 4   //define (Factory)5   if(Argslen = = 1) {6Factory =ID7ID =undefined8   }9   Else if(Argslen = = 2) {TenFactory =Deps One  A     //define (Deps, factory) -     if(IsArray (id)) { -Deps =ID theID =undefined -     } -     //define (ID, factory) -     Else { +Deps =undefined -     } +   } A  at   //Parse Dependencies According to the module factory code -   if(!isarray (Deps) &&isfunction (Factory)) { -Deps =parsedependencies (factory.tostring ()) -   } -...

If pass the deps that does not carry on the analysis, if passes that then carries on the source parsing:

1 varRequire_re =/"(?: \ \"| [^"]) * ' | ' (?:\ \ ' | [^ ']) * ' |\/\*[\s\s]*?\*\/|\/(?: \ \\/| [^\/\r\n]) +\/(? =[^\/]) |\/\/.*|\.\s*require| (?:^| [^$]) \brequire\s*\ (\s* (["']) (. +?) \1\s*\)/g2 varSlash_re =/\\\\/g3 4 functionparsedependencies (code) {5   varRET = []6 7Code.replace (Slash_re, "")8. replace (Require_re,function(M, M1, m2) {9         if(m2) {Ten Ret.push (m2) One         } A       }) -  -   returnret the}

Seajs is a regular comparison of the source code, find require, that is require in the Seajs module, is a key word.

Then the problem comes, generally we use compression tools are compressed, require is not the standard JS keyword, so after a compressed require become ABCDEFG .... So naturally it is impossible to use.

There are two analytic methods:

1. Seajs is officially given the SEAJS standard module building tool: https://github.com/seajs/seajs/issues/538 is built using SPM.

2. Replace the compression tool, using a customizable keyword, which means that the compression tool does not compress the require variable.

Currently the mainstream of three compression tools: YUI compressor,google Closure compiler,uglifyjs. As far as we know, it seems that the top two does not support custom keywords (?), UGLIFYJS is supported. So you can use UGLIFYJS to compress

UGLIFYJS hello.js-o hello.min.js-m-c-r require

Use the-r option to specify that the variable is not compressed.

  

In general the use of SEAJS should be as far as possible to use the official building tools.

SEAJS Module Compression problem

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.