├── .gitignore ├── .npmignore ├── README.md ├── index.js ├── libs └── dep_store.js ├── package.json └── test ├── dest └── apps │ ├── app.js │ ├── app2.js │ ├── app3.js │ └── app4.js ├── gulpfile.js └── src ├── apps ├── app.js ├── app2.js ├── app3.js └── app4.js └── views ├── a.js ├── b.js ├── c.js └── d.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | .swap 4 | .swp 5 | .orig 6 | .log 7 | npm-debug.log 8 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | .swap 4 | .orig 5 | .log 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-alias-combo [![npm](https://img.shields.io/npm/v/gulp-alias-combo.svg)](https://www.npmjs.com/package/gulp-alias-combo) [![npm](https://img.shields.io/npm/dt/gulp-alias-combo.svg)](https://www.npmjs.com/package/gulp-alias-combo) 2 | 3 | > 一个根据配置合并js文件的gulp插件,合并时会自动提取模块间的依赖 4 | 5 | ## Install 6 | 7 | ``` 8 | npm install gulp-alias-combo --save-dev 9 | ``` 10 | 11 | 安装依赖 12 | 13 | ``` 14 | npm install 15 | ``` 16 | 17 | ## 配置参数 18 | 19 | #### baseUrl 20 | 21 | 必须参数,要构建项目的根路径,以斜杠结束 22 | 23 | #### supportRelative { Boolean } 24 | 25 | 可选参数,默认是false,为true时开启对相对路径的支持 26 | 27 | #### alias { Object } 28 | 29 | 别名配置,当supportRelative为false时为必须参数,supportRelative为true是为可选参数,alias配置对象的key 为模块的别名(模块ID),value 为模块的路径,baseUrl+此处配置的路径就是模块的绝对路径, 如果需要给入口模块自定义ID,需要在alias中进行配置,key为入口模块ID,value为入口模块的路径,默认的入口模块ID是入口模块相对于baseUrl的相对路径 30 | 31 | 32 | #### paths { Object } 33 | 34 | 可选参数,路径的简写,只有supportRelative为true时,才会起作用,例如: 35 | 36 | ``` 37 | var test = require('views/test') 38 | 39 | //如果配置了paths 40 | paths: { 41 | 'views': 'apps/modules/' 42 | } 43 | 44 | //解析后变为 45 | var test = require('apps/modules/test') 46 | ``` 47 | 48 | #### exclude { Array } 49 | 50 | 可选参数, 要忽略的模块ID,合并时在exclude配置的模块ID会直接忽略 51 | 52 | #### parseAllDefine { Boolean } 53 | 54 | 可选参数,默认是false,当以一个文件里有多个 define 时,是否都要添加模块 ID 55 | 56 | #### moduleIdPrefix { String } 57 | 58 | 可选参数,默认是 '',分析模块 ID 时,自动添加模块 ID 的前缀,alias 配置的 模块 ID 不会添加此前缀。 59 | 60 | ## Usage 61 | 62 | ### 使用场景 63 | 64 | gulp-alias-combo插件主要目的是合并seajs/requirejs中依赖的模块,会把所 65 | 有依赖的模块合并到入口js文件中。合并过程中会自动提取依赖模块,不会出现 66 | 重复合并。 67 | 68 | ### 使用样例 69 | 70 | ``` 71 | var gulp = require('gulp') 72 | var aliasCombo = require('gulp-alias-combo') 73 | 74 | gulp.task('combo', function(){ 75 | return gulp.src('src/apps/*.js') 76 | .pipe(aliasCombo({ 77 | baseUrl: __dirname + '/src/', 78 | //supportRelative 默认为 false, 如果要支持相对路径模块的合并,设置为true 79 | supportRelative: true, 80 | alias: { 81 | 'test/a': 'views/a.js', 82 | //会自动添加.js后缀 83 | 'test/b': 'views/b', 84 | 'app': 'apps/app.js' 85 | } 86 | })) 87 | .pipe(gulp.dest('dest/apps')); 88 | }) 89 | ``` 90 | 91 | ### 合并规则 92 | 93 | 1. supportRelative 为 false 时,如果在alias中没有配置的别名,在合并时会忽略,不会进行合并操作。 94 | 2. supportRelative 为 true 时,会合并所有依赖,如果要排除特定的某些模块,在 exclude 中配置。 95 | 3. 合并相对路径的模块时,模块的ID为模块路径相对于baseUrl的相对路径。 96 | 4. 要合并的模块不要指定模块ID,合并的时候会根据alias配置设置模块的ID,模块要使用CommonJS标准写法。即: 97 | ``` 98 | defind(function(require, exports, module){ 99 | //code 100 | }) 101 | ``` 102 | #### 举例 103 | 104 | ##### 第一种 适用 Seajs 的入口文件 105 | 106 | 要合并的入口文件: 107 | 108 | ``` 109 | define(function(require){ 110 | var a = require('test/a') 111 | var b = require('test/b') 112 | }) 113 | ``` 114 | 合并后: 115 | 116 | ``` 117 | define(function(require){ 118 | var a = require('test/a') 119 | var b = require('test/b') 120 | }) 121 | 122 | define('test/a', function(require, exports){ 123 | //code 124 | }) 125 | 126 | define('test/b', function(require, exports){ 127 | //code 128 | }) 129 | ``` 130 | 131 | ##### 第二种 适用 requirejs 的入口文件 132 | 133 | 要合并的入口文件: 134 | 135 | ``` 136 | require(['test/a', 'test/b'], function(a, b){ 137 | //code 138 | }) 139 | ``` 140 | 合并后: 141 | 142 | ``` 143 | require(['test/a', 'test/b'], function(a, b){ 144 | //code 145 | }) 146 | 147 | define('test/a', function(require, exports){ 148 | //code 149 | }) 150 | 151 | define('test/b', function(require, exports){ 152 | //code 153 | }) 154 | ``` 155 | 156 | ### 运行 157 | ``` 158 | gulp combo 159 | ``` 160 | 运行完成后,会打印合并日志: 161 | ``` 162 | [16:44:18] build /work/build/src/apps/app.js: 163 | test/a:[/work/build/src/views/a.js] 164 | test/b:[/work/build/src/views/b.js] 165 | ``` 166 | 167 | ## License 168 | 169 | MIT @ [Peter Mu](https://github.com/PeterMu) 170 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * alias module combo pulgin for gulp 3 | * Author : mukezhai@gmail.com 4 | * Date : 2015-06-03 5 | */ 6 | 7 | var through = require('through2') 8 | var gutil = require('gulp-util') 9 | var fs = require('fs') 10 | var path = require('path') 11 | var PLUGIN_NAME = 'gulp-alias-combo' 12 | var requireReg = /require\s*\(\s*(["'])(.+?)\1\s*\)/g 13 | var requirejsReg = /require(js)?\s*\(\s*\[(.+?)\]/g 14 | var commentReg = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg 15 | var jsFileReg = /^.+\.js$/ 16 | var DepStore = require('./libs/dep_store') 17 | 18 | /* 19 | * 提取模块中的依赖 20 | * param { String } content 文件内容 21 | * param { Object } options 配置参数 22 | * param { String } filePath 文件路径 23 | * param { Object } depStore 依赖存储对象 24 | * return 提取的依赖存到 options 里 25 | */ 26 | function analyseDeps(content, filePath, options, depStore){ 27 | var relativePath = '', parsedDep = null, deps = null 28 | deps = getDeps(content, options.exclude) 29 | if(deps.length > 0){ 30 | deps.forEach(function(dep){ 31 | if(dep){ 32 | if(options.alias && options.alias[dep]){ 33 | depStore.addAlias(dep, mergePath(dep, options)) 34 | analyseDeps( 35 | readModule(dep, depStore.getAlias(dep)), 36 | depStore.getAlias(dep), 37 | options, 38 | depStore 39 | ) 40 | }else{ 41 | if(options.supportRelative && !depStore.hasAlias(dep)){ 42 | relativePath = getRelativePath(filePath, dep, options) 43 | parsedDep = parseDep(relativePath, options.baseUrl, options.moduleIdPrefix) 44 | depStore.addAlias(parsedDep, relativePath) 45 | if(!depStore.hasRelative(dep)){ 46 | depStore.addRelative(dep, parsedDep) 47 | analyseDeps( 48 | readModule(parsedDep, relativePath), 49 | relativePath, 50 | options, 51 | depStore 52 | ) 53 | } 54 | } 55 | } 56 | } 57 | }) 58 | } 59 | } 60 | 61 | /* 62 | * 使用正则提取依赖 63 | * param { String } content 内容 64 | * return { Array } 提取的依赖 65 | */ 66 | function getDeps(content, exclude){ 67 | var deps = [], moduleId, moduleIds, result = [] 68 | var requires = content.match(requireReg) 69 | if(requires){ 70 | requires.forEach(function(dep){ 71 | moduleId = dep.substring(dep.indexOf('(') + 1, dep.lastIndexOf(')')).trim() 72 | moduleId = moduleId.substring(1, moduleId.length-1) 73 | deps.push(moduleId) 74 | }) 75 | } 76 | requires = content.match(requirejsReg) 77 | if(requires){ 78 | requires.forEach(function(dep){ 79 | try { 80 | moduleIds = eval(dep.substring(dep.indexOf('['), dep.lastIndexOf(']') + 1)) 81 | if(moduleIds && moduleIds.length > 0){ 82 | deps = deps.concat(moduleIds) 83 | } 84 | }catch(e){} 85 | }) 86 | } 87 | if(exclude && (exclude instanceof Array)){ 88 | for(var i=0,l=deps.length; i