From c0b111ba0c8b9c08d29047eed1a8d3ea43d2e735 Mon Sep 17 00:00:00 2001 From: duanlian <505441575@qq.com> Date: Sun, 3 Dec 2017 00:45:23 +0800 Subject: [PATCH 01/10] wepy-cli: Added aliasFields feature && mainFields feature --- packages/wepy-cli/src/compile-script.js | 6 +- packages/wepy-cli/src/compile-style.js | 10 +- packages/wepy-cli/src/compile-wpy.js | 13 ++- packages/wepy-cli/src/compile.js | 2 +- packages/wepy-cli/src/resolve.js | 122 +++++++++++++++++++++--- packages/wepy-cli/src/util.js | 4 + 6 files changed, 137 insertions(+), 20 deletions(-) diff --git a/packages/wepy-cli/src/compile-script.js b/packages/wepy-cli/src/compile-script.js index debbf54ed..5662b199d 100644 --- a/packages/wepy-cli/src/compile-script.js +++ b/packages/wepy-cli/src/compile-script.js @@ -43,8 +43,10 @@ export default { if (config.output === 'ant' && lib === 'wepy') { lib = 'wepy-ant'; } - lib = resolve.resolveAlias(lib); - if (path.isAbsolute(lib)) { + lib = resolve.resolveAlias(lib, opath); + if (lib === 'false') { + return `{}` + } else if (path.isAbsolute(lib)) { source = lib; target = util.getDistPath(source); } else if (lib[0] === '.') { // require('./something''); diff --git a/packages/wepy-cli/src/compile-style.js b/packages/wepy-cli/src/compile-style.js index 071837d17..ea379a35f 100644 --- a/packages/wepy-cli/src/compile-style.js +++ b/packages/wepy-cli/src/compile-style.js @@ -99,12 +99,14 @@ export default { requires.forEach((r) => { let comsrc = null; isNPM = false; - if (path.isAbsolute(r)) { - if (path.extname(r) === '' && util.isFile(r + ext)) { - comsrc = r + ext; + let lib = resolve.resolveAlias(r, opath, ext); + + if (path.isAbsolute(lib)) { + if (path.extname(lib) === '' && util.isFile(lib + ext)) { + comsrc = lib + ext; } } else { - let lib = resolve.resolveAlias(r); + // let lib = resolve.resolveAlias(r, opath); if (path.isAbsolute(lib)) { comsrc = lib; } else { diff --git a/packages/wepy-cli/src/compile-wpy.js b/packages/wepy-cli/src/compile-wpy.js index cd9d5e6c4..e3fc5ee90 100644 --- a/packages/wepy-cli/src/compile-wpy.js +++ b/packages/wepy-cli/src/compile-wpy.js @@ -254,7 +254,8 @@ export default { (() => { let coms = {}; rst.script.code.replace(/import\s*([\w\-\_]*)\s*from\s*['"]([\w\-\_\.\/\@]*)['"]/ig, (match, com, path) => { - coms[com] = path; + if (path !== 'false') + coms[com] = path; }); let match = rst.script.code.match(/[\s\r\n]components\s*=[\s\r\n]*/); @@ -373,7 +374,7 @@ export default { } }); if (Object.keys(props).length) { - rst.script.code =rst.script.code.replace(/[\s\r\n]components\s*=[\s\r\n]*/, (match, item, index) => { + rst.script.code = rst.script.code.replace(/[\s\r\n]components\s*=[\s\r\n]*/, (match, item, index) => { return `$repeat = ${JSON.stringify($repeat)};\r\n$props = ${JSON.stringify(props)};\r\n$events = ${JSON.stringify(events)};\r\n${match}`; }); } @@ -471,6 +472,14 @@ export default { } else { requires.push(path.join(opath.dir, wpy.template.components[k])); } + + // 去重 + // Example: + // components = { + // Count1: '../components/count', + // Count2: '../components/count' + // }; + requires = util.unique(requires) } } try { diff --git a/packages/wepy-cli/src/compile.js b/packages/wepy-cli/src/compile.js index d64e2cce6..21a440878 100644 --- a/packages/wepy-cli/src/compile.js +++ b/packages/wepy-cli/src/compile.js @@ -50,7 +50,7 @@ export default { reg = new RegExp('\\' + ext + '$'); if (!reg.test(importpath)) importpath = importpath + ext; - let resolved = resolve.resolveAlias(importpath); + let resolved = resolve.resolveAlias(importpath, opath); let compath; if (path.isAbsolute(resolved)) { compath = path.resolve(resolved); diff --git a/packages/wepy-cli/src/resolve.js b/packages/wepy-cli/src/resolve.js index f5353b2e4..361d2a852 100644 --- a/packages/wepy-cli/src/resolve.js +++ b/packages/wepy-cli/src/resolve.js @@ -10,17 +10,67 @@ import path from 'path'; import util from './util'; +import cache from './cache' + +const DEFAULT_MODULES = ['node_modules']; +const DEFAULT_ALIASFIELDS = ['wepy', 'weapp', 'browser']; +const DEFAULT_MAINFIELDS = ['wepy', 'weapp', 'browser', 'module', 'main']; export default { init (config) { - this.modules = config.modules || ['node_modules']; this.alias = config.alias; - - if (typeof this.modules === 'string') { - this.modules = [this.modules]; - } - - let cwd = process.cwd(); + this.modules = config.modules || DEFAULT_MODULES; + this.aliasFields = config.aliasFields || DEFAULT_ALIASFIELDS; + this.mainFields = config.mainFields || DEFAULT_MAINFIELDS; + + ['modules', 'aliasFields', 'mainFields'].forEach(opt => { + typeof this[opt] === 'string' && ( + this[opt] = [].concat(this[opt]) + ); + }); + + let pkgFile = util.getPkg(); + let pkg = JSON.parse(pkgFile); + let cwd = util.currentDir; + let ext = cache.getExt(); + + // 优先级递减 + this.aliasFields.forEach(fields => { + // 归类 + util.isObject(pkg[fields]) && Object.keys(pkg[fields] || {}).forEach(key => { + // module形式的fieldsAlias归置于alias中,例: "xyz": "./src/xyz.js",alias优先级较大 + if (key.indexOf('.') === -1) { + // => "xyz"、"xyz-xyz" + let value; + if (!pkg[fields][key]) { + value = 'false' + } else { + value = path.resolve(cwd, pkg[fields][key]) + } + + // fields中key或value路径后缀与配置缺省值相同时,replace后缀 + if (path.extname(value) === ext) + value = value.replace(ext, '') + + this.alias = Object.assign({}, { [key]: value }, this.alias || {}); + } + // fields转换:"./src/index.wpy" => "src/index.wpy"后合并至fieldsAlias中 + if (!path.isAbsolute(key) && path.extname(key)) { + // => "./src/xyz.js"、"src/xyz.js" + let value = path.resolve(cwd, pkg[fields][key]) + key = path.resolve(cwd, key) + + // fields中key或value路径后缀与配置缺省值相同时,replace后缀 + if (path.extname(key) === ext) + key = key.replace(ext, '') + + if (path.extname(value) === ext) + value = value.replace(ext, '') + + this.fieldsAlias = Object.assign({}, { [key]: value }, this.fieldsAlias || {}); + } + }); + }); this.modulePaths = this.modules.map(v => { if (path.isAbsolute(v)) { @@ -95,10 +145,19 @@ export default { if (!o) { return null; } - let main = o.pkg.main || 'index.js'; - if (o.pkg.browser && typeof o.pkg.browser === 'string') { - main = o.pkg.browser; + + // 优先级递减 + let mainField, main + for (let i = 0, l = this.mainFields.length; i < l; i++) { + mainField = this.mainFields[i]; + if (o.pkg[mainField] && typeof o.pkg[mainField] === 'string') { + main = o.pkg[mainField]; + break; + } } + + main = main || 'index.js'; + return { file: main, modulePath: o.modulePath, @@ -106,14 +165,53 @@ export default { dir: o.dir }; }, + + resolveFieldsAlias (lib) { + return lib && this.fieldsAlias && this.fieldsAlias[lib] + ? this.fieldsAlias[lib] + : lib; + }, + + replaceFieldsAlias (currentAlias, opath) { + let absolutePath; + + if (path.isAbsolute(currentAlias)) { + absolutePath = currentAlias + + currentAlias = this.resolveFieldsAlias(absolutePath) !== absolutePath + ? this.resolveFieldsAlias(absolutePath) + : currentAlias; + } else if (currentAlias[0] === '.') { + absolutePath = path.join(opath.dir, currentAlias); + + currentAlias = this.resolveFieldsAlias(absolutePath) !== absolutePath + ? this.resolveFieldsAlias(absolutePath) + : currentAlias; + } else if ( + currentAlias.indexOf('/') === -1 || // require('asset'); + currentAlias.indexOf('/') === currentAlias.length - 1 || // require('a/b/something/') + (currentAlias[0] === '@' && currentAlias.indexOf('/') !== -1 && currentAlias.lastIndexOf('/') === currentAlias.indexOf('/')) // require('@abc/something') + ) { + const mainFile = this.getMainFile(currentAlias); + + if (mainFile) { + absolutePath = path.join(mainFile.dir, mainFile.file); + currentAlias = this.resolveFieldsAlias(absolutePath) !== absolutePath + ? this.resolveFieldsAlias(absolutePath) + : currentAlias; + } + } + return currentAlias; + }, - resolveAlias (lib) { + resolveAlias (lib, opath) { if (!this.alias) return lib; if (this._cacheAlias[lib]) { return this._cacheAlias[lib]; } let rst = lib; + let ext = cache.getExt(); for (let k in this.alias) { let alias = this.alias[k]; @@ -135,6 +233,8 @@ export default { if (!this._cacheAlias[lib]) { this._cacheAlias[lib] = lib; } + // replace field alias + this._cacheAlias[lib] = this.replaceFieldsAlias(this._cacheAlias[lib], opath) return this._cacheAlias[lib]; } } \ No newline at end of file diff --git a/packages/wepy-cli/src/util.js b/packages/wepy-cli/src/util.js index f89420827..e764c572c 100644 --- a/packages/wepy-cli/src/util.js +++ b/packages/wepy-cli/src/util.js @@ -478,6 +478,10 @@ const utils = { let ignoreFile = path.join(this.currentDir, path.sep, '.wepyignore'); return this.isFile(ignoreFile) ? this.readFile(ignoreFile) : ''; }, + getPkg () { + let pkgFile = path.join(this.currentDir, path.sep, 'package.json'); + return this.isFile(pkgFile) ? this.readFile(pkgFile) : '{}'; + }, getFiles (dir = process.cwd(), prefix = '') { let cfiles = cache.getFileList(dir); if (cfiles) From 6fef14ea5c76a8507d1291fe82a20582a55bd4c0 Mon Sep 17 00:00:00 2001 From: duanlian <505441575@qq.com> Date: Sun, 3 Dec 2017 20:33:58 +0800 Subject: [PATCH 02/10] wepy-cli: [aliasFields Feature]removed ineffectiveness determine statements --- packages/wepy-cli/src/compile-wpy.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/wepy-cli/src/compile-wpy.js b/packages/wepy-cli/src/compile-wpy.js index e3fc5ee90..f63ac0b05 100644 --- a/packages/wepy-cli/src/compile-wpy.js +++ b/packages/wepy-cli/src/compile-wpy.js @@ -254,8 +254,7 @@ export default { (() => { let coms = {}; rst.script.code.replace(/import\s*([\w\-\_]*)\s*from\s*['"]([\w\-\_\.\/\@]*)['"]/ig, (match, com, path) => { - if (path !== 'false') - coms[com] = path; + coms[com] = path; }); let match = rst.script.code.match(/[\s\r\n]components\s*=[\s\r\n]*/); From e3ebbb5012eb8eb09825659df0eaa433294084cd Mon Sep 17 00:00:00 2001 From: duanlian <505441575@qq.com> Date: Sun, 3 Dec 2017 20:36:19 +0800 Subject: [PATCH 03/10] wepy-cli: [aliasFields Feature]update resolve.js && fixed typos --- packages/wepy-cli/src/compile-style.js | 2 +- packages/wepy-cli/src/resolve.js | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/wepy-cli/src/compile-style.js b/packages/wepy-cli/src/compile-style.js index ea379a35f..7b4d1257a 100644 --- a/packages/wepy-cli/src/compile-style.js +++ b/packages/wepy-cli/src/compile-style.js @@ -99,7 +99,7 @@ export default { requires.forEach((r) => { let comsrc = null; isNPM = false; - let lib = resolve.resolveAlias(r, opath, ext); + let lib = resolve.resolveAlias(r, opath); if (path.isAbsolute(lib)) { if (path.extname(lib) === '' && util.isFile(lib + ext)) { diff --git a/packages/wepy-cli/src/resolve.js b/packages/wepy-cli/src/resolve.js index 361d2a852..f7fcbd680 100644 --- a/packages/wepy-cli/src/resolve.js +++ b/packages/wepy-cli/src/resolve.js @@ -43,29 +43,29 @@ export default { // => "xyz"、"xyz-xyz" let value; if (!pkg[fields][key]) { - value = 'false' + value = 'false'; } else { - value = path.resolve(cwd, pkg[fields][key]) + value = path.resolve(cwd, pkg[fields][key]); } // fields中key或value路径后缀与配置缺省值相同时,replace后缀 if (path.extname(value) === ext) - value = value.replace(ext, '') + value = value.replace(ext, ''); this.alias = Object.assign({}, { [key]: value }, this.alias || {}); } // fields转换:"./src/index.wpy" => "src/index.wpy"后合并至fieldsAlias中 - if (!path.isAbsolute(key) && path.extname(key)) { + if (!path.isAbsolute(key)) { // => "./src/xyz.js"、"src/xyz.js" - let value = path.resolve(cwd, pkg[fields][key]) - key = path.resolve(cwd, key) + let value = path.resolve(cwd, pkg[fields][key]); + key = path.resolve(cwd, key); // fields中key或value路径后缀与配置缺省值相同时,replace后缀 if (path.extname(key) === ext) - key = key.replace(ext, '') + key = key.replace(ext, ''); if (path.extname(value) === ext) - value = value.replace(ext, '') + value = value.replace(ext, ''); this.fieldsAlias = Object.assign({}, { [key]: value }, this.fieldsAlias || {}); } @@ -176,7 +176,7 @@ export default { let absolutePath; if (path.isAbsolute(currentAlias)) { - absolutePath = currentAlias + absolutePath = currentAlias; currentAlias = this.resolveFieldsAlias(absolutePath) !== absolutePath ? this.resolveFieldsAlias(absolutePath) @@ -234,7 +234,7 @@ export default { this._cacheAlias[lib] = lib; } // replace field alias - this._cacheAlias[lib] = this.replaceFieldsAlias(this._cacheAlias[lib], opath) + this._cacheAlias[lib] = this.replaceFieldsAlias(this._cacheAlias[lib], opath); return this._cacheAlias[lib]; } } \ No newline at end of file From b37f6219fade584bf994a7f42240c6a35ec1c2c2 Mon Sep 17 00:00:00 2001 From: duanlian <505441575@qq.com> Date: Sun, 3 Dec 2017 22:44:36 +0800 Subject: [PATCH 04/10] wepy-cli: [aliasFields Feature]update resolve.js --- packages/wepy-cli/src/resolve.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/wepy-cli/src/resolve.js b/packages/wepy-cli/src/resolve.js index f7fcbd680..dc6619c00 100644 --- a/packages/wepy-cli/src/resolve.js +++ b/packages/wepy-cli/src/resolve.js @@ -53,10 +53,8 @@ export default { value = value.replace(ext, ''); this.alias = Object.assign({}, { [key]: value }, this.alias || {}); - } - // fields转换:"./src/index.wpy" => "src/index.wpy"后合并至fieldsAlias中 - if (!path.isAbsolute(key)) { - // => "./src/xyz.js"、"src/xyz.js" + } else if (!path.isAbsolute(key)) { + // relative path let value = path.resolve(cwd, pkg[fields][key]); key = path.resolve(cwd, key); From a5d5d43ea800113bce5d3be990bfef1a9511446d Mon Sep 17 00:00:00 2001 From: duanlian <505441575@qq.com> Date: Sun, 3 Dec 2017 22:45:05 +0800 Subject: [PATCH 05/10] wepy-cli: [aliasFields Feature]Added examples --- .../wepy-cli/templates/template/package.json | 4 ++ .../template/src/components/wepy-list.wpy | 55 +++++++++++++++++++ .../template/src/pages/index-redux.wpy | 6 +- .../templates/template/src/pages/index.wpy | 5 +- .../templates/template/wepy.config.js | 1 + 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 packages/wepy-cli/templates/template/src/components/wepy-list.wpy diff --git a/packages/wepy-cli/templates/template/package.json b/packages/wepy-cli/templates/template/package.json index db59540a7..af4d83220 100644 --- a/packages/wepy-cli/templates/template/package.json +++ b/packages/wepy-cli/templates/template/package.json @@ -8,6 +8,10 @@ "build": "cross-env NODE_ENV=production wepy build --no-cache", "test": "echo \"Error: no test specified\" && exit 1" }, + "wepy": { + "module-a": false, + "./src/components/list": "./src/components/wepy-list.wpy" + }, "author": "", "license": "MIT" } diff --git a/packages/wepy-cli/templates/template/src/components/wepy-list.wpy b/packages/wepy-cli/templates/template/src/components/wepy-list.wpy new file mode 100644 index 000000000..07ac10620 --- /dev/null +++ b/packages/wepy-cli/templates/template/src/components/wepy-list.wpy @@ -0,0 +1,55 @@ + + + diff --git a/packages/wepy-cli/templates/template/src/pages/index-redux.wpy b/packages/wepy-cli/templates/template/src/pages/index-redux.wpy index b76ad3a41..8f06f82e1 100644 --- a/packages/wepy-cli/templates/template/src/pages/index-redux.wpy +++ b/packages/wepy-cli/templates/template/src/pages/index-redux.wpy @@ -84,12 +84,16 @@ import wepy from 'wepy' import { connect } from 'wepy-redux' import List from '../components/list' - import Panel from '../components/panel' + import Panel from '@/components/panel' // alias example import Counter from 'counter' // alias example + import List from '../components/list' // aliasFields example + import moduleA from 'module-a' // aliasFields ignore module example import Group from '../components/group' import Toast from 'wepy-com-toast' import testMixin from '../mixins/test' + console.log('moduleA ignored: ', moduleA) // => moduleA ignored: {} + @connect({ num (state) { return state.counter.num diff --git a/packages/wepy-cli/templates/template/src/pages/index.wpy b/packages/wepy-cli/templates/template/src/pages/index.wpy index df70212ac..07e416084 100644 --- a/packages/wepy-cli/templates/template/src/pages/index.wpy +++ b/packages/wepy-cli/templates/template/src/pages/index.wpy @@ -79,13 +79,16 @@