gulp-path-alias
Advanced tools
+7
-5
@@ -12,6 +12,7 @@ const path = require('path'); | ||
| const suffixPatten = `\\/|['"]|\\s*\\)`; | ||
| function getRegExp(prefixPatten) { | ||
| return function (aliasName) { | ||
| return new RegExp(`(?:(${prefixPatten})\\s*['"]?\\s*)${aliasName}(\\/[^'";=)]*|['"]|\\s*\\))`, 'gm'); | ||
| return new RegExp(`(?:(${prefixPatten})\\s*['"]?\\s*)${aliasName}(${suffixPatten})`, 'gm'); | ||
| } | ||
@@ -32,3 +33,3 @@ } | ||
| function replaceAll(file, dirname, aliasMap) { | ||
| const ext = path.extname(file.relative); | ||
| const ext = path.extname(file.relative); | ||
| const isStream = file.isStream(); | ||
@@ -67,8 +68,9 @@ | ||
| const regExp = reg(alias); | ||
| const replacer = relative(dirname, aliasMap[alias]); | ||
| const subReg = new RegExp(`${alias}(${suffixPatten})`); | ||
| const replacer = `${relative(dirname, aliasMap[alias])}$1`; | ||
| if (isStream) { | ||
| file.contents = file.contents.pipe(replace(regExp, (match) => match.replace(alias, replacer))); | ||
| file.contents = file.contents.pipe(replace(regExp, (match) => match.replace(subReg, replacer))); | ||
| } else { | ||
| file.contents = Buffer.from(String(file.contents).replace(regExp, (match) => match.replace(alias, replacer))); | ||
| file.contents = Buffer.from(String(file.contents).replace(regExp, (match) => match.replace(subReg, replacer))); | ||
| } | ||
@@ -75,0 +77,0 @@ }); |
+1
-1
| { | ||
| "name": "gulp-path-alias", | ||
| "version": "1.0.1", | ||
| "version": "1.0.2", | ||
| "description": "path alias", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+22
-3
| # gulp-path-alias  | ||
| 别名路径替换 | ||
| `gulp` 路径别名 | ||
@@ -10,2 +10,3 @@ ## Install | ||
| ## Usage | ||
| 配置 `gulp task` | ||
| ```js | ||
@@ -26,3 +27,21 @@ const gulp = require('gulp'); | ||
| ``` | ||
| 在 `js` 使用:(支持 `js`、`ts`、`wxs`) | ||
| ```js | ||
| import foo from '@libs/foo'; | ||
| require('@libs/foo'); | ||
| ``` | ||
| 在 `html` 使用:(支持 `html`、`wxml`) | ||
| ```html | ||
| <img src="@assets/img/bg.png"> | ||
| ``` | ||
| 在 `css`:(支持 `css`、`less` 等) | ||
| ```css | ||
| @import "@assets/css/var.less"; | ||
| .bg { | ||
| background-image: url(@assets/img/bg.png); | ||
| } | ||
| ``` | ||
| ## API | ||
@@ -34,9 +53,9 @@ ### alias(options) | ||
| ##### cwd | ||
| Type: `string` | ||
| Type: `string` | ||
| Default: `process.cwd()` | ||
| ##### paths | ||
| Type: `object` | ||
| Type: `object` | ||
| Default: `{}` | ||
| Path alias map. |
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
31374
1.52%102
2%59
47.5%