posts - 495,comments - 227,trackbacks - 0
var path = require('path');
var fs = require('fs');
var vm = require('vm');
var os = require('os');

/**
 * 格式化缩进的个数
 
*/
function toIndent(indent) {
    
var s = [];
    
for (var i = 0; i < indent; i++) {
        s.push('\t');
    }
    
return s.join('');
}

/**
 * 将数组对象转换成原始字符串
 
*/
function array2string(arr, indent) {
    
var s = ['[', os.EOL], hasProp = false;
    
for (var i = 0; i < arr.length; i++) {
        
if (!hasProp) {
            hasProp 
= true;
        }

        s.push(toIndent(indent 
+ 1));

        
var item = arr[i];
        
var itemtp = typeof(item);
        
if (itemtp === 'object') {
            
if (item instanceof Array) {
                s.push(array2string(item, indent 
+ 1));
            } 
else {
                s.splice(s.length 
- 22);
                s.push(object2strng(item, indent).trim());
            }
        } 
else {
            s.push(JSON.stringify(item));
        }
        s.push(',');
        s.push(os.EOL);
    }
    
if (hasProp) {
        s.splice(s.length 
- 21);
    }
    s.push(toIndent(indent));
    s.push(']');
    
return s.join('');
}

/**
 * 将对象转换成原始字符串
 
*/
function object2strng(obj, indent) {
    
var s = ['{', os.EOL], hasProp = false;

    
for (var o in obj) {
        
if (!hasProp) {
            hasProp 
= true;
        }
        s.push(toIndent(indent 
+ 1));
        s.push(JSON.stringify(o));
        s.push(':');

        
var tp = typeof(obj[o]);
        
if (tp === 'object') {
            
if (obj[o] instanceof Array) {
                s.push(array2string(obj[o], indent 
+ 1));
            } 
else {
                s.push(object2strng(obj[o], indent 
+ 1));
            }
        } 
else if (tp === 'function') {
            s.push(obj[o].toString());
        } 
else {
            s.push(JSON.stringify(obj[o]));
        }
        s.push(',');
        s.push(os.EOL);
    }
    
if (hasProp) {
        s.splice(s.length 
- 21);
    }
    s.push(toIndent(indent));
    s.push('}');
    
return s.join('');
}

//提取正式代码里的requirejs的配置字符串,并动态执行转换成json对象; 修改相关的值信息为下边的打包操作做准备; 并将配置信息再转成字符串形式写到临时文件下
var mainPath = path.resolve(process.cwd(), '../js/main.js');
var mainContent = fs.readFileSync(mainPath, 'utf-8').replace(/(requirejs\.config\()?([^)]]*)(\);)?/gm, '$2');
vm.runInThisContext('
var mainCfg= ' + mainContent);//将提取的字符串转成mainCfg对象
mainCfg.baseUrl = '/static/js/dist/lib';
var nMainCfgStr = 'requirejs.config(' + object2strng(mainCfg, 0+ ');';//重新生成main.js配置文件,为下边的打包做准备
var buildPath = path.resolve(process.cwd(), './main.js');
fs.writeFileSync(buildPath, nMainCfgStr);
console.log('write temp file main.js fininshed');

//打包的配置信息
var buildJson = {
    appDir: '..
/js',
    baseUrl: 'lib',
    mainConfigFile: '.
/main.js',
    dir: '..
/js/dist',
    modules: [{
        'name': '..
/main',
        include: []
    }]
};
for (var p in mainCfg.paths) {//这里提取所有的依赖模块,打包时放到main.js文件下
    buildJson.modules[0].include.push(p);
}

var buildPath = path.resolve(process.cwd(), './build_main.json');
fs.writeFileSync(buildPath, object2strng(buildJson, 
0));//生成打包配置文件
console.log('wirte temp file build_main.json fininshed');



写一批处理文件build.bat
@echo off
node build.js
node r.js -o build_main.json
@pause
执行就可以了
posted on 2016-11-01 16:24 SIMONE 阅读(3856) 评论(0)  编辑  收藏 所属分类: nodejs

只有注册用户登录后才能发表评论。


网站导航: