Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

gulp-path-alias

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-path-alias - npm Package Compare versions

Comparing version
1.0.2
to
1.1.0
+16
CHANGELOG.md
# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## 1.1.0 (2021-01-04)
### Features
* update ([6da5270](https://github.com/CryUshio/gulp-path-alias/commit/6da52701d244106cee6117f5e3e7a36e0a9f456b))
* 支持ts ([b20688c](https://github.com/CryUshio/gulp-path-alias/commit/b20688c48944869d7d82e3e68f5c13df5e7e5359))
### Bug Fixes
* [@import](https://github.com/import) ([0a19386](https://github.com/CryUshio/gulp-path-alias/commit/0a193867ae0e25014b0bdf9df432543636b222a0))
/// <reference types="node" />
declare type AliasMapType = Record<string, string>;
declare type Options = {
cwd?: string;
paths?: AliasMapType;
};
/**
* 100000 rows * 100 columns -> 324ms
*
function replaceAll(file, pathname, aliasMap) {
const isStream = file.isStream();
const aliasList = Object.keys(aliasMap);
const aliasMatch = `(${aliasList.join('|')})`;
const reg = getRegExp(aliasMatch);
function replaceOne(match) {
return match.replace(new RegExp(aliasMatch), (m) => relative(pathname, aliasMap[m]));
}
if (isStream) {
file.contents = file.contents.pipe(replace(reg, replaceOne));
} else {
file.contents = Buffer.from(String(file.contents).replace(reg, replaceOne));
}
return file;
}
*/
export default function (options?: Options): import("stream").Transform;
export {};
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const through2_1 = __importDefault(require("through2"));
const replacestream_1 = __importDefault(require("replacestream"));
const prefixPattenMap = {
js: `import\\s*[^'"]*\\(?|from|require\\s*\\(`,
// poster: wxml
xml: `src=|url=|poster=`,
css: `@import\\s*|url\\s*\\(`,
};
const suffixPatten = `\\/|['"]|\\s*\\)`;
function getRegExp(prefixPatten) {
return function (aliasName) {
return new RegExp(`(?:(${prefixPatten})\\s*['"]?\\s*)${aliasName}(${suffixPatten})`, "gm");
};
}
function relative(from, to) {
const relativePath = path_1.default.relative(from, to);
if (!relativePath) {
return ".";
}
return !/^\./.test(relativePath) ? `./${relativePath}` : relativePath;
}
// 100000 rows * 100 columns -> 248ms
function replaceAll(file, dirname, aliasMap) {
const ext = path_1.default.extname(file.relative);
const isStream = file.isStream();
let reg;
switch (ext) {
// js
case ".js":
case ".ts":
case ".wxs":
reg = getRegExp(prefixPattenMap.js);
break;
// css
case ".css":
case ".less":
case ".scss":
case ".styl":
case ".stylus":
case ".wxss":
reg = getRegExp(prefixPattenMap.css);
break;
// xml
case ".html":
case ".wxml":
reg = getRegExp(prefixPattenMap.xml);
break;
case ".jsx":
case ".tsx":
default:
reg = getRegExp(Object.keys(prefixPattenMap)
.map((k) => prefixPattenMap[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`;
if (isStream) {
file.contents = file.contents.pipe(replacestream_1.default(regExp, (match) => match.replace(subReg, replacer)));
}
else {
file.contents = Buffer.from(String(file.contents).replace(regExp, (match) => match.replace(subReg, replacer)));
}
});
return file;
}
/**
* 100000 rows * 100 columns -> 324ms
*
function replaceAll(file, pathname, aliasMap) {
const isStream = file.isStream();
const aliasList = Object.keys(aliasMap);
const aliasMatch = `(${aliasList.join('|')})`;
const reg = getRegExp(aliasMatch);
function replaceOne(match) {
return match.replace(new RegExp(aliasMatch), (m) => relative(pathname, aliasMap[m]));
}
if (isStream) {
file.contents = file.contents.pipe(replace(reg, replaceOne));
} else {
file.contents = Buffer.from(String(file.contents).replace(reg, replaceOne));
}
return file;
}
*/
function default_1(options = {}) {
const _options = Object.assign({ cwd: process.cwd(), paths: {} }, options);
const { paths } = _options;
const emptyAlias = !Object.keys(paths).length;
return through2_1.default.obj(function (file, _, cb) {
const dirname = path_1.default.dirname(file.path);
if (file.isNull() || emptyAlias) {
return cb(null, file);
}
file = replaceAll(file, dirname, paths);
cb(null, file);
});
}
exports.default = default_1;
+21
-5
{
"name": "gulp-path-alias",
"version": "1.0.2",
"version": "1.1.0",
"description": "path alias",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"test": "ava"
"test": "ava",
"build": "rimraf lib && tsc",
"release-major": "npm run build && standard-version --release-as major",
"release-minor": "npm run build && standard-version --release-as minor",
"release": "npm run build && standard-version --release-as patch"
},

@@ -37,7 +41,19 @@ "repository": {

"files": [
"index.js"
"lib/index.js",
"lib/index.d.ts",
"README.md",
"LICENSE",
"CHANGELOG.md"
],
"devDependencies": {
"@types/node": "^14.14.19",
"@types/replacestream": "^4.0.0",
"@types/through2": "^2.0.36",
"@typescript-eslint/eslint-plugin": "^4.11.1",
"@typescript-eslint/parser": "^4.11.1",
"ava": "^3.13.0",
"gulp": "^4.0.2"
"gulp": "^4.0.2",
"rimraf": "^3.0.2",
"standard-version": "^9.1.0",
"typescript": "^4.1.3"
},

@@ -44,0 +60,0 @@ "dependencies": {

+3
-3

@@ -16,4 +16,4 @@ # gulp-path-alias ![build](https://img.shields.io/badge/build-passing-green)

exports.default = () => (
gulp.src('src/*.js')
.pipe(alias({
gulp.src('src/*.js')
.pipe(alias({
paths: {

@@ -24,3 +24,3 @@ '@libs': path.resolve(__dirname, '../src/libs'),

}))
.pipe(gulp.dest('dist'))
.pipe(gulp.dest('dist'))
);

@@ -27,0 +27,0 @@ ```

const path = require('path');
const through = require('through2');
const replace = require('replacestream');
const prefixPattenMap = {
js: `import\\s*[^'"]*\\(?|from|require\\s*\\(`,
// poster: wxml
xml: `src=|url=|poster=`,
css: `@import\\s*|url\\s*\\(`
};
const suffixPatten = `\\/|['"]|\\s*\\)`;
function getRegExp(prefixPatten) {
return function (aliasName) {
return new RegExp(`(?:(${prefixPatten})\\s*['"]?\\s*)${aliasName}(${suffixPatten})`, 'gm');
}
}
function relative(from, to) {
const relativePath = path.relative(from, to);
if (!relativePath) {
return '.';
}
return !/^\./.test(relativePath) ? `./${relativePath}` : relativePath;
}
// 100000 rows * 100 columns -> 248ms
function replaceAll(file, dirname, aliasMap) {
const ext = path.extname(file.relative);
const isStream = file.isStream();
let reg;
switch (ext) {
// js
case '.js':
case '.ts':
case '.wxs':
reg = getRegExp(prefixPattenMap.js);
break;
// css
case '.css':
case '.less':
case '.scss':
case '.styl':
case '.stylus':
case '.wxss':
reg = getRegExp(prefixPattenMap.css);
break;
// xml
case '.html':
case '.wxml':
reg = getRegExp(prefixPattenMap.xml);
break;
case '.jsx':
case '.tsx':
default:
reg = getRegExp(Object.keys(prefixPattenMap).map((k) => prefixPattenMap[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`;
if (isStream) {
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(subReg, replacer)));
}
});
return file;
}
/**
* 100000 rows * 100 columns -> 324ms
*
function replaceAll(file, pathname, aliasMap) {
const isStream = file.isStream();
const aliasList = Object.keys(aliasMap);
const aliasMatch = `(${aliasList.join('|')})`;
const reg = getRegExp(aliasMatch);
function replaceOne(match) {
return match.replace(new RegExp(aliasMatch), (m) => relative(pathname, aliasMap[m]));
}
if (isStream) {
file.contents = file.contents.pipe(replace(reg, replaceOne));
} else {
file.contents = Buffer.from(String(file.contents).replace(reg, replaceOne));
}
return file;
}
*/
module.exports = function (options = {}) {
options = {
cwd: process.cwd(),
paths: {},
...options,
};
const { paths } = options;
const emptyAlias = !Object.keys(paths).length;
return through.obj(function (file, _, cb) {
const dirname = path.dirname(file.path);
if (file.isNull() || emptyAlias) {
return cb(null, file);
}
file = replaceAll(file, dirname, paths);
cb(null, file);
});
};