gulp-path-alias
Advanced tools
+7
-0
@@ -5,2 +5,9 @@ # Changelog | ||
| ## [1.2.0](https://github.com/CryUshio/gulp-path-alias/compare/v1.1.3...v1.2.0) (2021-01-18) | ||
| ### Features | ||
| * 支持类 webpack 全字匹配, 支持 alias 带斜线, 支持替换为相对路径 ([adad6cd](https://github.com/CryUshio/gulp-path-alias/commit/adad6cde1688a3aeb25b5b6437594056fd9e7fef)) | ||
| ### [1.1.3](https://github.com/CryUshio/gulp-path-alias/compare/v1.1.2...v1.1.3) (2021-01-08) | ||
@@ -7,0 +14,0 @@ |
+77
-40
@@ -9,18 +9,37 @@ "use strict"; | ||
| const slash_1 = __importDefault(require("slash")); | ||
| const prefixPattenMap = { | ||
| js: `import\\s*[^'"]*\\(?|from|require\\s*\\(`, | ||
| const prefixPatternMap = { | ||
| js: 'import\\s*[^\'"]*\\(?|from|require\\s*\\(', | ||
| // poster: wxml | ||
| xml: `src=|url=|poster=|href=`, | ||
| css: `@import\\s*|url\\s*\\(` | ||
| xml: 'src=|url=|poster=|href=', | ||
| css: '@import\\s*|url\\s*\\(', | ||
| }; | ||
| const suffixPatten = `\\/|['"]|\\s*\\)`; | ||
| function getRegExp(prefixPatten) { | ||
| return function (aliasName) { | ||
| return new RegExp(`(?:(${prefixPatten})\\s*['"]?\\s*)${aliasName}(${suffixPatten})`, "gm"); | ||
| }; | ||
| /* 全匹配的正则规则 */ | ||
| const exactMatchPattern = /\$$/; | ||
| /** | ||
| * 编码别名替换项 | ||
| * eg. '/' -> '\\/' | ||
| */ | ||
| function encodeAliasString(alias) { | ||
| return alias.replace('/', '\\/'); | ||
| } | ||
| /* 获取匹配前缀正则字符串 */ | ||
| function getPrefixPatternString(prefixPatten) { | ||
| return `(?:(${prefixPatten})\\s*['"]?\\s*)`; | ||
| } | ||
| /* 获取匹配后缀正则字符串 */ | ||
| function getSuffixPatternString(exactMatch = false) { | ||
| const suffixPattern = '[\'"]|\\s*\\)'; | ||
| // 全匹配时,后面没有内容,拿掉斜线 | ||
| return exactMatch ? suffixPattern : `\\/|${suffixPattern}`; | ||
| } | ||
| /* 获取匹配主体和后缀的正则字符串 */ | ||
| function getRemainPatternString(aliasKey, exactMatch) { | ||
| const _aliasKey = exactMatch ? aliasKey.replace(exactMatchPattern, '') : aliasKey; | ||
| return `${_aliasKey}(${getSuffixPatternString(exactMatch)})`; | ||
| } | ||
| /* 获取相对路径 */ | ||
| function relative(from, to) { | ||
| const relativePath = slash_1.default(path_1.default.relative(from, to)); | ||
| if (!relativePath) { | ||
| return "."; | ||
| return '.'; | ||
| } | ||
@@ -30,44 +49,50 @@ return !/^\./.test(relativePath) ? `./${relativePath}` : relativePath; | ||
| // 100000 rows * 100 columns -> 248ms | ||
| function replaceAll(file, dirname, aliasMap) { | ||
| function replaceAll(file, dirname, aliasList) { | ||
| const ext = path_1.default.extname(file.relative); | ||
| const isStream = file.isStream(); | ||
| let reg; | ||
| /* 根据后缀名获得前缀正则字符串,降低复杂度 */ | ||
| let prefixPatternString; | ||
| switch (ext) { | ||
| // js | ||
| case ".js": | ||
| case ".ts": | ||
| case ".wxs": | ||
| reg = getRegExp(prefixPattenMap.js); | ||
| case '.js': | ||
| case '.ts': | ||
| case '.wxs': | ||
| prefixPatternString = getPrefixPatternString(prefixPatternMap.js); | ||
| break; | ||
| // css | ||
| case ".css": | ||
| case ".less": | ||
| case ".scss": | ||
| case ".styl": | ||
| case ".stylus": | ||
| case ".wxss": | ||
| reg = getRegExp(prefixPattenMap.css); | ||
| case '.css': | ||
| case '.less': | ||
| case '.scss': | ||
| case '.sass': | ||
| case '.styl': | ||
| case '.stylus': | ||
| case '.wxss': | ||
| prefixPatternString = getPrefixPatternString(prefixPatternMap.css); | ||
| break; | ||
| // xml | ||
| case ".html": | ||
| case ".wxml": | ||
| reg = getRegExp(prefixPattenMap.xml); | ||
| case '.html': | ||
| case '.wxml': | ||
| prefixPatternString = getPrefixPatternString(prefixPatternMap.xml); | ||
| break; | ||
| case ".jsx": | ||
| case ".tsx": | ||
| case '.jsx': | ||
| case '.tsx': | ||
| default: | ||
| reg = getRegExp(Object.keys(prefixPattenMap) | ||
| .map((k) => prefixPattenMap[k]) | ||
| .join("|")); | ||
| prefixPatternString = getPrefixPatternString(Object.keys(prefixPatternMap) | ||
| .map((k) => prefixPatternMap[k]) | ||
| .join('|')); | ||
| break; | ||
| } | ||
| Object.keys(aliasMap).forEach((alias) => { | ||
| const regExp = reg(alias); | ||
| const subReg = new RegExp(`${alias}(${suffixPatten})`); | ||
| const replacer = `${relative(dirname, aliasMap[alias])}$1`; | ||
| aliasList.forEach(({ aliasKey, aliasValue }) => { | ||
| const isExactMatch = exactMatchPattern.test(aliasKey); | ||
| const remainPatternString = getRemainPatternString(aliasKey, isExactMatch); | ||
| const sentenceReg = new RegExp(`${prefixPatternString}${remainPatternString}`, 'gm'); | ||
| const subReg = new RegExp(remainPatternString); | ||
| /* 如果替换路径是相对路径不使用 relative 路径替换,而是直接替换 */ | ||
| const replacer = path_1.default.isAbsolute(aliasValue) ? `${relative(dirname, aliasValue)}$1` : `${aliasValue}$1`; | ||
| /* 先确定文件中匹配的语句,再替换其中所有匹配的 alias */ | ||
| if (isStream) { | ||
| file.contents = file.contents.pipe(replacestream_1.default(regExp, (match) => match.replace(subReg, replacer))); | ||
| file.contents = file.contents.pipe(replacestream_1.default(sentenceReg, (match) => match.replace(subReg, replacer))); | ||
| } | ||
| else { | ||
| file.contents = Buffer.from(String(file.contents).replace(regExp, (match) => match.replace(subReg, replacer))); | ||
| file.contents = Buffer.from(String(file.contents).replace(sentenceReg, (match) => match.replace(subReg, replacer))); | ||
| } | ||
@@ -102,9 +127,21 @@ }); | ||
| const { paths } = _options; | ||
| const emptyAlias = !Object.keys(paths).length; | ||
| const isEmptyAlias = !Object.keys(paths).length; | ||
| /* 初始化 aliasList,全局使用,避免重复计算 */ | ||
| const aliasList = Object.keys(paths) | ||
| /* 全匹配的需要优先替换,放在前面 */ | ||
| .sort((a) => (exactMatchPattern.test(a) ? -1 : 1)) | ||
| .map((aliasKey) => { | ||
| /* 替换斜线,否则生成 pattern 时会出错 */ | ||
| const encodeKey = encodeAliasString(aliasKey); | ||
| return { | ||
| aliasKey: encodeKey, | ||
| aliasValue: paths[aliasKey], | ||
| }; | ||
| }); | ||
| return through2_1.default.obj(function (file, _, cb) { | ||
| const dirname = path_1.default.dirname(file.path); | ||
| if (file.isNull() || emptyAlias) { | ||
| if (file.isNull() || isEmptyAlias) { | ||
| return cb(null, file); | ||
| } | ||
| file = replaceAll(file, dirname, paths); | ||
| file = replaceAll(file, dirname, aliasList); | ||
| cb(null, file); | ||
@@ -111,0 +148,0 @@ }); |
+2
-1
| { | ||
| "name": "gulp-path-alias", | ||
| "version": "1.1.3", | ||
| "version": "1.2.0", | ||
| "description": "path alias", | ||
@@ -59,2 +59,3 @@ "main": "lib/index.js", | ||
| "ava": "^3.13.0", | ||
| "eslint": "^7.18.0", | ||
| "gulp": "^4.0.2", | ||
@@ -61,0 +62,0 @@ "husky": "^4.3.7", |
+16
-0
@@ -10,2 +10,17 @@ # gulp-path-alias  | ||
| ## Usage | ||
| ### Rules | ||
| #### type | ||
| ```ts | ||
| interface AliasType { | ||
| [key: string]: string; | ||
| } | ||
| ``` | ||
| #### key | ||
| 1. 常规 `key`:正常匹配 | ||
| 2. 全字匹配 `key`:在常规 `key` 后加上 `$` 符号 | ||
| #### value | ||
| 1. 绝对路径:根据 `options.pwd` 替换为相对路径 | ||
| 2. 相对路径:直接替换 | ||
| ### Example | ||
| 配置 `gulp task` | ||
@@ -22,2 +37,3 @@ ```js | ||
| '@pages': path.resolve(__dirname, '../src/pages'), | ||
| 'wx$': 'wx/libs', // 将 'wx' 替换为 'wx/libs' | ||
| } | ||
@@ -24,0 +40,0 @@ })) |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
37581
7.19%171
27.61%75
27.12%1
-50%12
9.09%