Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kmc

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kmc - npm Package Compare versions

Comparing version 1.0.25 to 1.0.27

601

index.js
/**
* Module Compiler for KISSY 1.2+
* @author daxingplay<daxingplay@gmail.com>
* KISSY Module Compiler
* @author: daxingplay<daxingplay@gmail.com>
* @time: 13-3-12 11:41
* @description:
*/
var fs = require('fs'),
path = require('path'),
fileUtil = require('./fileUtil'),
colors = require('colors'),
_ = require('lodash'),
iconv = require('iconv-lite'),
colors = require('colors');
Compiler = require('./lib/compiler'),
utils = require('./lib/utils'),
parseConfig = require('./lib/parse-config');
function startWith(str, prefix){
return str.lastIndexOf(prefix, 0) === 0;
}
function convertCharset(charset){
charset = charset.toLowerCase();
if(charset == '' || charset == 'gbk' || charset == 'gb2312'){
charset = '';
}
return charset;
}
function addPkgNameToPath(pkgPath, pkgName){
if(pkgName){
var basename = path.basename(pkgPath).replace(/(\\|\/)$/, '');
if(basename != pkgName){
pkgPath = path.normalize(path.join(pkgPath, pkgName));
function getModulePath(moduleName, config){
for(var i = 0; i < config.packages.length; i++){
var pkg = config.packages[i],
mod = moduleName.match(new RegExp(pkg.name + '\/(.*)'));
if(mod && mod[1]){
var modulePath = path.resolve(pkg.path, pkg.name, mod[1].replace(/\.js/, '') + '.js');
if(fs.existsSync(modulePath)){
return modulePath;
}
}
}
return pkgPath;
return false;
}
function concat(a, origin){
if(a && origin && typeof origin.length !== 'undefined'){
if(typeof a === 'string'){
origin.push(a);
}else if(a.length){
origin = origin.concat(a);
}
}
return origin;
}
var ModuleCompiler = {
_config: {
exclude: [],
ignoreFiles: [],
suffix: '',
charset: '',
debug: false,
combine: false,
silent: false
},
_packages: {},
_mappedRules: [],
_moduleCache: {},
_analyzedFiles: [],
_combinedModules: [],
_dependencies: {},
_clean: function(){
this._moduleCache = {};
this._analyzedFiles = [];
this._combinedModules = [];
this._dependencies = {};
},
_parseConfig: function(cfg){
module.exports = {
_config: {},
config: function(cfg){
var self = this;
if(cfg.packages){
for(var i = 0; i < cfg.packages.length; i++){
var pkg = cfg.packages[i];
if(typeof pkg.charset === 'undefined'){
// if left blank, node will treat it as utf-8
pkg.charset = '';
}
pkg.path = path.resolve(addPkgNameToPath(pkg.path, pkg.name)) || __dirname;
pkg.combine = !!pkg.combine;
if(fs.existsSync(pkg.path)){
self._packages[pkg.name] = pkg;
}
}
if(cfg){
self._config = parseConfig.parse(cfg, self._config);
}
if(cfg.map){
for(var i = 0; i < cfg.map.length; i++){
var curMap = cfg.map[i];
if(curMap && typeof curMap[0] !== 'undefined' && typeof curMap[1] !== 'undefined'){
self._mappedRules.push(curMap);
}
}
self._config.packages = [];
for(var pkg in self._config.pkgs){
self._config.packages.push(self._config.pkgs[pkg]);
}
if(cfg.suffix){
self._config.suffix = cfg.suffix;
}
self._config.debug = cfg.debug ? true : false;
// if(cfg.exclude){
// if(typeof cfg.exclude === 'string'){
// self._config.exclude.push(cfg.exclude);
// }else{
// self._config.exclude = self._config.exclude.concat(cfg.exclude);
// }
// }
self._config.exclude = concat(cfg.exclude, self._config.exclude);
self._config.ignoreFiles = concat(cfg.ignoreFiles, self._config.ignoreFiles);
self._config.charset = cfg.charset ? cfg.charset : '';
self._config.silent = !!cfg.silent;
return self._config;
return this._config;
},
_removeComments: function(str){
var totallen = str.length,
token;
// remove single line comments
var startIndex = 0;
var endIndex = 0;
var comments = [];
while ((startIndex = str.indexOf("//", startIndex)) >= 0) {
endIndex = str.indexOf("\n", startIndex + 2);
if (endIndex < 0) {
endIndex = totallen;
}
token = str.slice(startIndex + 2, endIndex);
comments.push(token);
startIndex += 2;
}
for (var i=0,max=comments.length; i<max; i = i+1){
str = str.replace("//" + comments[i] + "\n", "");
}
// remove multi line comments.
startIndex = 0;
endIndex = 0;
comments = [];
while ((startIndex = str.indexOf("/*", startIndex)) >= 0) {
endIndex = str.indexOf("*/", startIndex + 2);
if (endIndex < 0) {
endIndex = totallen;
}
token = str.slice(startIndex + 2, endIndex);
comments.push(token);
startIndex += 2;
}
for (i=0,max=comments.length; i<max; i = i+1){
str = str.replace("/*" + comments[i] + "*/", "");
}
// remove white spaces
str = str.replace(/\s+/g, " ");
return str;
},
_analyzeRequires: function(filePath){
analyze: function(inputFile){
var self = this;
if(self._analyzedFiles.indexOf(filePath) > 0){
self._config.debug && console.log('file :' + filePath + ' already analyzed.');
}else if(fs.existsSync(filePath)){
self._analyzedFiles.push(filePath);
var fileContent = fs.readFileSync(filePath).toString();
// remove comments
fileContent = self._removeComments(fileContent);
var requires = fileContent.match(/\{[\s\w:'",]*requires['"\s]*:\s*(\[?[^;\}]*\]?)\}\s*\)/g);
if(requires != null){
for(var i = 0; i < requires.length; i++){
var requiredModules = eval('(' + requires[i]).requires;
for(var j = 0; j < requiredModules.length; j++){
var requirePath = requiredModules[j],
module;
if(path.extname(requirePath) == '.css') {
continue;
}
module = self._addModule(requirePath, path.dirname(filePath));
if(module !== null && module.path){
// self._analyzedFiles.push(module.path);
self._analyzeRequires(module.path);
}
}
}
}else{
self._config.debug && console.log('INFO: module ' + filePath + ' has no depends.');
}
}else{
!self._config.silent && console.log('WARING: file %s not found.', filePath);
}
// to make sure there is at least one package in config.
self._config = parseConfig.check(self._config, inputFile);
// start to analyze.
var c = new Compiler(self._config);
return c.analyze(inputFile);
},
_resolveModuleName: function(modulePath, pkg){
var relativePkgPath = path.relative(pkg.path, modulePath).replace(/\\/g, '/'),
moduleName = relativePkgPath.replace(/^\.\//, '');
if(!startWith(relativePkgPath, pkg.name + '/') && pkg.name !== 'kissy'){
moduleName = pkg.name + '/' + moduleName;
}
return moduleName.replace(/\.js$/i, '');
},
_addModule: function(requirePath, curDir){
build: function(inputFilePath, outputFilePath, outputCharset, depFile, traverse){
var self = this,
module = {};
if(requirePath.match(/\.js$/i)){
if(typeof curDir === 'undefined' || fs.existsSync(requirePath)){
self._config.debug && console.log('core module ' + requirePath);
module.path = requirePath;
}
}else{
requirePath += '.js';
targets = [],
result = {
'success': true,
'files': []
},
combo = [];
// for object arguments.
if(_.isPlainObject(inputFilePath) && inputFilePath.src){
outputFilePath = inputFilePath.dest;
outputCharset = inputFilePath.outputCharset;
depFile = inputFilePath.depPath;
traverse = inputFilePath.traverse;
inputFilePath = inputFilePath.src;
}
if(requirePath.match(/^\.{1,2}/) && curDir){
module.path = path.resolve(curDir, requirePath);
}else{
if(requirePath.indexOf('/') === 0){
requirePath = requirePath.substring(1);
}
}
var packageName,
packagePath = '';
for(var pkgName in self._packages){
if(self._packages.hasOwnProperty(pkgName)){
var pkg = self._packages[pkgName];
if(startWith(requirePath, pkg.name + '/')){
module.path = path.resolve(pkg.path, requirePath.replace(pkg.name + '/', ''));
if(fs.existsSync(module.path)){
packageName = pkg.name;
module.name = requirePath;
break;
}
}
if(module.path){
var curRelativePath = path.relative(pkg.path, module.path);
if(packagePath == '' || packagePath.length > curRelativePath.length){
packagePath = curRelativePath;
packageName = pkg.name;
}
if(_.isString(inputFilePath)){
var target = path.resolve(inputFilePath);
if(fs.existsSync(target)){
if(fs.statSync(target).isDirectory()){
// var files = fs.readdirSync(target);
_.forEach(utils.traverseDirSync(target, traverse), function(file){
var inputFile = path.resolve(target, file),
outputFile = path.resolve(outputFilePath, path.relative(target, file));
if(path.extname(inputFile) === '.js'){
targets.push({
src: inputFile,
dest: outputFile
});
}
});
}else{
var curPath = path.normalize(path.join(pkg.path, requirePath));
if(fs.existsSync(curPath)){
module.path = curPath;
packageName = pkg.name;
break;
}
targets.push({
src: target,
dest: outputFilePath
});
}
}
}
if(module.path){
module.package = self._packages[packageName];
module.name = (typeof module.name === 'undefined' ? self._resolveModuleName(module.path, module.package) : module.name).replace(/\.js\s*$/i, '');
module.name = self._mapModuleName(module.name);
module.charset = module.package.charset;
if(module.name){
if(!self.isExcluded(module.name)){
if(fs.existsSync(module.path)){
module.status = 'ok';
}else{
module.status = 'missing';
}
}else{
module.status = 'excluded';
}else{
// MC.build('pkgName/abc');
// in this case, package must be configured.
var modulePath = getModulePath(inputFilePath, self._config);
if(modulePath){
targets.push({
src: modulePath,
dest: outputFilePath
});
}
}else{
module.status = 'error';
}
self._moduleCache[module.name] = module;
self._combinedModules.push(module.name);
// self._analyzedFiles.push(module.path);
return module;
}else{
self._config.debug && console.log('module %s cannot be found.', requirePath);
}
return null;
},
_mapModuleName: function(moduleName){
var self = this;
if(self._mappedRules.length > 0){
for(var i = 0; i < self._mappedRules.length; i++){
var rule = self._mappedRules[i];
if(moduleName.match(rule[0])){
return moduleName.replace(rule[0], rule[1]);
}else if(_.isPlainObject(inputFilePath)){
_.forEach(inputFilePath, function(file){
if(fs.src){
targets.push({
src: file.src,
dest: file.dest ? file.dest : path.dirname(file.src)
});
}
}
});
}else if(_.isArray(inputFilePath)){
var destIsArray = _.isArray(outputFilePath) ? outputFilePath : false;
_.forEach(inputFilePath, function(file, index){
targets.push({
src: file,
dest: destIsArray && outputFilePath[index] ? outputFilePath[index] : outputFilePath
});
});
}
return moduleName;
},
_combo: function(result, outputPath, outputCharset){
var self = this,
combinedComment = [
'/*',
'combined files : ',
'',
result.combined.join('\r\n'),
'',
'*/',
''
].join('\r\n');
// deal with output charset. if not specified, use charset in config.
outputCharset = (typeof outputCharset === 'undefined' || outputCharset === null) ? self._config.charset : outputCharset;
_.forEach(targets, function(file, index){
self._config = parseConfig.check(self._config, file.src);
var config = _.cloneDeep(self._config);
var kmc = new Compiler(config);
var re = kmc.build(file.src, file.dest, outputCharset);
re.modules = kmc.modules;
depFile && combo.push(re.autoCombo);
result.files.push(re);
});
result.success = result.files.length !== 0;
var combineFile = self._config.suffix ? outputPath.replace(/\.js$/i, self._config.suffix + '.js') : outputPath;
//prepare output dir
fileUtil.mkdirsSync(path.dirname(combineFile));
// if exists, unlink first, otherwise, there may be some problems with the file encoding.
if(fs.existsSync(combineFile)){
fs.unlinkSync(combineFile);
if(depFile){
utils.writeFileSync(path.resolve(path.dirname(outputFilePath), depFile), utils.joinCombo(combo), outputCharset);
}
var fd = fs.openSync(combineFile, 'w');
// fs.writeSync(fd, combinedComment, 0, convertCharset(self._config.charset));
var combinedCommentBuffer = iconv.encode(combinedComment, outputCharset);
fs.writeSync(fd, combinedCommentBuffer, 0, combinedCommentBuffer.length);
fs.closeSync(fd);
fd = fs.openSync(combineFile, 'a');
for (var moduleName in result._moduleCache) {
if(result._moduleCache.hasOwnProperty(moduleName)){
var module = result._moduleCache[moduleName],
moduleContent = iconv.decode(fs.readFileSync(module.path), module.charset);
//add module path
var start = moduleContent.indexOf('KISSY.add(');
if(start == -1) {
start = moduleContent.indexOf('.add(');
if(start != -1) {
start = start + 5;
}
} else {
start = start + 10;
}
var end = moduleContent.indexOf('function', start);
//find it
if(start > -1 && end > start) {
//KISSY.add(/*xxx*/function(xxx)) or KISSY.add('xxx', function(xxx))
//remove user comments but preserve user defined module name.
var oldModuleName = moduleContent.substring(start, end),
moduleNameRegResult = oldModuleName.match(/^(\s*)\/\*(.*)\*\/(\s*)$/);
if(moduleNameRegResult != null){
// replace user comments with my module name.
// moduleContent = moduleContent.replace(oldModuleName, moduleNameRegResult[1]);
moduleContent = moduleContent.replace(oldModuleName, '\'' + module.name + '\', ');
}
} else if(start > -1 && end == start) {
//KISSY.add(function(xxx))
moduleContent = [moduleContent.slice(0, start), '\'' + module.name + '\',', moduleContent.slice(end)].join('');
}
// add a new line after.
moduleContent += '\r\n';
var buffer = iconv.encode(moduleContent, outputCharset);
fs.writeSync(fd, buffer, 0, buffer.length);
}
}
fs.closeSync(fd);
return result;
},
analyze: function(inputPath){
combo: function(inputFile, depFileName, depFileCharset, fixModuleName, returnDependencies, outputDir,comboOnly){
var self = this,
result = null;
self._analyzeRequires(inputPath);
var dependencies = [],
combined = [],
moduleCache = {};
for(var moduleName in self._moduleCache){
var module = self._moduleCache[moduleName];
dependencies.push(module);
if(module.status == 'ok'){
combined.push(module.name);
moduleCache[module.name] = module;
}
content,
config;
if(_.isObject(inputFile)){
depFileName = inputFile.depPath;
depFileCharset = inputFile.depCharset;
fixModuleName = inputFile.fixModuleName;
returnDependencies = inputFile.showFullResult;
outputDir = inputFile.dest;
inputFile = inputFile.src;
}
var coreModule = self._addModule(inputPath);
if(coreModule){
if(combined.indexOf(coreModule.name) > 0){
// this situation is rare. But we should consider it.
self._config.debug && console.log('core module already added before. Skip...');
}else{
combined.push(coreModule.name);
moduleCache[coreModule.name] = coreModule;
}
result = coreModule;
result.submods = dependencies;
result.combined = combined;
result._moduleCache = moduleCache;
self._config = parseConfig.check(self._config, inputFile);
config = _.cloneDeep(self._config);
fixModuleName = fixModuleName !== false;
var outputFile = path.resolve(outputDir,path.basename(inputFile));
var c = new Compiler(config,outputFile);
var result = c.analyze(inputFile);
content = c.combo(fixModuleName, outputDir,comboOnly);
if(content && depFileName){
utils.writeFileSync(depFileName, content, depFileCharset);
}
// clean.
self._clean();
return result;
return returnDependencies === true ? { files: [result], success: true, modules: c.modules, content: content } : content;
},
build: function(inputPath, outputPath, outputCharset){
var self = this;
// self._analyzeRequires(inputPath);
// var coreModule = self._addModule(inputPath);
var result = self.analyze(inputPath);
// self._config.charset = coreModule.charset;
// combo file.
self._combo(result, outputPath, outputCharset);
!self._config.silent && console.info('[ok]'.bold.green+' ===> %s', inputPath, outputPath);
// clean.
// self._clean();
return result;
},
isExcluded: function(file){
var self = this;
for(var i = 0; i < self._config.exclude.length; i++){
if(file.match(self._config.exclude[i])){
return true;
}
}
return false;
},
isFileIgnored: function(filePath){
var self = this;
for(var i = 0; i < self._config.ignoreFiles.length; i++){
if(filePath.match(self._config.ignoreFiles[i])){
return true;
}
}
return false;
},
getModulePath: function(moduleName){
var self = this;
for(var i = 0; i < self._config.packages.length; i++){
var pkg = self._config.packages[i],
module = moduleName.match(new RegExp(pkg.name + '\/(.*)'));
if(module && module[1]){
var modulePath = path.resolve(pkg.path, module[1].replace(/\.js/, '') + '.js');
if(fs.existsSync(modulePath)){
return modulePath;
}
}
}
return false;
}
};
module.exports = {
config: function(cfg){
var config = ModuleCompiler._config;
if(cfg){
config = ModuleCompiler._parseConfig(cfg);
}
config.packages = [];
for(var pkg in ModuleCompiler._packages){
config.packages.push(ModuleCompiler._packages[pkg]);
}
return config;
},
/**
* analyze the modules in a file.
* @param inputPath
* @return {Object}
*/
analyze: function(inputPath){
return ModuleCompiler.analyze(inputPath);
},
/**
* build a file or a directory
* @param inputPath {String} the source file location.
* @param outputPath {String} the combined file location.
* @param outputCharset {String} the combined file charset. default to 'utf-8'
* @return {Object}
*/
build: function(inputPath, outputPath, outputCharset){
var target = path.resolve(inputPath),
result = {
'success': true,
'files': []
};
if(fs.existsSync(target)){
if(fs.statSync(target).isDirectory()) {
var targets = fs.readdirSync(target);
for (var i in targets) {
if(!ModuleCompiler.isFileIgnored(targets[i])){
var inputFile = path.resolve(target, targets[i]),
outputFile = path.join(outputPath, targets[i]);
if(path.extname(inputFile)==='.js') {
result.files.push(ModuleCompiler.build(inputFile, outputFile, outputCharset));
}
}
}
} else {
result.files.push(ModuleCompiler.build(target, outputPath, outputCharset));
}
}else{
// MC.build('pkgName/abc');
var modulePath = ModuleCompiler.getModulePath(inputPath);
if(modulePath){
result.files.push(ModuleCompiler.build(modulePath, outputPath, outputCharset));
}else{
result.success = false;
!ModuleCompiler._config.silent && console.info('[err]'.bold.red + ' cannot find target: %s', target);
}
}
return result;
},
clean: function(){
ModuleCompiler._config = {
this._config = {
packages: [],
exclude: [],
ignoreFiles: [],
suffix: '',
charset: '',
combine: false,
silent: false
};
ModuleCompiler._packages = {};
ModuleCompiler._mappedRules = [];
ModuleCompiler._clean();
return true;
}
};
{
"name":"kmc",
"version":"1.0.25",
"description":"KISSY Module Compiler",
"author":"daxingplay <daxingplay@gmail.com>",
"homepage":"",
"keywords":[
"kissy",
"build",
"module",
"compiler"
],
"bugs":{
"url":"https://github.com/daxingplay/ModuleCompiler/issues"
},
"contributors":[],
"engines":{
"node":">=0.8.0"
},
"directories":{
"lib":"./lib/"
},
"main":"./index.js",
"dependencies":{
"iconv-lite":">=0.1.0",
"colors":">=0.6.0-1",
"lodash": "",
"uglify-js": ""
},
"devDependencies": {
"mocha": "~1.4.1",
"should": "~0.6.3"
},
"scripts": {
"test": "mocha"
},
"licenses":[
{
"type":"MIT",
"url":"https://github.com/daxingplay/ModuleCompiler/blob/master/LICENSE.md"
}
],
"repository":{
"type":"git",
"url":"git://github.com/daxingplay/ModuleCompiler.git"
"name": "kmc",
"version": "1.0.27",
"description": "KISSY Module Compiler",
"author": {
"name": "daxingplay",
"email": "daxingplay@gmail.com"
},
"homepage": "",
"keywords": [
"kissy",
"build",
"module",
"compiler"
],
"bugs": {
"url": "https://github.com/daxingplay/ModuleCompiler/issues"
},
"contributors": [],
"engines": {
"node": ">=0.8.0"
},
"directories": {
"lib": "./lib/"
},
"main": "./index.js",
"dependencies": {
"iconv-lite": ">=0.1.0",
"colors": ">=0.6.0-1",
"lodash": "",
"uglify-js": ""
},
"devDependencies": {
"mocha": "~1.4.1",
"should": "~0.6.3"
},
"scripts": {
"test": "mocha"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/daxingplay/ModuleCompiler/blob/master/LICENSE.md"
}
],
"repository": {
"type": "git",
"url": "git://github.com/daxingplay/ModuleCompiler.git"
},
"readme": "# ModuleCompiler\n\n[![Build Status](https://secure.travis-ci.org/daxingplay/ModuleCompiler.png)](http://travis-ci.org/daxingplay/ModuleCompiler)\n\n[![NPM version](https://badge.fury.io/js/kmc.png)](http://badge.fury.io/js/kmc)\n\n## 简介\n\nKISSY Module Compiler(kmc)是一个基于NodeJS的KISSY模块打包工具,目前适用于KISSY 1.2+的代码打包\n\n如果您有任何问题,请邮件或者来往(daxingplay)上联系我。\n\n## 特点\n\n- 支持GruntJS,参见[grunt-kmc](https://github.com/daxingplay/grunt-kmc).\n- 基于NodeJS,相比于KISSY自带的Java工具,打包快速\n- 参照浏览器端的KISSY的config进行配置,无需额外知识,只需要改一下包路径即能快速打包\n- 支持混合编码打包,不同的包可以使用不同的编码\n- 支持GBK输出\n- 支持KISSY 1.3的自动combo功能,可以生成依赖关系文件\n- 提供底层依赖分析接口,方便集成到其他工具当中\n- 支持map功能,可以使用正则自由替换输出的模块名\n\n## 版本说明\n\n- 0.0.7版本适用于KISSY 1.2、1.3的打包,目前已经在淘宝多个业务广泛使用,单纯打包没有任何问题,但是不具备依赖分析生成功能,此版本已经不再维护,推荐使用新版本。\n- 1.0.0版本开始支持KISSY 1.3的自动combo功能,推荐使用\n\n## 使用\n\n### 安装\n npm install kmc\n\nor\n\n git clone git://github.com/daxingplay/kmc.git\n\n**注意**: 新版本的KISSY Module Compiler的npm包已经更名为kmc,如果使用老版本(此版已经不再维护),请`npm install module-compiler`\n\n### 编写你的打包脚本\n\n```js\nvar kmc = require('kmc');\n\n// 这里和KISSY.config一样,先配置包\nkmc.config({\n packages: [{\n 'name': 'sh',\n 'path': '这里建议写绝对路径,即sh这个包所在的目录',\n 'charset': 'gbk'\n }]\n});\n\n// 将xxx.js打包为xxx.combine.js,输出编码为GBK\nkmc.build('xxx.js', 'xxx.combine.js', 'gbk');\n\n// 用node执行你这个打包脚本就ok啦~\n```\n\n### 高级使用指南\n\n```js\nvar kmc = require('kmc');\n\nkmc.config({\n // 和KISSY一样,可以配置多个包\n packages: [{\n 'name': 'app1',\n 'path': 'app1这个包所在目录的绝对路径',\n // 这里是指app1这个包中的文件的编码,同一个包内的编码请保持一致\n 'charset': 'gbk'\n }, {\n 'name': 'app2',\n 'path': 'app2这个包所在目录的绝对路径',\n // 这里是指app2这个包源码的编码\n 'charset': 'utf-8'\n }],\n // 可以设置哪些模块不打包进来。注意,这里exclude的是具体的模块名,支持正则\n exclude: ['base', 'event'],\n // 如果是对一个目录下的所有文件进行打包,可以设置哪些文件不打包进来,支持正则。注意和上面的exclude的配置的区别。\n ignoreFiles: ['.combo.js', '-min.js'],\n // 输出的文件名后缀,不带.js,比如打包后你想输出为xxx.combine.js,那么这里就配置为:.combine\n suffix: '',\n // 类似于KISSY的map方法,可以自己定义把模块名中的路径进行替换\n map: [\n // 这样配置的话,那么,如果原先输出的app1的模块名中含有app1/2.0/字样的话,就会被替换成app1/19891014/\n ['app1/2.0/', 'app1/19891014/']\n ],\n // 这里设置的是最后打包出来的文件的编码,默认UTF-8,这里的设置相当于是全局设置,下面build中的设置是针对单一打包实例的\n charset: 'gbk'\n});\n\n/**\n * 打包一个文件/目录\n * @param src {String} 源文件/目录的绝对路径.\n * @param dest {String} 打包出来的文件/目录的路径.\n * @param outputCharset {String} 输出编码,这里的设置会覆盖config.charset中的设置,默认UTF-8\n * @return {Object} 打包出来的文件信息\n */\nkmc.build({\n src: 'xxx.js',\n dest: 'xxx.combine.js',\n outputCharset: 'gbk'\n});\n```\n\n更详细的文档,请参见[wiki](https://github.com/daxingplay/kmc/wiki)。\n\n### API汇总\n\n#### 配置\n\n配置包,返回当前所有配置信息。如果不带参数,直接返回当前所有配置信息。\n\n```js\nkmc.config(cfg);\n```\n\n#### 分析依赖\n\n只分析该文件依赖,不打包。\n\n```js\nkmc.analyze(src);\n```\n\n#### 打包\n\n打包函数,具体见wiki\n\n```js\nkmc.build({\n\tsrc: '\\xxx\\xxx\\src\\a.js', // 需要打包的文件\n\tdest: '\\xxx\\xxx\\build\\a.js', // 打包后文件输出的路径\n\toutputCharset: 'gbk', // 所有的输出文件编码,包括依赖文件\n\tdepPath: '\\xxx\\xxx\\dep.js' // 依赖文件的路径\n});\n```\n\n\n#### 生成依赖\n\n不打包,只生成KISSY 1.3的自动combo依赖文件\n\n```js\nkmc.combo({\n\tsrc: '', // 需要打包or生成依赖关系的文件\n\tdest: '', // 模块最后输出的路径(fixModuleName === true的时候必须配置这个选项,否则源文件的内容会被修改)\n\tdepPath: '', // 依赖文件的输出路径\n\tdepCharset: '', // 依赖文件的编码\n\tfixModuleName: '', // 是否给文件添加模块名,默认为true\n\tshowFullResult: '' // 函数的return结果会以对象的形式输出丰富的信息\n});\n```\n\n#### 清空配置\n\n可以清空config中的设置。因为ModuleCompiler是单例运行,所以如果出现一些特别情况,可以在config前执行clean方法清空之前的配置。\n\n```js\nkmc.clean();\n```\n\n\n## CHANGELOG\n\n[版本更新记录](https://github.com/daxingplay/kmc/blob/master/HISTORY.md)\n\n## License\n遵守 \"MIT\":https://github.com/daxingplay/kmc/blob/master/LICENSE.md 协议",
"readmeFilename": "README.md",
"_id": "kmc@1.0.26",
"_from": "kmc@~1.0.26"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc