├── .gitignore ├── LICENSE ├── package-lock.json ├── package.json ├── readme.md └── src ├── alias.js └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 鹏展博 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 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-miniprogram-path-alias", 3 | "version": "0.2.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "ansi-colors": { 8 | "version": "1.1.0", 9 | "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-1.1.0.tgz", 10 | "integrity": "sha512-SFKX67auSNoVR38N3L+nvsPjOE0bybKTYbkf5tRvushrAPQ9V75huw0ZxBkKVeRU9kqH3d6HA4xTckbwZ4ixmA==", 11 | "dev": true, 12 | "requires": { 13 | "ansi-wrap": "^0.1.0" 14 | } 15 | }, 16 | "ansi-wrap": { 17 | "version": "0.1.0", 18 | "resolved": "https://registry.npmjs.org/ansi-wrap/-/ansi-wrap-0.1.0.tgz", 19 | "integrity": "sha1-qCJQ3bABXponyoLoLqYDu/pF768=", 20 | "dev": true 21 | }, 22 | "arr-diff": { 23 | "version": "4.0.0", 24 | "resolved": "https://registry.npmjs.org/arr-diff/-/arr-diff-4.0.0.tgz", 25 | "integrity": "sha1-1kYQdP6/7HHn4VI1dhoyml3HxSA=", 26 | "dev": true 27 | }, 28 | "arr-union": { 29 | "version": "3.1.0", 30 | "resolved": "https://registry.npmjs.org/arr-union/-/arr-union-3.1.0.tgz", 31 | "integrity": "sha1-45sJrqne+Gao8gbiiK9jkZuuOcQ=", 32 | "dev": true 33 | }, 34 | "assign-symbols": { 35 | "version": "1.0.0", 36 | "resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz", 37 | "integrity": "sha1-WWZ/QfrdTyDMvCu5a41Pf3jsA2c=", 38 | "dev": true 39 | }, 40 | "core-util-is": { 41 | "version": "1.0.2", 42 | "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", 43 | "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", 44 | "dev": true 45 | }, 46 | "extend-shallow": { 47 | "version": "3.0.2", 48 | "resolved": "https://registry.npmjs.org/extend-shallow/-/extend-shallow-3.0.2.tgz", 49 | "integrity": "sha1-Jqcarwc7OfshJxcnRhMcJwQCjbg=", 50 | "dev": true, 51 | "requires": { 52 | "assign-symbols": "^1.0.0", 53 | "is-extendable": "^1.0.1" 54 | } 55 | }, 56 | "inherits": { 57 | "version": "2.0.4", 58 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", 59 | "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", 60 | "dev": true 61 | }, 62 | "is-extendable": { 63 | "version": "1.0.1", 64 | "resolved": "https://registry.npmjs.org/is-extendable/-/is-extendable-1.0.1.tgz", 65 | "integrity": "sha512-arnXMxT1hhoKo9k1LZdmlNyJdDDfy2v0fXjFlmok4+i8ul/6WlbVge9bhM74OpNPQPMGUToDtz+KXa1PneJxOA==", 66 | "dev": true, 67 | "requires": { 68 | "is-plain-object": "^2.0.4" 69 | } 70 | }, 71 | "is-plain-object": { 72 | "version": "2.0.4", 73 | "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz", 74 | "integrity": "sha512-h5PpgXkWitc38BBMYawTYMWJHFZJVnBquFE57xFpjB8pJFiF6gZ+bU+WyI/yqXiFR5mdLsgYNaPe8uao6Uv9Og==", 75 | "dev": true, 76 | "requires": { 77 | "isobject": "^3.0.1" 78 | } 79 | }, 80 | "isarray": { 81 | "version": "1.0.0", 82 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 83 | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", 84 | "dev": true 85 | }, 86 | "isobject": { 87 | "version": "3.0.1", 88 | "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", 89 | "integrity": "sha1-TkMekrEalzFjaqH5yNHMvP2reN8=", 90 | "dev": true 91 | }, 92 | "plugin-error": { 93 | "version": "1.0.1", 94 | "resolved": "https://registry.npmjs.org/plugin-error/-/plugin-error-1.0.1.tgz", 95 | "integrity": "sha512-L1zP0dk7vGweZME2i+EeakvUNqSrdiI3F91TwEoYiGrAfUXmVv6fJIq4g82PAXxNsWOp0J7ZqQy/3Szz0ajTxA==", 96 | "dev": true, 97 | "requires": { 98 | "ansi-colors": "^1.0.1", 99 | "arr-diff": "^4.0.0", 100 | "arr-union": "^3.1.0", 101 | "extend-shallow": "^3.0.2" 102 | } 103 | }, 104 | "process-nextick-args": { 105 | "version": "2.0.1", 106 | "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", 107 | "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", 108 | "dev": true 109 | }, 110 | "readable-stream": { 111 | "version": "2.3.6", 112 | "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", 113 | "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", 114 | "dev": true, 115 | "requires": { 116 | "core-util-is": "~1.0.0", 117 | "inherits": "~2.0.3", 118 | "isarray": "~1.0.0", 119 | "process-nextick-args": "~2.0.0", 120 | "safe-buffer": "~5.1.1", 121 | "string_decoder": "~1.1.1", 122 | "util-deprecate": "~1.0.1" 123 | } 124 | }, 125 | "safe-buffer": { 126 | "version": "5.1.2", 127 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", 128 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", 129 | "dev": true 130 | }, 131 | "string_decoder": { 132 | "version": "1.1.1", 133 | "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", 134 | "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", 135 | "dev": true, 136 | "requires": { 137 | "safe-buffer": "~5.1.0" 138 | } 139 | }, 140 | "through2": { 141 | "version": "2.0.5", 142 | "resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz", 143 | "integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==", 144 | "dev": true, 145 | "requires": { 146 | "readable-stream": "~2.3.6", 147 | "xtend": "~4.0.1" 148 | } 149 | }, 150 | "util-deprecate": { 151 | "version": "1.0.2", 152 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", 153 | "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", 154 | "dev": true 155 | }, 156 | "xtend": { 157 | "version": "4.0.2", 158 | "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", 159 | "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", 160 | "dev": true 161 | } 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-miniprogram-path-alias", 3 | "version": "0.3.1", 4 | "description": "小程序路径别名", 5 | "main": "src/index.js", 6 | "scripts": {}, 7 | "keywords": [ 8 | "gulp", 9 | "plugins", 10 | "miniprogram", 11 | "path alias", 12 | "小程序", 13 | "xiaochengxu" 14 | ], 15 | "author": "pengzhanbo", 16 | "license": "MIT", 17 | "devDependencies": { 18 | "plugin-error": "^1.0.1", 19 | "through2": "^2.0.5" 20 | }, 21 | "repository": { 22 | "type": "git", 23 | "url": "git+https://volodymyr-lian@github.com/volodymyr-lian/gulp-miniprogram-path-alias.git" 24 | }, 25 | "bugs": { 26 | "url": "https://github.com/volodymyr-lian/gulp-miniprogram-path-alias/issues" 27 | }, 28 | "homepage": "https://github.com/volodymyr-lian/gulp-miniprogram-path-alias#readme" 29 | } 30 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## gulp-miniprogram-path-alias 2 | 3 | ### 小程序 依赖引用路径别名 4 | 5 | - 支持在 `wxml`, `wxs`, `wxss`, `js` 中对资源文件使用别名引用依赖; 6 | - 支持本地资源别名; 7 | - 支持远程资源别名; 8 | - 支持小程序资源相关组件的引用别名(如:image/video/audio/web-view等)。 9 | 10 | ### Install 11 | ``` bash 12 | npm install --save-dev gulp-miniprogram-path-alias 13 | ``` 14 | 15 | ### Usage 16 | 17 | ``` bash 18 | . 19 | ├── src 20 | │ └── common 21 | └── gulpfile.js 22 | ``` 23 | 24 | 在你的 `gulpfile.js` 添加一个 `task` 25 | ``` js 26 | var alias = require('gulp-miniprogram-path-alias'); 27 | var path = require('path'); 28 | 29 | function _join(dirname) { 30 | return path.join(__dirname, '..', 'src', dirname); 31 | } 32 | var aliasConfig = { 33 | common: _join('common'), 34 | @act: 'https://m.abc.com/activity/' 35 | }; 36 | 37 | gulp.task('alias', function () { 38 | gulp.src('src/**/*.{js,wxml,wxss}') 39 | .pipe(alias(aliasConfig)) 40 | .pipe(gulp.dest('/build')); 41 | }); 42 | ``` 43 | `*.js` : 44 | ``` js 45 | var common = require('common'); 46 | // or 47 | import common from 'common'; 48 | ``` 49 | `*.wxml` : 50 | ``` html 51 | 52 | ``` 53 | ``` html 54 | 55 | 56 | 57 | ``` 58 | `*.wxss` : 59 | ``` css 60 | @import "common/style.wxss"; 61 | ``` 62 | 63 | ### 建议 64 | -------------------------------------------------------------------------------- /src/alias.js: -------------------------------------------------------------------------------- 1 | let path = require('path'); 2 | 3 | // js 4 | let es5 = /require\(['"](.*?)['"]\)/g; 5 | let es6 = /(?:from|import)\s+['"](.*?)['"]/g; 6 | // wxml : import|wxs|image|audio|video|live-player|live-pusher|web-view 7 | let wxml = /(?:src|url|poster)=['"](.*?)['"]/g; 8 | // css 9 | let css = /@import\s+['"](.*?)['"]/g; 10 | 11 | let keysPattern; 12 | let opt; 13 | let filePath; 14 | 15 | function alias(options, content, _filePath) { 16 | opt = options = options || {}; 17 | filePath = _filePath; 18 | setKeysPattern(options); 19 | switch (path.extname(_filePath)) { 20 | case '.js': 21 | case '.wxs': 22 | content = content.replace(es6, replaceCallback); 23 | content = content.replace(es5, replaceCallback); 24 | break; 25 | case '.wxml': 26 | content = content.replace(wxml, replaceCallback); 27 | break; 28 | case '.styl': 29 | case '.stylus': 30 | case '.less': 31 | case '.sass': 32 | case '.scss': 33 | case '.css': 34 | case '.wxss': 35 | content = content.replace(css, replaceCallback); 36 | break; 37 | } 38 | return content; 39 | }; 40 | 41 | function setKeysPattern(options) { 42 | options = options || {}; 43 | keysPattern = keysPattern || new RegExp('^(' + Object.keys(options).join('|') + ')'); 44 | } 45 | 46 | function replaceCallback(match, subMatch) { 47 | if (keysPattern.test(subMatch)) { 48 | let key = subMatch.substr(0, subMatch.indexOf('/')); 49 | let url = path.join(opt[key], subMatch.slice(subMatch.indexOf('/'))); 50 | return match.replace( 51 | subMatch, 52 | /^https?:\/\//.test(opt[key]) ? url : path.relative(filePath, url).replace(/\\/g, '/').replace(/^\.\.\//, '') 53 | ); 54 | } else { 55 | return match; 56 | } 57 | } 58 | 59 | module.exports = alias; -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | let through = require('through2'); 2 | var PluginError = require('plugin-error'); 3 | var alias = require('./alias'); 4 | 5 | module.exports = function (options) { 6 | return through.obj(function (file, enc, cb) { 7 | if (file.isNull()) { 8 | return cb(null, file); 9 | } 10 | if (file.isStream()) { 11 | return cb(new PluginError('gulp-miniprogram-path-alias', 'stream not supported')); 12 | } 13 | var content = file.contents.toString('utf8'); 14 | content = alias(options, content, file.path); 15 | file.contents = Buffer.from(content); 16 | cb(null, file); 17 | }); 18 | } 19 | --------------------------------------------------------------------------------