├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .gitignore ├── LICENSE ├── README.md ├── bin └── labrador ├── lib ├── build-js.js ├── build-less.js ├── build-sass.js ├── build-xml.js ├── build.js ├── config.js ├── create.js ├── generate-component.js ├── generate-page.js ├── generate-redux.js ├── generate-saga.js ├── labrador.js ├── minify-js.js ├── minify-page.js ├── types.js ├── utils.js └── watch.js ├── package.json └── templates ├── component ├── component.js ├── component.less ├── component.scss ├── component.test.js └── component.xml ├── redux └── index.js └── saga └── index.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | charset = utf-8 6 | trim_trailing_whitespace = false 7 | insert_final_newline = true 8 | indent_style = space 9 | indent_size = 2 10 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | dist/* 2 | node_modules/* 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "env": { 4 | "node": true 5 | }, 6 | "globals": { 7 | "rm": true, 8 | "cp": true, 9 | "exec": true 10 | }, 11 | "parser": "babel-eslint", 12 | "rules": { 13 | "no-console": 0, 14 | "prefer-const": 0, 15 | "prefer-template": 0, 16 | "no-param-reassign": 0, 17 | "comma-dangle": [1, "never"], 18 | "spaced-comment": [0, "always"], 19 | "func-names": 0, 20 | "no-underscore-dangle": 0, 21 | "import/prefer-default-export": 1, 22 | "class-methods-use-this": 1, 23 | "no-prototype-builtins": 0, 24 | "import/no-extraneous-dependencies": 1, 25 | "radix": 0 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | *.log 3 | npm-debug.log* 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # nyc test coverage 17 | .nyc_output 18 | 19 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 20 | .grunt 21 | 22 | # node-waf configuration 23 | .lock-wscript 24 | 25 | # Compiled binary addons (http://nodejs.org/api/addons.html) 26 | build/Release 27 | 28 | # Dependency directories 29 | node_modules 30 | jspm_packages 31 | 32 | # Optional npm cache directory 33 | .npm 34 | 35 | # Optional REPL history 36 | .node_repl_history 37 | yarn.lock 38 | 39 | # idea 40 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Maichong Software 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | #[labrador-cli](https://github.com/maichong/labrador-cli) 3 | 4 | 微信小程序模块化开发框架,Labrador命令行工具 5 | 6 | 使用手册参见 [https://github.com/maichong/labrador](https://github.com/maichong/labrador) 7 | 8 | ## Contribute 9 | 10 | [Maichong Software](https://maichong.it) 11 | 12 | [Liang Xingchen](https://github.com/liangxingchen) 13 | 14 | ## License 15 | 16 | This project is licensed under the terms of the MIT license 17 | -------------------------------------------------------------------------------- /bin/labrador: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict'; 4 | 5 | process.title = 'labrador'; 6 | 7 | require('../lib/labrador'); 8 | -------------------------------------------------------------------------------- /lib/build-js.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-09-25 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const fs = require('fs'); 10 | const path = require('path'); 11 | const mkdirp = require('mkdirp'); 12 | const slash = require('slash'); 13 | const utils = require('./utils'); 14 | const config = require('./config')(); 15 | const version = require('../package.json').version; 16 | require('colors'); 17 | 18 | /** 19 | * 编译JS文件 20 | * @param {FileInfo|string} from 源文件绝对路径 21 | * @param {FileInfo|string} to 目标文件绝对路径 22 | * @param {Object} targets 编译目标文件列表 23 | * @param {Object} metadata 编译信息 24 | */ 25 | module.exports = function* buildJS(from, to, targets, metadata) { 26 | if (typeof from === 'string') { 27 | from = utils.getInfo(from); 28 | } 29 | if (typeof to === 'string') { 30 | to = utils.getInfo(to); 31 | } 32 | if (targets[to.file]) return; 33 | const distNpmDir = config.distDir + '/npm/'; 34 | const isNPM = utils.inNpm(from.file); 35 | const isTest = /\.test\.js$/.test(from.file); 36 | const isPage = utils.inPages(from.file) && !isTest; 37 | if (isTest && !process.env.TEST) { 38 | console.log((isNPM ? '\tignore test' : 'ignore test').yellow, from.relative.gray); 39 | return; 40 | } 41 | targets[to.file] = true; 42 | 43 | if (!utils.isChanged(from.file, to.file, metadata)) { 44 | console.log((isNPM ? '\tignore unchanged' : 'ignore unchanged').yellow, from.relative.gray); 45 | if (metadata[from.file].depends) { 46 | for (let f in metadata[from.file].depends) { 47 | yield* buildJS(f, metadata[from.file].depends[f], targets, metadata); 48 | } 49 | } 50 | return; 51 | } 52 | const depends = metadata[from.file].depends = {}; 53 | metadata[from.file].v = version; 54 | 55 | const isApp = from.fromSrc === 'app.js'; 56 | const relativePath = slash(to.fromDist); 57 | console.log((isNPM ? '\tbuild js' : 'build js').green, from.relative.blue, '->', to.relative.cyan); 58 | 59 | let testPath; 60 | let relativeTestPath; 61 | if (!isTest && process.env.TEST) { 62 | let file = path.join(from.dir, from.name + '.test.js'); 63 | if (utils.isFile(file)) { 64 | testPath = './' + from.name + '.test.js'; 65 | relativeTestPath = path.relative(config.srcDir, file); 66 | } 67 | } 68 | 69 | let code = fs.readFileSync(from.file, 'utf8'); 70 | 71 | if (!utils.shouldBabelIgnore(from.relative)) { 72 | const babel = require(config.modulesDir + 'babel-core'); 73 | code = babel.transform(code, Object.assign({}, config.babelConfig, { 74 | sourceMaps: process.env.NODE_ENV === 'development' ? 'inline' : false, 75 | sourceFileName: from.base 76 | })).code; 77 | if (process.env.NODE_ENV === 'development') { 78 | code = code.replace('sourceMappingURL=data:application/json;base64', 'sourceMappingURL=data:application/json;charset=utf-8;base64;base64'); 79 | } 80 | } else { 81 | //console.log('babel ignored'); 82 | } 83 | 84 | code = code.replace(/'use strict';\n?/g, ''); 85 | 86 | //如果代码中引用了global或window 则加载'labrador/global'尝试兼容 87 | if (!process.env.MINIFY && /global|window/.test(code)) { 88 | code = "var global=window=require('labrador/global');" + code; 89 | } 90 | 91 | if (code.indexOf('__DEBUG__') > -1) { 92 | throw new Error(`__DEBUG__ 已经弃用,请使用 __DEV__ 代替`); 93 | } 94 | 95 | code = code.replace(/__DEV__/g, process.env.NODE_ENV === 'development' ? 'true' : 'false'); 96 | code = code.replace(/process\.env\.NODE_ENV/g, JSON.stringify(process.env.NODE_ENV)); 97 | if (config.define) { 98 | for (let key in config.define) { 99 | let value = config.define[key]; 100 | code = code.replace(new RegExp(utils.escapeRegExp(key), 'g'), JSON.stringify(value)); 101 | } 102 | } 103 | 104 | if (/[^\w_]process\.\w/.test(code) && !/typeof process/.test(code)) { 105 | code = `var process={};${code}`; 106 | } 107 | 108 | //转换 foobar instanceof Function 为 typeof foobar ==='function' 109 | //由于微信重定义了全局的Function对象,所以moment等npm库会出现异常 110 | code = code.replace(/([\w\[\]a-d\.]+)\s*instanceof Function/g, function (matchs, word) { 111 | return ' typeof ' + word + " ==='function' "; 112 | }); 113 | 114 | if (isPage) { 115 | let defaultExport = 'exports.default'; 116 | let lastExport = code.match(/exports\.default\s*=\s*(\w+);/ig); 117 | let matchs = lastExport && lastExport.pop().match(/exports\.default\s*=\s*(\w+);/i); 118 | if (matchs) { 119 | defaultExport = matchs[1]; 120 | code = code.replace(/exports\.default\s*=\s*(\w+);/i, ''); 121 | } 122 | 123 | if (testPath) { 124 | defaultExport = `require('labrador-test')(${defaultExport},require('${testPath}'),'${relativeTestPath}')`; 125 | } 126 | 127 | if (code.indexOf('var _labrador = require(') > -1) { 128 | code += `\nPage(_labrador._createPage(${defaultExport}));\n`; 129 | } else { 130 | code += `\nPage(require('labrador')._createPage(${defaultExport}));\n`; 131 | } 132 | } else { 133 | if (testPath) { 134 | code += `\nmodule.exports=require('labrador-test')(module.exports,require('${testPath}'),'${relativeTestPath}');`; 135 | } 136 | } 137 | 138 | let promises = []; 139 | 140 | code = code.replace(/require\(['"]([\w\d_\-\.\/\@]+)['"]\)/ig, function (match, lib) { 141 | //如果引用文件是相对位置引用,并且当前文件不是NPM包文件,不存在映射 142 | if (lib[0] === '.' && !isNPM) { 143 | let file = path.join(path.dirname(from.file), lib); 144 | //兼容省略了.js的路径 145 | if (!utils.isFile(file) && utils.isFile(file + '.js')) { 146 | lib += '.js'; 147 | } 148 | //兼容省略了/index.js的路径 149 | if (!utils.isFile(file) && utils.isFile(file + '/index.js')) { 150 | lib += '/index.js'; 151 | } 152 | return `require('${lib}')`; 153 | } 154 | 155 | //如果引用NPM包文件 156 | let source; 157 | let target; 158 | let isPrivatePackReg = /^\@[\w\d\-\_\.]+\/[\w\d\-\_\.]+$/; 159 | if (lib.indexOf('/') === -1 || lib.indexOf('/') === lib.length - 1 || isPrivatePackReg.test(lib)) { 160 | //只指定了包名 161 | if (!isPrivatePackReg.test(lib)) lib = lib.replace(/\//, ''); 162 | if (config.npmMap && config.npmMap.hasOwnProperty(lib)) { 163 | lib = config.npmMap[lib]; 164 | } 165 | let dir = path.join(config.modulesDir, lib); 166 | let pkgFile = path.join(dir, '/package.json'); 167 | if (utils.isFile(pkgFile)) { 168 | let pkg = utils.readJSON(pkgFile); 169 | let main = pkg.main || 'index.js'; 170 | if (pkg.labrador && typeof pkg.labrador === 'string') { 171 | main = pkg.labrador; 172 | } else if (pkg.browser && typeof pkg.browser === 'string') { 173 | main = pkg.browser; 174 | } else if (pkg['jsnext:main']) { 175 | main = pkg['jsnext:main']; 176 | } 177 | source = path.join(config.modulesDir, lib, main); 178 | } else { 179 | source = dir; 180 | } 181 | if (!utils.isFile(source)) { 182 | if (utils.isFile(source + '.js')) { 183 | source += '.js'; 184 | } else if (utils.isFile(source + '/index.js')) { 185 | source += '/index.js'; 186 | } 187 | } 188 | target = path.join(distNpmDir, path.relative(config.modulesDir, source)); 189 | } else { 190 | //如果还指定了包里边的路径 191 | lib = lib.replace(/^([\w\.\-\_\@]+)/i, function (name) { 192 | if (config.npmMap && config.npmMap.hasOwnProperty(name)) { 193 | return config.npmMap[name]; 194 | } 195 | return name; 196 | }); 197 | source = config.modulesDir + lib; 198 | target = distNpmDir + lib; 199 | if (lib[0] === '.') { 200 | source = path.join(from.dir, lib); 201 | target = path.join(to.dir, lib); 202 | } 203 | if (!utils.isFile(source) && utils.isFile(source + '.js')) { 204 | source += '.js'; 205 | target += '.js'; 206 | } else if (utils.isDirectory(source)) { 207 | source += '/index.js'; 208 | target += '/index.js'; 209 | } 210 | if (!utils.isFile(source)) { 211 | console.log(source); 212 | throw new Error('Can not resolve ' + lib); 213 | } 214 | } 215 | source = path.normalize(source); 216 | target = path.normalize(target); 217 | 218 | let sourceRelative = slash(path.relative(config.modulesDir, source)); 219 | 220 | if (config.npmMap.hasOwnProperty(sourceRelative)) { 221 | //TODO log 222 | source = path.join(config.modulesDir, config.npmMap[sourceRelative]); 223 | target = path.join(config.distDir, 'npm', config.npmMap[sourceRelative]); 224 | } 225 | 226 | let relative = slash(path.relative(to.dir, target)); 227 | if (!targets[target]) { 228 | depends[source] = target; 229 | promises.push(buildJS(source, target, targets, metadata)); 230 | } 231 | 232 | relative = slash(relative); 233 | if (relative[0] !== '.') { 234 | relative = './' + relative; 235 | } 236 | 237 | return `require('${relative}')`; 238 | }); 239 | 240 | if (isApp) { 241 | code += `\n{\nvar __app=new exports.default();Object.getOwnPropertyNames(__app.constructor.prototype).forEach(function(name){if(name!=='constructor')__app[name]=__app.constructor.prototype[name]});App(__app);\n}`; 242 | } 243 | 244 | if (process.env.CATCH) { 245 | code = `\ntry{${code}\n}catch(error){console.error('JS载入失败 ${relativePath} '+error.stack);throw error;}`; 246 | } 247 | 248 | if (!process.env.MINIFY) { 249 | code = '"use strict";var exports=module.exports={};' + code; 250 | } 251 | 252 | mkdirp.sync(to.dir); 253 | fs.writeFileSync(to.file, code); 254 | metadata[to.file].mtime = utils.getModifiedTime(to.file).toString(); 255 | metadata[to.file].v = version; 256 | 257 | while (promises.length) { 258 | yield* promises.shift(); 259 | } 260 | }; 261 | -------------------------------------------------------------------------------- /lib/build-less.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-09-25 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const fs = require('fs'); 10 | const path = require('path'); 11 | const mkdirp = require('mkdirp'); 12 | const less = require('less'); 13 | const utils = require('./utils'); 14 | const config = require('./config')(); 15 | require('colors'); 16 | 17 | /** 18 | * @param {FileInfo} from 19 | * @param {Object} depends 20 | * @returns {string} 21 | */ 22 | function build(from, depends) { 23 | if (typeof from === 'string') { 24 | from = utils.getInfo(from); 25 | } 26 | const componentsPath = config.srcDir + 'components/'; 27 | 28 | let data = fs.readFileSync(from.file, 'utf8'); 29 | return data.replace(/@import\s+['"]([\w\d\.\-_\/]+)['"];?/ig, function (match, lib) { 30 | //尝试加载 相对目录或同一目录下指定的less文件 31 | let src = path.join(from.dir, lib); 32 | if (!utils.isFile(src)) { 33 | //尝试加载 没有指定less后缀时,相对目录或同一目录下指定的less文件 34 | src = path.join(from.dir, lib + '.less'); 35 | 36 | if (!utils.isFile(src)) { 37 | //尝试加载 components 目录下的通用组件样式 38 | src = path.join(componentsPath, lib, lib + '.less'); 39 | if (!utils.isFile(src)) { 40 | //尝试加载 node_modules 目录下的index.less 41 | src = path.join(config.modulesDir, lib, 'index.less'); 42 | 43 | if (!utils.isFile(src)) { 44 | //尝试加载 node_modules 目录下的指定less文件 45 | src = path.join(config.modulesDir, lib); 46 | } 47 | } 48 | } 49 | } 50 | 51 | if (!utils.isFile(src)) { 52 | throw new Error(`Can not import less file '${lib}' from ` + from.relative); 53 | } 54 | depends[src] = true; 55 | return build(src, depends); 56 | }); 57 | } 58 | 59 | /** 60 | * 编译LESS 61 | * @param {FileInfo} from 62 | * @param {FileInfo} to 63 | * @returns {Array} 64 | */ 65 | module.exports = function* buildLess(from, to) { 66 | console.log('build less'.green, from.relative.blue, '->', to.relative.cyan); 67 | let depends = {}; 68 | let data = build(from, depends); 69 | let options = { 70 | paths: [from.dir] 71 | }; 72 | let result = yield less.render(data, options); 73 | if (!result.css) return []; 74 | mkdirp.sync(to.dir); 75 | fs.writeFileSync(to.file, result.css); 76 | return Object.keys(depends); 77 | }; 78 | -------------------------------------------------------------------------------- /lib/build-sass.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-09-25 4 | * @author soulwu 5 | */ 6 | 'use strict'; 7 | 8 | const fs = require('fs'); 9 | const path = require('path'); 10 | const mkdirp = require('mkdirp'); 11 | const promisify = require('es6-promisify'); 12 | const utils = require('./utils'); 13 | const config = require('./config')(); 14 | require('colors'); 15 | 16 | /** 17 | * @param {FileInfo} from 18 | * @param {Object} depends 19 | * @returns {string} 20 | */ 21 | function build(from, depends) { 22 | if (typeof from === 'string') { 23 | from = utils.getInfo(from); 24 | } 25 | const componentsPath = config.srcDir + 'components/'; 26 | 27 | let data = fs.readFileSync(from.file, 'utf8'); 28 | return data.replace(/@import\s+['"]([\w\d\.\-_\/]+)['"];?/ig, (match, lib) => { 29 | //尝试加载 相对目录或同一目录下指定的scss文件 30 | let src = path.join(from.dir, lib); 31 | if (!utils.isFile(src)) { 32 | //尝试加载 没有指定scss后缀时,相对目录或同一目录下指定的scss文件 33 | src = path.join(from.dir, lib + '.scss'); 34 | if (!utils.isFile(src)) { 35 | //尝试加载 尝试.sass后缀 36 | src = path.join(from.dir, lib + '.sass'); 37 | if (!utils.isFile(src)) { 38 | //尝试加载 components 目录下的通用组件样式 39 | src = path.join(componentsPath, lib, lib + '.scss'); 40 | if (!utils.isFile(src)) { 41 | //尝试加载 components 目录下的.sass后缀 42 | src = path.join(componentsPath, lib, lib + '.sass'); 43 | if (!utils.isFile(src)) { 44 | //尝试加载 node_modules 目录下的index.scss 45 | src = path.join(config.modulesDir, lib, 'index.scss'); 46 | if (!utils.isFile(src)) { 47 | //尝试加载 node_modules 目录下的index.sass 48 | src = path.join(config.modulesDir, lib, 'index.sass'); 49 | if (!utils.isFile(src)) { 50 | //尝试加载 node_modules 目录下的指定scss文件 51 | src = path.join(config.modulesDir, lib); 52 | } 53 | } 54 | } 55 | } 56 | } 57 | } 58 | } 59 | 60 | if (!utils.isFile(src)) { 61 | throw new Error(`Can not import scss file '${lib}' from ` + from.relative); 62 | } 63 | depends[src] = true; 64 | return build(src, depends); 65 | }); 66 | } 67 | 68 | /** 69 | * 编译LESS 70 | * @param {FileInfo} from 71 | * @param {FileInfo} to 72 | * @returns {Array} 73 | */ 74 | module.exports = function* buildSass(from, to) { 75 | console.log('build sass'.green, from.relative.blue, '->', to.relative.cyan); 76 | let sass; 77 | try { 78 | sass = require(config.modulesDir + 'node-sass'); 79 | } catch (e) { 80 | console.log('\nnode-sass 加载失败,请在项目中运行 '.red + 'npm install --save node-sass'.blue + ' 安装node-sass\n'.red); 81 | throw e; 82 | } 83 | const render = promisify(sass.render); 84 | let depends = {}; 85 | let data = build(from, depends); 86 | let result = yield render({ data, outputStyle: 'expanded', includePaths: [from.dir] }); 87 | if (!result.css) return []; 88 | mkdirp.sync(to.dir); 89 | fs.writeFileSync(to.file, result.css); 90 | return Object.keys(depends); 91 | }; 92 | -------------------------------------------------------------------------------- /lib/build-xml.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-09-26 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const fs = require('fs'); 10 | const path = require('path'); 11 | const mkdirp = require('mkdirp'); 12 | const xmldom = require('xmldom'); 13 | const utils = require('./utils'); 14 | const config = require('./config')(); 15 | require('colors'); 16 | 17 | const DOMParser = xmldom.DOMParser; 18 | 19 | let _uid = 0; 20 | function uid() { 21 | _uid++; 22 | return _uid; 23 | } 24 | /** 25 | * 判断字符串中指定的位置是否是被包含在引号中 26 | * @param string 27 | * @param n 28 | * @returns {boolean} 29 | */ 30 | function inText(string, n) { 31 | let firstIndex = string.search(/"|'/); 32 | if (firstIndex === -1 || firstIndex > n) return false; 33 | let char = ''; 34 | let last = ''; 35 | for (let i = 0; i < n; i++) { 36 | let c = string[i]; 37 | if (c === '"' || c === "'") { 38 | if (!char) { 39 | char = c; 40 | } else if (char === c && last !== '\\') { 41 | char = ''; 42 | } 43 | } 44 | last = c; 45 | } 46 | return char !== ''; 47 | } 48 | 49 | /** 50 | * 将带数据绑定的字符串替换 51 | * @param {Object} from 52 | * @param {string} str 原始字符串 53 | * @param {string} prefix 前缀 54 | * @param {object} ignores 忽略的字符串map 55 | * @returns {string} 56 | */ 57 | function replaceString(from, str, prefix, ignores) { 58 | // 替换字符串中 {{}} 包含的表达式 59 | 60 | // 获取类似 a.b.c 表达式中第一个有效变量名 a 61 | function getFirstWord(word) { 62 | return word.match(/[_a-z][\w\d]*/i)[0]; 63 | } 64 | 65 | // 检查类似 a.b.c 格式表达式是否忽略绑定 66 | function shouldIgnore(word, matchs, n) { 67 | if (word[0] === '"' || word[0] === "'" || /^\d+$/.test(word)) return true; 68 | let w = getFirstWord(word); 69 | if (ignores.hasOwnProperty(w) || (matchs && inText(matchs, n))) { 70 | return true; 71 | } 72 | if (['state', 'props'].indexOf(w) < 0) { 73 | console.error(`'${from.fromSrc}' 中发现无效变量引用 '${word}',XML模板中只能引用组件'props'和'state'中的数据。`.red); 74 | console.error('如果您的项目基于Labrador 0.5.x,请按照升级指南升级到0.6.x版本 https://github.com/maichong/labrador'); 75 | } 76 | 77 | return false; 78 | } 79 | 80 | if (prefix) { 81 | prefix += '.'; 82 | } else { 83 | prefix = ''; 84 | } 85 | return str.replace(/\{\{([^}]+)\}\}/ig, function (matchs, words) { 86 | // matchs 是{{xxxxx}}格式的字符串 87 | // words 是{{}}中间的表达式 88 | 89 | // ...foo 90 | if (/^\s*\.\.\.[\w_][\w\d\-_.\[\]]*\s*$/.test(words)) { 91 | let word = words.match(/\s*\.\.\.([\w_][\w\d\-_.\[\]]*)/)[1].trim(); 92 | if (shouldIgnore(word)) { 93 | return matchs; 94 | } 95 | return `{{...${prefix}${word}}}`; 96 | } 97 | 98 | let isArray = /{{\s*\[/.test(matchs); 99 | if (!isArray) { 100 | //支持对象简写 101 | let arrays = words.split(','); 102 | if (arrays.length > 1) { 103 | let isObject = true; 104 | let props = arrays.map(function (str) { 105 | if (!isObject) return; 106 | // str 为对象中的一个属性, 可能为 a:b / a / ...a / ...a.b 107 | str = str.trim(); 108 | 109 | let arr = str.split(':'); 110 | if (arr.length === 1) { 111 | // 如果属性表达式中不包含冒号 112 | 113 | // 如果为简写属性表达式,例如 {foo} 114 | if (/^[a-z_][\w\d]*$/i.test(str)) { 115 | if (ignores[str]) { 116 | return str + ':' + str; 117 | } 118 | return str + ':' + prefix + str; 119 | } 120 | 121 | // 属性展开表达式 ...foo 122 | if (/^\.{3}[a-z_][\w\d.\[\]]*$/i.test(str)) { 123 | let word = str.substr(3); 124 | if (shouldIgnore(word)) { 125 | return str; 126 | } 127 | return '...' + prefix + word; 128 | } 129 | 130 | // 判定 ${matchs} 不为对象表达式 131 | isObject = false; 132 | return; 133 | } 134 | 135 | // 存在冒号的对象属性表达式 136 | 137 | let word = arr[1].trim(); 138 | // foo:2.3 139 | if (/^[\d.]+$/.test(word)) { 140 | return arr[0] + ':' + word; 141 | } 142 | 143 | // foo:bar 144 | // 'foo':bar 145 | if (shouldIgnore(word)) { 146 | return str; 147 | } 148 | 149 | // foo:bar 150 | // 'foo':bar 151 | // foo 152 | return arr[0] + ':' + prefix + word; 153 | }); 154 | 155 | //console.log('isObject', isObject); 156 | if (isObject) { 157 | return '{{' + props.join(',') + '}}'; 158 | } 159 | } 160 | } 161 | 162 | 163 | return matchs.replace(/[^\.\w'"]([a-z_\$][\w\d\._\$]*)/ig, function (match, word, n) { 164 | if (shouldIgnore(word, matchs, n)) { 165 | return match; 166 | } 167 | return match[0] + prefix + word; 168 | }); 169 | }); 170 | } 171 | 172 | /** 173 | * 递归绑定XML中的节点 174 | * @param from 175 | * @param node 176 | * @param comPrefix 177 | * @param valPrefix 178 | * @param clsPrefix 179 | * @param ignores 180 | */ 181 | function bind(from, node, comPrefix, valPrefix, clsPrefix, ignores) { 182 | ignores = Object.assign({ 183 | true: true, 184 | false: true, 185 | null: true, 186 | undefined: true 187 | }, ignores); 188 | 189 | let hasPath = false; 190 | 191 | //处理节点属性 192 | let attributes = node.attributes; 193 | for (let i in attributes) { 194 | if (!/^\d+$/.test(i)) continue; 195 | let attr = attributes[i]; 196 | 197 | //处理属性值 198 | if (attr.value.indexOf('{') > -1) { 199 | attr.value = replaceString(from, attr.value, valPrefix, ignores); 200 | } 201 | 202 | //绑定事件 203 | if (/^(bind|catch)\w+/.test(attr.name)) { 204 | node.setAttribute('data-' + attr.name, attr.value); 205 | attr.value = '_dispatch'; 206 | if (!hasPath && comPrefix) { 207 | node.setAttribute('data-path', comPrefix); 208 | } 209 | } 210 | 211 | //如果是循环标签,则在子标签中忽略循环索引和值变量 212 | if (attr.name === 'wx:for') { 213 | let index = node.getAttribute('wx:for-index') || 'index'; 214 | let item = node.getAttribute('wx:for-item') || 'item'; 215 | ignores[index] = true; 216 | ignores[item] = true; 217 | } 218 | 219 | if (clsPrefix && attr.name === 'class') { 220 | const matchArr = []; 221 | // "xxx {{a ? 'b' : 'c'}}" 222 | // => "xxx $" 223 | attr.value = attr.value.replace(/\{\{([^}]+)\}\}/ig, function (match) { 224 | matchArr.push(match); 225 | matchArr.push(match); 226 | return '$'; 227 | }); 228 | 229 | // => "xxx prefix-xxx $ prefix-$" 230 | attr.value = attr.value.split(' ').map(cls => `${cls} ${clsPrefix}-${cls}`).join(' '); 231 | 232 | // => "xxx prefix-xxx {{a ? 'b' : 'c'}} prefix-{{a ? 'b' : 'c'}}" 233 | attr.value = attr.value.replace(/\$/g, function () { 234 | const matchItem = matchArr.shift(); 235 | return matchItem; 236 | }); 237 | } 238 | } 239 | 240 | //如果节点为文本 241 | if (node.nodeName === '#text') { 242 | let data = node.data; 243 | if (data) { 244 | node.replaceData(0, data.length, replaceString(from, data, valPrefix, ignores)); 245 | } 246 | } 247 | 248 | //递归处理子节点 249 | for (let i in node.childNodes) { 250 | if (!/^\d+$/.test(i)) continue; 251 | let n = node.childNodes[i]; 252 | // 不转换template 定义 253 | if (n.nodeName === 'template' && n.getAttribute('name')) { 254 | bindTemplateEvents(n); 255 | continue; 256 | } 257 | bind(from, n, comPrefix, valPrefix, clsPrefix, ignores); 258 | } 259 | } 260 | 261 | /** 262 | * 递归绑定template标签子节点中的事件 263 | * @param node 264 | */ 265 | function bindTemplateEvents(node) { 266 | //处理节点属性 267 | let attributes = node.attributes; 268 | for (let i in attributes) { 269 | if (!/^\d+$/.test(i)) continue; 270 | let attr = attributes[i]; 271 | 272 | //绑定事件 273 | if (/^(bind|catch)\w+/.test(attr.name)) { 274 | node.setAttribute('data-' + attr.name, attr.value); 275 | attr.value = '_dispatch'; 276 | } 277 | } 278 | 279 | for (let i in node.childNodes) { 280 | if (!/^\d+$/.test(i)) continue; 281 | let n = node.childNodes[i]; 282 | bindTemplateEvents(n); 283 | } 284 | } 285 | 286 | /** 287 | * @param {FileInfo} from 288 | * @param {string} comPrefix 289 | * @param {string} valPrefix 290 | * @param {string} clsPrefix 291 | * @param {Object} depends 292 | * @returns {Document} 293 | */ 294 | function build(from, comPrefix, valPrefix, clsPrefix, depends) { 295 | if (typeof from === 'string') { 296 | from = utils.getInfo(from); 297 | } 298 | const components = config.srcDir + 'components/'; 299 | 300 | let data = fs.readFileSync(from.file, 'utf8'); 301 | 302 | if (!data) { 303 | throw new Error('XML file is empty ' + from.relative); 304 | } 305 | 306 | let doc = new DOMParser().parseFromString(data); 307 | 308 | bind(from, doc, comPrefix, valPrefix, clsPrefix); 309 | 310 | let listElemnts = doc.getElementsByTagName('list'); 311 | //console.log('listElemnts', listElemnts); 312 | 313 | for (let i = 0; i < listElemnts.$$length; i++) { 314 | let el = listElemnts[i]; 315 | let key = el.getAttribute('key'); 316 | let name = el.getAttribute('name') || key; 317 | if (!key) throw new Error('Unknown list key in ' + from.relative); 318 | let src; 319 | if (utils.isDirectory(path.join(components, name))) { 320 | //在components目录中 321 | src = path.join(components, name, name + '.xml'); 322 | } else if (utils.isFile(path.join(components, name + '.xml'))) { 323 | //在components目录中 324 | src = path.join(components, name + '.xml'); 325 | } else if (utils.isDirectory(path.join(config.modulesDir, name))) { 326 | //在node_modules目录中 327 | src = path.join(config.modulesDir, name, 'index.xml'); 328 | } else if (utils.isFile(path.join(config.modulesDir, name + '.xml'))) { 329 | //在node_modules目录中 330 | src = path.join(config.modulesDir, name + '.xml'); 331 | } else { 332 | throw new Error(`Can not find components "${name}" in ` + from.relative); 333 | } 334 | 335 | depends[src] = true; 336 | let id = uid(); 337 | let indexName = '_k' + id; 338 | let itemName = '_v' + id; 339 | let subComPrefix = comPrefix ? comPrefix + '.' + key : key; 340 | subComPrefix += '.{{' + itemName + '.__k}}'; 341 | let subValPrefix = valPrefix ? valPrefix + '.' + key : key; 342 | let subClsPrefix = clsPrefix ? clsPrefix + '-' + key : key; 343 | let listNode = doc.createElement('block'); 344 | listNode.setAttribute('wx:for', '{{' + subValPrefix + '}}'); 345 | listNode.setAttribute('wx:key', '__k'); 346 | listNode.setAttribute('wx:for-index', indexName); 347 | listNode.setAttribute('wx:for-item', itemName); 348 | el.parentNode.replaceChild(listNode, el); 349 | let ignores = {}; 350 | ignores[indexName] = true; 351 | ignores[itemName] = true; 352 | let node = build(src, subComPrefix, itemName, subClsPrefix, depends); 353 | listNode.appendChild(node); 354 | } 355 | 356 | let componentElements = doc.getElementsByTagName('component'); 357 | 358 | for (let i = 0; i < componentElements.$$length; i++) { 359 | let el = componentElements[i]; 360 | let key = el.getAttribute('key'); 361 | let name = el.getAttribute('name') || key; 362 | if (!key) throw new Error('Unknown component key in ' + from.relative); 363 | let src; 364 | if (utils.isDirectory(path.join(components, name))) { 365 | //在components目录中 366 | src = path.join(components, name, name + '.xml'); 367 | } else if (utils.isFile(path.join(components, name + '.xml'))) { 368 | //在components目录中 369 | src = path.join(components, name + '.xml'); 370 | } else if (utils.isDirectory(path.join(config.modulesDir, name))) { 371 | //在node_modules目录中 372 | src = path.join(config.modulesDir, name, 'index.xml'); 373 | } else if (utils.isFile(path.join(config.modulesDir, name + '.xml'))) { 374 | //在node_modules目录中 375 | src = path.join(config.modulesDir, name + '.xml'); 376 | } else { 377 | throw new Error(`Can not find components "${name}" in ` + from.relative); 378 | } 379 | depends[src] = true; 380 | let subComPrefix = comPrefix ? comPrefix + '.' + key : key; 381 | let subValPrefix = valPrefix ? valPrefix + '.' + key : key; 382 | let subClsPrefix = clsPrefix ? clsPrefix + '-' + key : key; 383 | let node = build(src, subComPrefix, subValPrefix, subClsPrefix, depends); 384 | el.parentNode.replaceChild(node, el); 385 | } 386 | return doc; 387 | } 388 | 389 | /** 390 | * 编译XML 391 | * @param {FileInfo} from 392 | * @param {FileInfo} to 393 | * @returns {Array} 394 | */ 395 | module.exports = function* buildXML(from, to) { 396 | console.log('build xml'.green, from.relative.blue, '->', to.relative.cyan); 397 | let depends = {}; 398 | let element = build(from, '', '', '', depends); 399 | mkdirp.sync(to.dir); 400 | let xml = element.toString(); 401 | xml = xml.replace(/&nbsp;/g, ' '); 402 | xml = xml.replace(/{{([^}]+)}}/g, function (matchs) { 403 | return matchs.replace(/</g, '<').replace(/&/g, '&'); 404 | }); 405 | fs.writeFileSync(to.file, xml); 406 | return Object.keys(depends); 407 | }; 408 | 409 | -------------------------------------------------------------------------------- /lib/build.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-09-25 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const co = require('co'); 10 | const path = require('path'); 11 | const fs = require('fs'); 12 | const mkdirp = require('mkdirp'); 13 | const UpdateNotifier = require('update-notifier').UpdateNotifier; 14 | const utils = require('./utils'); 15 | const buildJS = require('./build-js'); 16 | const buildLess = require('./build-less'); 17 | const buildSass = require('./build-sass'); 18 | const buildXML = require('./build-xml'); 19 | const minifyPage = require('./minify-page'); 20 | const minifyJs = require('./minify-js'); 21 | require('shelljs/global'); 22 | require('colors'); 23 | 24 | function* build(options) { 25 | const config = require('./config')(options); 26 | if (!utils.isDirectory(config.srcDir)) { 27 | throw new Error('源码目录不存在 ' + config.srcDir); 28 | } 29 | if (!utils.isFile(config.srcDir + 'app.json')) { 30 | throw new Error('app.json 不存在'); 31 | } 32 | 33 | let files = utils.getFileInfos(config.srcDir); 34 | 35 | mkdirp(config.tempDir); 36 | let metadataName = 'metadata'; 37 | if (process.env.NODE_ENV === 'development') { 38 | metadataName += '-dev'; 39 | } 40 | if (process.env.TEST) { 41 | metadataName += '-test'; 42 | } 43 | if (process.env.NODE_ENV !== 'development' && !process.env.TEST && process.env.CATCH) { 44 | metadataName += '-catch'; 45 | } 46 | let metadataPath = config.tempDir + metadataName + '.json'; 47 | let metadata = {}; 48 | if (!options.force && utils.isFile(metadataPath)) { 49 | metadata = utils.readJSON(metadataPath); 50 | } 51 | let to; 52 | let targets = {}; 53 | for (let from of files) { 54 | to = path.join(config.distDir, path.relative(config.srcDir, from.dir), from.name + utils.getDistFileExt(from.ext)); 55 | to = utils.getInfo(to); 56 | switch (from.ext) { 57 | case '.js': 58 | yield* buildJS(from, to, targets, metadata); 59 | break; 60 | case '.less': 61 | if (from.fromSrc === 'app.less' || utils.inPages(from.file)) { 62 | targets[to.file] = true; 63 | yield* buildLess(from, to); 64 | } else { 65 | console.log('ignore'.yellow, from.relative.gray); 66 | } 67 | break; 68 | case '.sass': 69 | case '.scss': 70 | if (from.fromSrc === 'app.sass' || from.fromSrc === 'app.scss' || utils.inPages(from.file)) { 71 | targets[to.file] = true; 72 | yield* buildSass(from, to); 73 | } else { 74 | console.log('ignore'.yellow, from.relative.gray); 75 | } 76 | break; 77 | case '.xml': 78 | if (utils.inPages(from.file) || utils.inTemplates(from.file)) { 79 | targets[to] = true; 80 | yield* buildXML(from, to); 81 | } else { 82 | console.log('ignore'.yellow, from.relative.gray); 83 | } 84 | break; 85 | default: 86 | targets[to] = true; 87 | console.log('copy'.green, from.relative.blue, '->', to.relative.cyan); 88 | mkdirp.sync(to.dir); 89 | cp(from.file, to.file); 90 | } 91 | } 92 | 93 | let distFiles = utils.getFileInfos(config.distDir); 94 | for (let file of distFiles) { 95 | if (!targets[file.file] && /\.js$/.test(file.file)) { 96 | utils.removeFile(file.file); 97 | } 98 | } 99 | utils.writeJson(metadataPath, metadata); 100 | 101 | if (process.env.MINIFY) { 102 | if (!options.ignoreMinifyJs) { 103 | yield* minifyJs(); 104 | } 105 | if (!options.ignoreMinifyPage) { 106 | yield* minifyPage(); 107 | } 108 | } 109 | 110 | fs.readdirSync(config.modulesDir).forEach((dir) => { 111 | if (/^labrador/.test(dir)) { 112 | let pkgFile = config.modulesDir + dir + '/package.json'; 113 | if (!utils.isFile(pkgFile)) return; 114 | let pkg = require(pkgFile); 115 | let notifier = new UpdateNotifier({ 116 | pkg, 117 | callback: function (error, update) { 118 | if (update && ['major', 'minor', 'patch'].indexOf(update.type) > -1) { 119 | notifier.update = update; 120 | notifier.notify({ 121 | defer: false, 122 | message: `发现新版本 ${dir.green}${'@'.grey}${update.latest.green},当前项目中安装的版本为${update.current.red}\n请在当前项目中运行升级命令:` + `npm install --save ${dir}`.blue 123 | }); 124 | } 125 | } 126 | }); 127 | notifier.check(); 128 | } 129 | }); 130 | } 131 | 132 | 133 | module.exports = function (options) { 134 | co(build(options)).then(() => { 135 | console.log('项目构建完成'.green); 136 | }, (error) => { 137 | console.log('项目构建失败!'.red); 138 | console.log(error.message); 139 | if (error.stack) { 140 | console.log(error.stack); 141 | } 142 | }); 143 | }; 144 | -------------------------------------------------------------------------------- /lib/config.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-10-11 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const fs = require('fs'); 10 | const path = require('path'); 11 | const utils = require('./utils'); 12 | const JSON5 = require('json5'); 13 | 14 | let program = {}; 15 | 16 | let configData = { 17 | srcDir: 'src', 18 | distDir: 'dist', 19 | modulesDir: 'node_modules', 20 | tempDir: '.build', 21 | define: {}, 22 | npmMap: {}, 23 | uglify: { 24 | mangle: [], 25 | compress: { 26 | warnings: false 27 | } 28 | }, 29 | classNames: {}, 30 | env: { 31 | development: {}, 32 | production: {} 33 | } 34 | }; 35 | 36 | let babelFileData = null; 37 | 38 | const config = { 39 | get define() { 40 | return configData.define || {}; 41 | }, 42 | 43 | get npmMap() { 44 | return configData.npmMap || {}; 45 | }, 46 | 47 | get uglify() { 48 | return configData.uglify || {}; 49 | }, 50 | 51 | get classNames() { 52 | return configData.classNames || {}; 53 | }, 54 | 55 | get workDir() { 56 | return process.cwd() + '/'; 57 | }, 58 | 59 | get srcDir() { 60 | return configData.srcDir; 61 | }, 62 | 63 | get distDir() { 64 | return configData.distDir; 65 | }, 66 | 67 | get tempDir() { 68 | return configData.tempDir; 69 | }, 70 | 71 | get modulesDir() { 72 | return configData.modulesDir; 73 | }, 74 | 75 | get babelConfig() { 76 | if (!babelFileData) { 77 | let content = '{}'; 78 | let file = config.workDir + '.babelrc'; 79 | if (utils.isFile(file)) { 80 | content = fs.readFileSync(file, 'utf8'); 81 | } 82 | babelFileData = JSON5.parse(content); 83 | } 84 | return babelFileData; 85 | }, 86 | }; 87 | 88 | module.exports = function (p) { 89 | if (p) { 90 | babelFileData = null; 91 | 92 | program = p; 93 | if (p.minify) { 94 | process.env.MINIFY = true; 95 | if (!process.env.NODE_ENV) { 96 | process.env.NODE_ENV = 'production'; 97 | } 98 | } else { 99 | if (p.catch) { 100 | process.env.CATCH = true; 101 | } 102 | if (p.test) { 103 | process.env.TEST = true; 104 | process.env.CATCH = true; 105 | } 106 | } 107 | if (!process.env.NODE_ENV) { 108 | process.env.NODE_ENV = 'development'; 109 | } 110 | 111 | if (p.workDir) { 112 | if (!utils.isDirectory(p.workDir)) { 113 | throw new Error('--work-dir=' + p.workDir + ' is not exist'); 114 | } 115 | process.chdir(p.workDir); 116 | } 117 | 118 | let file = path.join(process.cwd(), '.labrador'); 119 | 120 | if (utils.isFile(file)) { 121 | try { 122 | let data = utils.readJSON5(file); 123 | configData = Object.assign(configData, data); 124 | if (configData.env) { 125 | let envConfig = configData.env[process.env.NODE_ENV]; 126 | if (envConfig) { 127 | configData = Object.assign(configData, envConfig); 128 | } 129 | } 130 | } catch (error) { 131 | console.error('Read project config file error ' + file); 132 | throw error; 133 | } 134 | } 135 | 136 | ['srcDir', 'distDir', 'tempDir', 'modulesDir'].forEach((name) => { 137 | if (p[name]) { 138 | configData[name] = p[name]; 139 | } 140 | if (path.isAbsolute(configData[name])) { 141 | configData[name] = path.normalize(configData[name]) + '/'; 142 | } else { 143 | configData[name] = path.join(config.workDir, configData[name]) + '/'; 144 | } 145 | }); 146 | } 147 | return config; 148 | }; 149 | -------------------------------------------------------------------------------- /lib/create.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-11-21 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | require('colors'); 10 | require('shelljs/global'); 11 | const path = require('path'); 12 | const utils = require('./utils'); 13 | const download = require('download-github-repo'); 14 | const execSync = require('child_process').execSync; 15 | 16 | function create(name, options) { 17 | let rootDir = path.join(process.cwd(), name); 18 | if (utils.isDirectory(rootDir)) { 19 | console.error(`项目创建失败:"${rootDir}" 已经存在`.red); 20 | process.exit(); 21 | } 22 | 23 | console.log('下载初始项目...'.green); 24 | download('maichong/labrador-demo', rootDir, () => { 25 | console.log('下载完毕'.green); 26 | 27 | let pkgFile = path.join(rootDir, 'package.json'); 28 | let pkg = utils.readJSON(pkgFile); 29 | pkg.name = name; 30 | utils.writeJson(pkgFile, pkg); 31 | 32 | console.log('安装npm依赖'.green); 33 | execSync((which('yarn') ? 'yarn install' : 'npm install'), { 34 | cwd: rootDir, 35 | stdio: ['inherit', 'inherit', 'inherit'], 36 | env: Object.assign({ 37 | NPM_CONFIG_LOGLEVEL: 'http', 38 | NPM_CONFIG_PROGRESS: 'false', 39 | NPM_CONFIG_COLOR: 'false' 40 | }, process.env) 41 | }); 42 | console.log('构建项目...'.green); 43 | execSync('labrador build', { 44 | cwd: rootDir, 45 | stdio: ['inherit', 'inherit', 'inherit'] 46 | }); 47 | }); 48 | } 49 | 50 | module.exports = create; 51 | -------------------------------------------------------------------------------- /lib/generate-component.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-11-20 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | require('colors'); 10 | const path = require('path'); 11 | const mkdirp = require('mkdirp'); 12 | const utils = require('./utils'); 13 | const Config = require('./config'); 14 | 15 | /** 16 | * 创建组件 17 | * @param {string} name 18 | * @param {Object} options 19 | */ 20 | function generateComponent(name, options) { 21 | const config = Config(options); 22 | 23 | let className = utils.nameToKey(name); 24 | let fileBase = path.join(config.srcDir, 'components', className, className); 25 | 26 | mkdirp.sync(path.dirname(fileBase)); 27 | 28 | ['.js', '.xml', (options.scss ? '.scss' : '.less'), '.test.js'].forEach((ext) => { 29 | let target = fileBase + ext; 30 | if (utils.isFile(target)) { 31 | console.error(`组件创建失败:"${path.relative(config.workDir, target)}" 已经存在`.red); 32 | process.exit(); 33 | } 34 | utils.copyAndReplace(path.join(__dirname, '../templates/component/component' + ext), target, { 35 | COMPONENT_NAME: name, 36 | CLASS_NAME: className 37 | }); 38 | }); 39 | } 40 | 41 | module.exports = generateComponent; 42 | -------------------------------------------------------------------------------- /lib/generate-page.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-11-21 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | require('colors'); 10 | const path = require('path'); 11 | const mkdirp = require('mkdirp'); 12 | const utils = require('./utils'); 13 | const Config = require('./config'); 14 | 15 | /** 16 | * 创建页面 17 | * @param {string} name 18 | * @param {Object} options 19 | */ 20 | function generatePage(name, options) { 21 | const config = Config(options); 22 | 23 | let componentName = name.substr(name.lastIndexOf('/') + 1).replace(/^\w/, (w) => w.toUpperCase()); 24 | let className = name.replace(/\//g, '-'); 25 | let fileBase = path.join(config.srcDir, 'pages', name); 26 | 27 | mkdirp.sync(path.dirname(fileBase)); 28 | 29 | ['.js', '.xml', (options.scss ? '.scss' : '.less'), '.test.js'].forEach((ext) => { 30 | let target = fileBase + ext; 31 | if (utils.isFile(target)) { 32 | console.error(`组件创建失败:"${path.relative(config.workDir, target)}" 已经存在`.red); 33 | process.exit(); 34 | } 35 | utils.copyAndReplace(path.join(__dirname, '../templates/component/component' + ext), target, { 36 | COMPONENT_NAME: componentName, 37 | CLASS_NAME: className 38 | }); 39 | }); 40 | } 41 | 42 | module.exports = generatePage; 43 | -------------------------------------------------------------------------------- /lib/generate-redux.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-11-21 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | require('colors'); 10 | const path = require('path'); 11 | const mkdirp = require('mkdirp'); 12 | const utils = require('./utils'); 13 | const Config = require('./config'); 14 | 15 | /** 16 | * 创建Redux 17 | * @param {string} name 18 | * @param {Object} options 19 | */ 20 | function generateRedux(name, options) { 21 | const config = Config(options); 22 | 23 | let target = path.join(config.srcDir, 'redux', name + '.js'); 24 | if (utils.isFile(target)) { 25 | console.error(`组件创建失败:"${path.relative(config.workDir, target)}" 已经存在`.red); 26 | process.exit(); 27 | } 28 | 29 | mkdirp.sync(path.dirname(target)); 30 | 31 | utils.copyAndReplace(path.join(__dirname, '../templates/redux/index.js'), target, { 32 | NAME_UPPER_CASE: name.toUpperCase(), 33 | NAME_LOWER_CASE: name.toLowerCase() 34 | }); 35 | } 36 | 37 | module.exports = generateRedux; 38 | -------------------------------------------------------------------------------- /lib/generate-saga.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-11-21 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | require('colors'); 10 | const path = require('path'); 11 | const mkdirp = require('mkdirp'); 12 | const utils = require('./utils'); 13 | const Config = require('./config'); 14 | 15 | /** 16 | * 创建Saga 17 | * @param {string} name 18 | * @param {Object} options 19 | */ 20 | function generateSaga(name, options) { 21 | const config = Config(options); 22 | 23 | let target = path.join(config.srcDir, 'sagas', name + '.js'); 24 | if (utils.isFile(target)) { 25 | console.error(`组件创建失败:"${path.relative(config.workDir, target)}" 已经存在`.red); 26 | process.exit(); 27 | } 28 | 29 | mkdirp.sync(path.dirname(target)); 30 | 31 | utils.copyAndReplace(path.join(__dirname, '../templates/saga/index.js'), target, { 32 | NAME_UPPER_CASE: name.toUpperCase(), 33 | NAME_LOWER_CASE: name.toLowerCase() 34 | }); 35 | } 36 | 37 | module.exports = generateSaga; 38 | -------------------------------------------------------------------------------- /lib/labrador.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-09-25 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const co = require('co'); 10 | const program = require('commander'); 11 | const updateNotifier = require('update-notifier'); 12 | const pkg = require('../package.json'); 13 | 14 | const notifier = updateNotifier({ 15 | pkg, 16 | callback: function (error, update) { 17 | if (update && ['major', 'minor', 'patch'].indexOf(update.type) > -1) { 18 | notifier.update = update; 19 | notifier.notify({ 20 | defer: false 21 | }); 22 | } 23 | } 24 | }); 25 | 26 | 27 | program 28 | .version(pkg.version); 29 | 30 | program 31 | .command('create ') 32 | .alias('c') 33 | .description('创建新项目') 34 | .action((name, options) => { 35 | require('./create')(name, options); 36 | }); 37 | 38 | program 39 | .command('generate ') 40 | .alias('g') 41 | .description('创建新组件、页面、Redux、Saga等等') 42 | .option('--work-dir [dir]', '工作目录,默认为当前目录') 43 | .option('--config [file]', '配置文件,默认为.labrador') 44 | .option('--src-dir [dir]', '源码目录,默认为工作目录下的src文件夹') 45 | .option('--scss', '使用scss,默认为less') 46 | .action((type, name, options) => { 47 | switch (type) { 48 | case 'page': 49 | require('./generate-page')(name, options); 50 | return; 51 | case 'component': 52 | require('./generate-component')(name, options); 53 | return; 54 | case 'redux': 55 | require('./generate-redux')(name, options); 56 | return; 57 | case 'saga': 58 | require('./generate-saga')(name, options); 59 | return; 60 | } 61 | console.log('Unknown type to generate'); 62 | }); 63 | 64 | program 65 | .command('build') 66 | .alias('b') 67 | .description('编译当前项目') 68 | .option('-c, --catch', '在载入时自动catch所有JS脚本的错误') 69 | .option('-t, --test', '运行测试脚本') 70 | .option('-m, --minify', '压缩代码') 71 | .option('-f, --force', '强制构建,不使用缓存') 72 | .option('--work-dir [dir]', '工作目录,默认为当前目录') 73 | .option('--config [file]', '配置文件,默认为.labrador') 74 | .option('--src-dir [dir]', '源码目录,默认为工作目录下的src文件夹') 75 | .option('--dist-dir [dir]', '目标目录,默认为工作目录下的dist文件夹') 76 | .option('--modules-dir [dir]', 'NPM模块目录,默认为工作目录下的node_modules文件夹') 77 | .option('--temp-dir [dir]', '临时目录,默认为工作目录下的.build文件夹') 78 | .option('--ignore-minify-js', 'minify模式下,不压缩JS代码') 79 | .option('--ignore-minify-page', 'minify模式下,不强力压缩WXSS和WXML代码') 80 | .action((options) => { 81 | if (!options.minify) { 82 | if (options.ignoreMinifyJs) { 83 | console.error('--ignore-minify-js 必须配合 --minify 选项一起使用'); 84 | process.exit(); 85 | } 86 | if (options.ignoreMinifyPage) { 87 | console.error('--ignore-minify-page 必须配合 --minify 选项一起使用'); 88 | process.exit(); 89 | } 90 | } 91 | require('./build')(options); 92 | }); 93 | 94 | program 95 | .command('watch') 96 | .alias('w') 97 | .description('编译当前项目并检测文件改动') 98 | .option('-c, --catch', '在载入时自动catch所有JS脚本的错误') 99 | .option('-t, --test', '运行测试脚本') 100 | .option('--work-dir [dir]', '工作目录,默认为当前目录') 101 | .option('--config [file]', '配置文件,默认为.labrador') 102 | .option('--src-dir [dir]', '源码目录,默认为工作目录下的src文件夹') 103 | .option('--dist-dir [dir]', '目标目录,默认为工作目录下的dist文件夹') 104 | .option('--modules-dir [dir]', 'NPM模块目录,默认为工作目录下的node_modules文件夹') 105 | .option('--temp-dir [dir]', '临时目录,默认为工作目录下的.build文件夹') 106 | .action((options) => { 107 | require('./watch')(options); 108 | }); 109 | 110 | program 111 | .command('minify-page') 112 | .description('minify page') 113 | .option('--work-dir [dir]', '工作目录,默认为当前目录') 114 | .option('--config [file]', '配置文件,默认为.labrador') 115 | .option('--src-dir [dir]', '源码目录,默认为工作目录下的src文件夹') 116 | .option('--dist-dir [dir]', '目标目录,默认为工作目录下的dist文件夹') 117 | .option('--modules-dir [dir]', 'NPM模块目录,默认为工作目录下的node_modules文件夹') 118 | .option('--temp-dir [dir]', '临时目录,默认为工作目录下的.build文件夹') 119 | .action((options) => { 120 | require('./utils'); 121 | require('./config')(options); 122 | let minifyPage = require('./minify-page'); 123 | co(minifyPage).then(() => console.log('done'), (error) => console.error(error)); 124 | }); 125 | 126 | program 127 | .command('minify-js') 128 | .description('minify js') 129 | .option('--work-dir [dir]', '工作目录,默认为当前目录') 130 | .option('--config [file]', '配置文件,默认为.labrador') 131 | .option('--src-dir [dir]', '源码目录,默认为工作目录下的src文件夹') 132 | .option('--dist-dir [dir]', '目标目录,默认为工作目录下的dist文件夹') 133 | .option('--modules-dir [dir]', 'NPM模块目录,默认为工作目录下的node_modules文件夹') 134 | .option('--temp-dir [dir]', '临时目录,默认为工作目录下的.build文件夹') 135 | .action((options) => { 136 | require('./utils'); 137 | require('./config')(options); 138 | let minifyJs = require('./minify-js'); 139 | co(minifyJs).then(() => console.log('done'), (error) => console.error(error)); 140 | }); 141 | 142 | program.parse(process.argv); 143 | 144 | if (!program.args.length) program.help(); 145 | -------------------------------------------------------------------------------- /lib/minify-js.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-11-17 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | require('colors'); 10 | const fs = require('fs'); 11 | const path = require('path'); 12 | const utils = require('./utils'); 13 | const UglifyJS = require('uglify-js'); 14 | const slash = require('slash'); 15 | const config = require('./config')(); 16 | 17 | require('shelljs/global'); 18 | 19 | module.exports = function* minifyJs() { 20 | console.log('minify js...'.green); 21 | 22 | const distPagesDir = config.distDir + 'pages/'; 23 | 24 | let pages = [config.distDir + 'app.js'].concat(utils.getJsFiles(distPagesDir)).map(file => path.normalize(file)); 25 | 26 | let fileMap = {}; 27 | let codeArray = []; 28 | 29 | function compile(file) { 30 | let code = fs.readFileSync(file, 'utf8'); 31 | 32 | code = code.replace(/([\s;]?)require\(['"]([\w\_\-\.\/\@]+)['"]\)/g, function (matchs, char, ref) { 33 | let refFile = path.normalize(path.join(path.dirname(file), ref)); 34 | if (fileMap[refFile] === undefined) { 35 | fileMap[refFile] = compile(refFile); 36 | } 37 | return char + `__labrador_require__(${fileMap[refFile]})`; 38 | }); 39 | 40 | code = `function (module, exports, __labrador_require__) {\n//START ${path.relative(config.distDir, file)}\n${code}\n//END\n}\n`; 41 | 42 | codeArray.push(code); 43 | fileMap[file] = codeArray.length - 1; 44 | //console.log(codeArray.length - 1, file); 45 | return fileMap[file]; 46 | } 47 | 48 | pages.forEach((file) => { 49 | compile(file); 50 | }); 51 | 52 | let code = `module.exports=(function(modules) { 53 | var installedModules = {}; 54 | 55 | function __labrador_require__(moduleId) { 56 | if (installedModules[moduleId]) 57 | return installedModules[moduleId].exports; 58 | var module = installedModules[moduleId] = { 59 | exports: {}, 60 | id: moduleId, 61 | loaded: false 62 | }; 63 | modules[moduleId].call(module.exports, module, module.exports, __labrador_require__); 64 | module.loaded = true; 65 | return module.exports; 66 | } 67 | return __labrador_require__; 68 | })([` + codeArray.join(',') + ']);'; 69 | 70 | code = code.replace(/function _interopRequireDefault\(obj\) \{ return obj && obj\.__esModule \? obj : \{ default: obj \}; }/g, ''); 71 | code = 'function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n' + code; 72 | 73 | code = code.replace(/__esModule/g, '_E'); 74 | 75 | code = `'use strict';(function(){ 76 | var window; 77 | var global=window={ 78 | Array: Array, 79 | Date: Date, 80 | Error: Error, 81 | Function: Function, 82 | Math: Math, 83 | Object: Object, 84 | RegExp: RegExp, 85 | String: String, 86 | TypeError: TypeError, 87 | setTimeout: setTimeout, 88 | clearTimeout: clearTimeout, 89 | setInterval: setInterval, 90 | clearInterval: clearInterval 91 | }; 92 | ${code} 93 | })()`; 94 | 95 | fs.writeFileSync(config.distDir + 'm.js', code); 96 | try { 97 | code = UglifyJS.minify(code, Object.assign({}, config.uglify, { fromString: true })).code; 98 | } catch (error) { 99 | console.log(error); 100 | throw error; 101 | } 102 | 103 | utils.getJsFiles(config.distDir).forEach((f) => utils.removeFile(f)); 104 | 105 | pages.forEach((f) => { 106 | let r = path.relative(path.dirname(f), config.distDir + 'm.js'); 107 | let fragment = `require('${slash(r)}')(${fileMap[f]})`; 108 | console.log('update'.green + ' ' + path.relative(config.workDir, f).blue); 109 | fs.writeFileSync(f, fragment); 110 | }); 111 | 112 | console.log('create'.green, path.relative(config.workDir, config.distDir + 'm.js').blue); 113 | fs.writeFileSync(config.distDir + 'm.js', code); 114 | }; 115 | -------------------------------------------------------------------------------- /lib/minify-page.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-10-19 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | require('colors'); 10 | const fs = require('fs'); 11 | const path = require('path'); 12 | const radix64 = require('radix64').radix64; 13 | const CleanCSS = require('clean-css'); 14 | const utils = require('./utils'); 15 | const config = require('./config')(); 16 | 17 | let _id = 0; 18 | function createId() { 19 | _id++; 20 | let str = radix64(_id); 21 | if (/^[\d_-]/.test(str) || /[-_]$/.test(str)) { 22 | return createId(); 23 | } 24 | return str; 25 | } 26 | 27 | /** 28 | * 压缩app.less 29 | * @param appNameMap 30 | * @param appContentMap 31 | * @returns {Function} 32 | */ 33 | function minifyApp(appNameMap, appContentMap) { 34 | let file = config.distDir + 'app.wxss'; 35 | if (!utils.isFile(file)) { 36 | return; 37 | } 38 | let minified = new CleanCSS({ keepBreaks: true }).minify(fs.readFileSync(file, 'utf8')).styles; 39 | minified = minified.replace(/::?(after|before|first\-child|last\-child)/g, ':$1'); 40 | let finalCssContent = ''; 41 | minified.split('\n').forEach((line) => { 42 | let index = line.indexOf('{'); 43 | let selectors = line.substr(0, index).split(','); 44 | let content = line.substr(index); 45 | 46 | selectors.forEach((selector) => { 47 | if (selector[0] !== '.') { 48 | finalCssContent += selector + content + '\n'; 49 | return; 50 | } 51 | let className = selector.substr(1); 52 | if (!appNameMap[className]) { 53 | appNameMap[className] = { 54 | id: '', 55 | contents: [] 56 | }; 57 | } 58 | appNameMap[className].contents.push(content); 59 | if (!appContentMap[content]) { 60 | appContentMap[content] = []; 61 | } 62 | appContentMap[content].push(className); 63 | }); 64 | }); 65 | 66 | return function () { 67 | //console.log(appNameMap, appContentMap); 68 | console.log('minify'.green, path.normalize('dist/app.wxss').blue); 69 | for (let key in appNameMap) { 70 | let matchs = key.match(/(.*):+(after|before|first\-child|last\-child)$/); 71 | if (matchs && appNameMap[matchs[1]] && appNameMap[matchs[1]].id) { 72 | appNameMap[key].id = appNameMap[matchs[1]].id + ':' + matchs[2]; 73 | } 74 | } 75 | for (let c in appContentMap) { 76 | let keys = []; 77 | appContentMap[c].forEach((key) => { 78 | if (appNameMap[key].id) { 79 | keys.push('.' + appNameMap[key].id); 80 | console.log(('\t.' + key).blue, '->', ('.' + appNameMap[key].id).cyan); 81 | } else if (config.classNames && config.classNames[key]) { 82 | keys.push('.' + key); 83 | console.log(('\t.' + key).blue, '->', ('.' + key).cyan); 84 | } 85 | }); 86 | if (keys.length) { 87 | finalCssContent += keys.join(',') + c + '\n'; 88 | } 89 | } 90 | finalCssContent = new CleanCSS({ keepBreaks: true }).minify(finalCssContent).styles; 91 | fs.writeFileSync(file, finalCssContent); 92 | }; 93 | } 94 | 95 | function findPages(dir) { 96 | let files = fs.readdirSync(dir); 97 | let res = []; 98 | for (let file of files) { 99 | let filePath = path.join(dir, file); 100 | if (utils.isDirectory(filePath)) { 101 | res = res.concat(findPages(filePath)); 102 | continue; 103 | } 104 | let info = path.parse(filePath); 105 | if (info.ext === '.wxml') { 106 | res.push({ 107 | wxml: filePath, 108 | wxss: path.join(info.dir, info.name + '.wxss') 109 | }); 110 | } 111 | } 112 | return res; 113 | } 114 | 115 | /** 116 | * 压缩页面 117 | * @param {Object} page 118 | * @param {Object} appNameMap 119 | * @param {Object} appContentMap 120 | */ 121 | function minify(page, appNameMap, appContentMap) { 122 | let cssContent = ''; 123 | let xmlContent = fs.readFileSync(page.wxml, 'utf8'); 124 | if (utils.isFile(page.wxss)) { 125 | cssContent = fs.readFileSync(page.wxss, 'utf8'); 126 | } 127 | if (cssContent) { 128 | cssContent = new CleanCSS({ keepBreaks: true }).minify(cssContent).styles; 129 | cssContent = cssContent.replace(/::?(after|before|first\-child|last\-child)/g, ':$1'); 130 | } 131 | // LESS中存在的className列表 132 | let styleClassNames = {}; 133 | // LESS中 content -> className 映射 134 | let pageContentMap = {}; 135 | let finalCssContent = ''; 136 | cssContent.split('\n').forEach((line) => { 137 | let index = line.indexOf('{'); 138 | let selectors = line.substr(0, index).split(','); 139 | let content = line.substr(index); 140 | 141 | selectors.forEach((selector) => { 142 | if (selector[0] !== '.') { 143 | finalCssContent += selector + content + '\n'; 144 | return; 145 | } 146 | let className = selector.substr(1); 147 | if (!pageContentMap[content]) { 148 | pageContentMap[content] = []; 149 | } 150 | styleClassNames[className] = true; 151 | pageContentMap[content].push(className); 152 | //styles.push({ selector, content, className }); 153 | }); 154 | }); 155 | // console.log('styleClassNames', styleClassNames); 156 | // console.log('pageContentMap', pageContentMap); 157 | // console.log('cssContent', cssContent); 158 | let xmlClassNames = {}; 159 | let clsNameMap = {}; 160 | xmlContent = xmlContent.replace(/[\r\n]\s+]+-->/g, ''); 161 | xmlContent = xmlContent.replace(/ class="([^"]+)"/g, (matchs, names) => { 162 | names = names.replace(/{{([^}]+)}}/g, function (m, words) { 163 | return '{{' + (new Buffer(words, 'utf8')).toString('hex') + '}}'; 164 | }); 165 | names = names.split(' ').filter((name) => { 166 | if (!name) return false; 167 | if (name.indexOf('{') > -1) return true; 168 | if (config.classNames && config.classNames[name]) return true; 169 | if (appNameMap[name]) { 170 | appNameMap[name].used = true; 171 | if (!appNameMap[name].id) { 172 | appNameMap[name].id = createId(); 173 | } 174 | } 175 | if ( 176 | styleClassNames[name] 177 | || styleClassNames[name + ':before'] 178 | || styleClassNames[name + ':after'] 179 | || styleClassNames[name + ':first-child'] 180 | || styleClassNames[name + ':last-child'] 181 | || appNameMap[name] 182 | || appNameMap[name + ':before'] 183 | || appNameMap[name + ':after'] 184 | || appNameMap[name + ':first-child'] 185 | || appNameMap[name + ':last-child'] 186 | ) { 187 | xmlClassNames[name] = true; 188 | return true; 189 | } 190 | return false; 191 | }).map(function (name) { 192 | if (name.indexOf('{') > -1) return name; 193 | if (config.classNames && config.classNames[name]) { 194 | clsNameMap[name] = name; 195 | if (appNameMap[name]) { 196 | appNameMap[name].id = name; 197 | } else if (appNameMap[name + ':before'] || appNameMap[name + ':after'] || appNameMap[name + ':first-child'] || appNameMap[name + ':lasr-child']) { 198 | // 如果app.less中存在 .name:before 或 .name:after 但是不存在 .name,自动添加 .name 199 | appNameMap[name] = { id: name, contents: ['{}'] }; 200 | } 201 | return name; 202 | } 203 | if (clsNameMap[name]) { 204 | return clsNameMap[name]; 205 | } 206 | let id; 207 | if (appNameMap[name]) { 208 | id = appNameMap[name].id; 209 | } else { 210 | id = createId(); 211 | // 如果app.less中存在 .name:before 或 .name:after 但是不存在 .name,自动添加 .name 212 | if (appNameMap[name + ':before'] || appNameMap[name + ':after'] || appNameMap[name + ':first-child'] || appNameMap[name + ':last-child']) { 213 | appNameMap[name] = { id, contents: ['{}'] }; 214 | } 215 | } 216 | clsNameMap[name] = id; 217 | return id; 218 | }); 219 | 220 | if (names.length) { 221 | // 返回替换后的class name输出到XML中 222 | return ' class="' + names.join(' ').replace(/{{([\da-f]+)}}/g, 223 | (m, hex) => { 224 | return '{{' + new Buffer(hex, 'hex').toString('utf8') + '}}'; 225 | }) + '"'; 226 | } 227 | return ''; 228 | }); 229 | 230 | if (cssContent) { 231 | console.log('minify'.green, path.relative(process.cwd(), page.wxss).blue); 232 | for (let c in pageContentMap) { 233 | // c 为样式定义content '{foo:bar}' 234 | let keys = []; 235 | pageContentMap[c].forEach((key) => { 236 | // key 为className 237 | 238 | if (appContentMap[c] && appContentMap[c].indexOf(key) > -1) { 239 | //如果app.wxss中已经存在完全一模一样的记录,则忽略本条 240 | return; 241 | } 242 | 243 | let matchs = key.match(/(.*):(after|before|first\-child|last\-child)$/); 244 | if (matchs && clsNameMap[matchs[1]]) { 245 | //如果是伪类 246 | keys.push('.' + clsNameMap[matchs[1]] + ':' + matchs[2]); 247 | return; 248 | } 249 | 250 | if (clsNameMap[key]) { 251 | // 如果页面XML文件中有对应className引用 252 | 253 | keys.push('.' + clsNameMap[key]); 254 | console.log(('\t.' + key).blue, '->', ('.' + clsNameMap[key]).cyan); 255 | } else if (config.classNames[key]) { 256 | // 如果配置文件中要求保留此类名 257 | 258 | keys.push('.' + key); 259 | console.log(('\t.' + key).blue, '->', ('.' + key).cyan); 260 | } 261 | }); 262 | if (keys.length) { 263 | finalCssContent += keys.join(',') + c + '\n'; 264 | } 265 | } 266 | } 267 | 268 | fs.writeFileSync(page.wxml, xmlContent); 269 | if (finalCssContent) { 270 | finalCssContent = new CleanCSS({ keepBreaks: true }).minify(finalCssContent).styles; 271 | fs.writeFileSync(page.wxss, finalCssContent); 272 | } 273 | } 274 | 275 | module.exports = function* minifyPage() { 276 | console.log('minify page...'.green); 277 | let appNameMap = {}; 278 | let appContentMap = {}; 279 | let output = minifyApp(appNameMap, appContentMap); 280 | let pages = findPages(config.distDir + 'pages'); 281 | for (let page of pages) { 282 | console.log('minify'.green, path.relative(config.workDir, page.wxml).blue); 283 | minify(page, appNameMap, appContentMap); 284 | } 285 | output(); 286 | }; 287 | -------------------------------------------------------------------------------- /lib/types.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-11-18 4 | * @author Liang 5 | */ 6 | 7 | declare class FileInfo { 8 | file : string; 9 | root : string; 10 | dir :string; 11 | name : string; 12 | base : string; 13 | ext :string; 14 | relative :string; 15 | fromSrc : string; 16 | fromDist : string; 17 | } 18 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-01-19 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const fs = require('fs'); 10 | const path = require('path'); 11 | const config = require('./config')(); 12 | const version = require('../package.json').version; 13 | const minimatch = require('minimatch'); 14 | const JSON5 = require('json5'); 15 | 16 | /** 17 | * 判断指定路径是否是文件 18 | * @param path 19 | * @returns {boolean} 20 | */ 21 | exports.isFile = function isFile(path) { 22 | try { 23 | return fs.statSync(path).isFile(); 24 | } catch (e) { 25 | return false; 26 | } 27 | }; 28 | 29 | /** 30 | * 判断指定路径是否是文件夹 31 | * @param path 32 | * @returns {boolean} 33 | */ 34 | exports.isDirectory = function isDirectory(path) { 35 | try { 36 | return fs.statSync(path).isDirectory(); 37 | } catch (e) { 38 | return false; 39 | } 40 | }; 41 | 42 | /** 43 | * 获取修改时间 44 | * @param path 45 | * @returns {boolean} 46 | */ 47 | exports.getModifiedTime = function (path) { 48 | try { 49 | return fs.statSync(path).mtime; 50 | } catch (e) { 51 | return false; 52 | } 53 | }; 54 | 55 | exports.readJSON = function readJSON(file) { 56 | let data = fs.readFileSync(file, 'utf8'); 57 | return JSON.parse(data); 58 | }; 59 | 60 | /** 61 | * 读取JSON5文件 62 | * @param file 63 | */ 64 | exports.readJSON5 = function readJSON5(file) { 65 | let data = fs.readFileSync(file, 'utf8'); 66 | return JSON5.parse(data); 67 | }; 68 | 69 | exports.writeJson = function writeJson(file, data) { 70 | return fs.writeFileSync(file, JSON.stringify(data, null, 2)); 71 | }; 72 | 73 | exports.copyAndReplace = function copyAndReplace(src, target, replaces) { 74 | let data = fs.readFileSync(src, 'utf8'); 75 | for (let key in replaces) { 76 | data = data.replace(new RegExp(exports.escapeRegExp(key), 'g'), replaces[key]); 77 | } 78 | fs.writeFileSync(target, data); 79 | }; 80 | 81 | /** 82 | * 递归获取JS文件列表 83 | * @param dir 84 | * @returns {*|Array} 85 | */ 86 | exports.getJsFiles = function getJsFiles(dir) { 87 | let res = []; 88 | let files = fs.readdirSync(dir); 89 | for (let file of files) { 90 | if (exports.isDirectory(dir + '/' + file)) { 91 | res = res.concat(exports.getJsFiles(dir + '/' + file)); 92 | } else if (/\.js$/.test(file)) { 93 | res.push(dir + '/' + file); 94 | } 95 | } 96 | return res; 97 | }; 98 | 99 | /** 100 | * 获取文件信息 101 | * @param file 102 | * @returns {FileInfo} 103 | */ 104 | exports.getInfo = function (file) { 105 | let info = path.parse(file); 106 | return Object.assign(info, { 107 | file: path.normalize(file), 108 | relative: path.relative(config.workDir, file), 109 | fromSrc: path.relative(config.srcDir, file), 110 | fromDist: path.relative(config.distDir, file) 111 | }); 112 | }; 113 | 114 | /** 115 | * 获取文件信息列表 116 | * @param dir 117 | * @returns {Array} 118 | */ 119 | exports.getFileInfos = function getFileInfos(dir) { 120 | let res = []; 121 | let list = fs.readdirSync(dir); 122 | for (let name of list) { 123 | if (name[0] === '.') continue; 124 | let file = path.join(dir, name); 125 | if (exports.isDirectory(file)) { 126 | res = res.concat(exports.getFileInfos(file)); 127 | } else { 128 | res.push(exports.getInfo(file)); 129 | } 130 | } 131 | return res; 132 | }; 133 | 134 | /** 135 | * 递归向上删除空文件夹 136 | * @param dir 137 | */ 138 | exports.removeEmptyDir = function removeEmptyDir(dir) { 139 | if (!fs.readdirSync(dir).length) { 140 | console.log('remove'.red, path.relative(config.workDir, dir)); 141 | rm('-R', dir); 142 | removeEmptyDir(path.dirname(dir)); 143 | } 144 | }; 145 | 146 | /** 147 | * 删除文件 148 | * @param file 149 | */ 150 | exports.removeFile = function removeFile(file) { 151 | rm(file); 152 | exports.removeEmptyDir(path.dirname(file)); 153 | }; 154 | 155 | /** 156 | * 获取目标文件后缀 157 | * @param ext 158 | */ 159 | exports.getDistFileExt = function getDistFileExt(ext) { 160 | switch (ext) { 161 | case '.less': 162 | case '.sass': 163 | case '.scss': 164 | return '.wxss'; 165 | case '.xml': 166 | return '.wxml'; 167 | } 168 | return ext; 169 | }; 170 | 171 | /** 172 | * 判断某个文件是否是页面中的文件 173 | * @param file 174 | * @returns {boolean} 175 | */ 176 | exports.inPages = function inPages(file) { 177 | return path.relative(config.srcDir + 'pages/', file)[0] !== '.'; 178 | }; 179 | 180 | /** 181 | * 判断某个文件是否是templates中的文件 182 | * @param file 183 | * @returns {boolean} 184 | */ 185 | exports.inTemplates = function inTemplates(file) { 186 | return path.relative(config.srcDir + 'templates/', file)[0] !== '.'; 187 | }; 188 | 189 | 190 | /** 191 | * 判断某个文件是否是node_modules中的文件 192 | * @param file 193 | * @returns {boolean} 194 | */ 195 | exports.inNpm = function inNpm(file) { 196 | return path.relative(config.modulesDir, file)[0] !== '.'; 197 | }; 198 | 199 | /** 200 | * 判断文件是否发生了变化 201 | * @param {string} from 202 | * @param {string} to 203 | * @param {Object} metadata 204 | * @returns {boolean} 205 | */ 206 | exports.isChanged = function isChanged(from, to, metadata) { 207 | if (process.env.MINIFY) { 208 | metadata[from] = {}; 209 | metadata[to] = {}; 210 | return true; 211 | } 212 | 213 | let fromTime = exports.getModifiedTime(from).toString(); 214 | 215 | if (!metadata[from] || !metadata[to] || metadata[from].mtime !== fromTime || !metadata[from].depends || !exports.isFile(to)) { 216 | metadata[from] = { 217 | mtime: fromTime 218 | }; 219 | metadata[to] = {}; 220 | return true; 221 | } 222 | 223 | if (metadata[from].v !== version || metadata[to].v !== version) { 224 | metadata[from] = { 225 | mtime: fromTime 226 | }; 227 | metadata[to] = {}; 228 | return true; 229 | } 230 | 231 | let toTime = exports.getModifiedTime(to).toString(); 232 | if (metadata[to].mtime !== toTime) { 233 | metadata[from] = { 234 | mtime: fromTime 235 | }; 236 | metadata[to] = {}; 237 | return true; 238 | } 239 | 240 | return false; 241 | }; 242 | 243 | /** 244 | * 生成安全的正则字符串 245 | * @param {string} str 246 | * @returns {string} 247 | */ 248 | exports.escapeRegExp = function escapeRegExp(str) { 249 | if (str && str.toString) str = str.toString(); 250 | if (typeof str !== 'string' || !str.length) return ''; 251 | return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); 252 | }; 253 | 254 | /** 255 | * 将驼峰样式字符串转为小写字符串样式 256 | * @param {string} name 257 | * @returns {string} 258 | */ 259 | exports.nameToKey = function nameToKey(name) { 260 | return name.replace(/([a-z])([A-Z])/g, (a, b, c) => (b + '-' + c.toLowerCase())).toLowerCase(); 261 | }; 262 | 263 | function match(file, role) { 264 | if (typeof role === 'string') { 265 | return minimatch(file, role); 266 | } else if (Array.isArray(role)) { 267 | for (let r of role) { 268 | if (match(file, r)) { 269 | return true; 270 | } 271 | } 272 | } 273 | return false; 274 | } 275 | 276 | /** 277 | * @param {string} file 278 | * @returns {boolean} 279 | */ 280 | exports.shouldBabelIgnore = function shouldBabelIgnore(file) { 281 | let babelConfig = config.babelConfig; 282 | if (babelConfig.only) { 283 | return !match(file, babelConfig.only); 284 | } 285 | if (!babelConfig.ignore) { 286 | return false; 287 | } 288 | 289 | return match(file, babelConfig.ignore); 290 | }; 291 | -------------------------------------------------------------------------------- /lib/watch.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @copyright Maichong Software Ltd. 2016 http://maichong.it 3 | * @date 2016-09-25 4 | * @author Liang 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chokidar = require('chokidar'); 10 | const co = require('co'); 11 | const path = require('path'); 12 | const mkdirp = require('mkdirp'); 13 | const updateNotifier = require('update-notifier'); 14 | const utils = require('./utils'); 15 | const buildJS = require('./build-js'); 16 | const buildLess = require('./build-less'); 17 | const buildSass = require('./build-sass'); 18 | const buildXML = require('./build-xml'); 19 | require('shelljs/global'); 20 | 21 | function* watch(options) { 22 | const config = require('./config')(options); 23 | if (!utils.isDirectory(config.srcDir)) { 24 | throw new Error('src 目录不存在'); 25 | } 26 | if (!utils.isFile(config.srcDir + 'app.json')) { 27 | throw new Error('src/app.json 不存在'); 28 | } 29 | if (!utils.isFile(config.srcDir + 'app.js')) { 30 | throw new Error('src/app.js 不存在'); 31 | } 32 | 33 | let pkg = require(path.join(config.modulesDir, 'labrador/package.json')); 34 | const notifier = updateNotifier({ 35 | pkg, 36 | callback: function (error, update) { 37 | if (update && ['major', 'minor', 'patch'].indexOf(update.type) > -1) { 38 | notifier.update = update; 39 | notifier.notify({ 40 | message: `Labardor update available ${update.current} → ${update.latest.green}\nRun ` + 'npm install --save labrador'.cyan + ' to update your project', 41 | defer: false 42 | }); 43 | } 44 | } 45 | }); 46 | 47 | let targets = {}; 48 | let refs = {}; 49 | 50 | function* buildFile(source) { 51 | let from = utils.getInfo(source); 52 | let to = path.join(config.distDir, path.relative(config.srcDir, from.dir), from.name + utils.getDistFileExt(from.ext)); 53 | to = utils.getInfo(to); 54 | switch (from.ext) { 55 | case '.js': 56 | delete targets[to.file]; 57 | yield* buildJS(from, to, targets, {}); 58 | break; 59 | case '.less': 60 | if ((from.fromSrc === 'app.less') || utils.inPages(from.file)) { 61 | let depends = yield* buildLess(from, to); 62 | depends.forEach((d) => { 63 | d = path.normalize(d); 64 | if (!refs[d]) { 65 | refs[d] = {}; 66 | } 67 | refs[d][from.file] = true; 68 | }); 69 | } else if (refs[from.file]) { 70 | console.log('changed'.green, from.relative.blue); 71 | let files = Object.keys(refs[from.file]); 72 | for (let s of files) { 73 | yield* buildFile(s); 74 | } 75 | } else { 76 | console.log('ignore'.yellow, from.relative.gray); 77 | } 78 | break; 79 | case '.sass': 80 | case '.scss': 81 | if ((from.fromSrc === 'app.sass') || (from.fromSrc === 'app.scss') || utils.inPages(from.file)) { 82 | let depends = yield* buildSass(from, to); 83 | depends.forEach((d) => { 84 | d = path.normalize(d); 85 | if (!refs[d]) { 86 | refs[d] = {}; 87 | } 88 | refs[d][from.file] = true; 89 | }); 90 | } else if (refs[from.file]) { 91 | console.log('changed'.green, from.relative.blue); 92 | let files = Object.keys(refs[from.file]); 93 | for (let s of files) { 94 | yield* buildFile(s); 95 | } 96 | } else { 97 | console.log('ignore'.yellow, from.relative.gray); 98 | } 99 | break; 100 | case '.xml': 101 | if (utils.inPages(from.file) || utils.inTemplates(from.file)) { 102 | let depends = yield* buildXML(from, to); 103 | depends.forEach((d) => { 104 | d = path.normalize(d); 105 | if (!refs[d]) { 106 | refs[d] = {}; 107 | } 108 | refs[d][from.file] = true; 109 | }); 110 | } else if (refs[from.file]) { 111 | console.log('changed'.green, from.relative.blue); 112 | let files = Object.keys(refs[from.file]); 113 | for (let s of files) { 114 | yield* buildFile(s); 115 | } 116 | } else { 117 | console.log('ignore'.yellow, from.relative.gray); 118 | } 119 | break; 120 | default: 121 | console.log('copy'.green, from.relative.blue, '->', to.relative.cyan); 122 | mkdirp.sync(to.dir); 123 | cp(from.file, to.file); 124 | } 125 | } 126 | 127 | chokidar.watch(config.srcDir, { ignored: /^\./ }).on('all', (event, source) => { 128 | if (event === 'addDir') return; 129 | co(buildFile(source)).catch((error) => { 130 | console.log(error.codeFrame || error.stack || ''); 131 | }); 132 | }); 133 | } 134 | 135 | module.exports = function (options) { 136 | co(watch(options)).then(() => { 137 | }, (error) => { 138 | console.error(error.stack); 139 | process.exit(); 140 | }); 141 | }; 142 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "labrador-cli", 3 | "version": "0.6.11", 4 | "description": "拉布拉多命令工具,微信小程序组件化开发框架", 5 | "keywords": [ 6 | "labrador", 7 | "wxa", 8 | "wxapp", 9 | "weixin", 10 | "wechat" 11 | ], 12 | "main": "index.js", 13 | "bin": { 14 | "labrador": "bin/labrador" 15 | }, 16 | "scripts": {}, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/maichong/labrador-cli.git" 20 | }, 21 | "author": { 22 | "email": "liang@maichong.it", 23 | "name": "liang", 24 | "url": "https://github.com/liangxingchen" 25 | }, 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/maichong/labrador-cli/issues" 29 | }, 30 | "homepage": "https://github.com/maichong/labrador-cli#readme", 31 | "dependencies": { 32 | "chokidar": "^1.6.0", 33 | "clean-css": "^3.4.20", 34 | "co": "^4.6.0", 35 | "colors": "^1.1.2", 36 | "commander": "^2.9.0", 37 | "download-github-repo": "^0.1.3", 38 | "es6-promisify": "^5.0.0", 39 | "json5": "^0.5.0", 40 | "less": "^2.7.1", 41 | "minimatch": "^3.0.3", 42 | "mkdirp": "^0.5.1", 43 | "radix64": "^1.1.0", 44 | "shelljs": "^0.7.4", 45 | "slash": "^1.0.0", 46 | "uglify-js": "^2.7.3", 47 | "update-notifier": "^1.0.2", 48 | "xmldom": "^0.1.22" 49 | }, 50 | "devDependencies": { 51 | "babel-eslint": "^7.1.1", 52 | "eslint": "^3.10.2", 53 | "eslint-config-airbnb": "^13.0.0", 54 | "eslint-plugin-import": "^2.2.0", 55 | "eslint-plugin-jsx-a11y": "^2.2.3", 56 | "eslint-plugin-react": "^6.7.1" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /templates/component/component.js: -------------------------------------------------------------------------------- 1 | import { Component, PropTypes } from 'labrador-immutable'; 2 | import immutable from 'seamless-immutable'; 3 | //import { connect } from 'labrador-redux'; 4 | 5 | const { any } = PropTypes; 6 | 7 | class COMPONENT_NAME extends Component { 8 | static propTypes = { 9 | foo: any 10 | }; 11 | 12 | static defaultProps = { 13 | foo: 'bar' 14 | }; 15 | 16 | constructor(props) { 17 | super(props); 18 | this.state = immutable({}); 19 | } 20 | 21 | children() { 22 | return {}; 23 | } 24 | 25 | // onLoad() { 26 | // } 27 | 28 | // onReady() { 29 | // } 30 | 31 | // onUpdate() { 32 | // } 33 | 34 | // onShow() { 35 | // } 36 | 37 | // onHide() { 38 | // } 39 | 40 | // onUnload() { 41 | // } 42 | 43 | } 44 | 45 | export default COMPONENT_NAME; 46 | 47 | // export default connect( 48 | // (state)=>({}) 49 | // )(COMPONENT_NAME); 50 | -------------------------------------------------------------------------------- /templates/component/component.less: -------------------------------------------------------------------------------- 1 | @import 'al-ui'; 2 | 3 | .CLASS_NAME { 4 | background: @color-page; 5 | font-size: @font-size-medium; 6 | } 7 | -------------------------------------------------------------------------------- /templates/component/component.scss: -------------------------------------------------------------------------------- 1 | @import 'al-ui'; 2 | 3 | .CLASS_NAME { 4 | background: $color-page; 5 | font-size: $font-size-medium; 6 | } 7 | -------------------------------------------------------------------------------- /templates/component/component.test.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert'; 2 | 3 | export async function onLoad(c, next) { 4 | next(); 5 | } 6 | -------------------------------------------------------------------------------- /templates/component/component.xml: -------------------------------------------------------------------------------- 1 | COMPONENT_NAME 2 | -------------------------------------------------------------------------------- /templates/redux/index.js: -------------------------------------------------------------------------------- 1 | import { createAction, handleActions } from 'redux-actions'; 2 | import immutable from 'seamless-immutable'; 3 | 4 | /** 5 | * Action Types 6 | */ 7 | export const NAME_UPPER_CASE_REQUEST = 'NAME_UPPER_CASE_REQUEST'; 8 | export const NAME_UPPER_CASE_SUCCESS = 'NAME_UPPER_CASE_SUCCESS'; 9 | export const NAME_UPPER_CASE_FAILURE = 'NAME_UPPER_CASE_FAILURE'; 10 | 11 | /** 12 | * Action Creators 13 | */ 14 | // 请求 action 15 | export const NAME_LOWER_CASERequest = createAction(NAME_UPPER_CASE_REQUEST); 16 | 17 | // 操作成功 18 | export const NAME_LOWER_CASESuccess = createAction(NAME_UPPER_CASE_SUCCESS, ({ id }) => ({ id })); 19 | 20 | // 操作失败 21 | export const NAME_LOWER_CASEFailure = createAction(NAME_UPPER_CASE_FAILURE, (error) => ({ error })); 22 | 23 | /** 24 | * Initial State 25 | */ 26 | export const INITIAL_STATE = immutable({ 27 | error: null, 28 | fetching: false 29 | }); 30 | 31 | /** 32 | * Reducers 33 | */ 34 | export default handleActions({ 35 | NAME_UPPER_CASE_REQUEST: (state) => 36 | state.merge({ fetching: true }), 37 | NAME_UPPER_CASE_SUCCESS: (state, { payload }) => 38 | state.merge({ fetching: false, error: null, ...payload }), 39 | NAME_UPPER_CASE_FAILURE: (state, { payload }) => 40 | state.merge({ fetching: false, error: payload.error }) 41 | }, INITIAL_STATE); 42 | -------------------------------------------------------------------------------- /templates/saga/index.js: -------------------------------------------------------------------------------- 1 | import { put } from 'redux-saga/effects'; 2 | import request from 'al-request'; 3 | import * as NAME_LOWER_CASEActions from '../redux/NAME_LOWER_CASE'; 4 | 5 | export default function* NAME_LOWER_CASESaga() { 6 | try { 7 | // 在这里写异步操作代码 8 | let data = yield request.post('api/NAME_LOWER_CASE'); 9 | 10 | // 将异步操作结果更新至Redux 11 | yield put(NAME_LOWER_CASEActions.NAME_LOWER_CASESuccess(data)); 12 | } catch (error) { 13 | yield put(NAME_LOWER_CASEActions.NAME_LOWER_CASEFailure(error)); 14 | } 15 | } 16 | --------------------------------------------------------------------------------