Skip to content

Commit

Permalink
feat: 支持ts
Browse files Browse the repository at this point in the history
  • Loading branch information
CryUshio committed Jan 4, 2021
1 parent 0a19386 commit b20688c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 34 deletions.
27 changes: 27 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"extends": ["plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"env": {
"node": true
},
"rules": {
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/triple-slash-reference": 0,
"import/prefer-default-export": 0,
"import/extensions": 0,
"import/no-dynamic-require": 0,
"prettier/prettier": 0,
"camelcase": 0,
"operator-linebreak": 0,
"prefer-promise-reject-errors": 0,
"no-use-before-define": 0,
"no-unused-vars": 1,
"no-console": 0,
"no-sync": 0,
"no-continue": 0,
"global-require": 0
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
package-lock.json
lib
71 changes: 42 additions & 29 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,76 @@
const path = require('path');
const through = require('through2');
const replace = require('replacestream');
import path from "path";
import through from "through2";
import replace from "replacestream";

type AliasMapType = Record<string, string>;
type Options = {
cwd?: string;
paths?: AliasMapType;
};
type GetRegExpReturn = (name: string) => RegExp;

const prefixPattenMap = {
js: `import\\s*[^'"]*\\(?|from|require\\s*\\(`,
// poster: wxml
xml: `src=|url=|poster=`,
css: `@import\\s*|url\\s*\\(`
css: `@import\\s*|url\\s*\\(`,
};

type PrefixPattenMap = typeof prefixPattenMap;

const suffixPatten = `\\/|['"]|\\s*\\)`;

function getRegExp(prefixPatten) {
function getRegExp(prefixPatten: string): GetRegExpReturn {
return function (aliasName) {
return new RegExp(`(?:(${prefixPatten})\\s*['"]?\\s*)${aliasName}(${suffixPatten})`, 'gm');
}
return new RegExp(`(?:(${prefixPatten})\\s*['"]?\\s*)${aliasName}(${suffixPatten})`, "gm");
};
}

function relative(from, to) {
function relative(from: string, to: string) {
const relativePath = path.relative(from, to);

if (!relativePath) {
return '.';
return ".";
}

return !/^\./.test(relativePath) ? `./${relativePath}` : relativePath;
}

// 100000 rows * 100 columns -> 248ms
function replaceAll(file, dirname, aliasMap) {
function replaceAll(file: any, dirname: string, aliasMap: AliasMapType) {
const ext = path.extname(file.relative);
const isStream = file.isStream();

let reg;
let reg: GetRegExpReturn;
switch (ext) {
// js
case '.js':
case '.ts':
case '.wxs':
case ".js":
case ".ts":
case ".wxs":
reg = getRegExp(prefixPattenMap.js);
break;
// css
case '.css':
case '.less':
case '.scss':
case '.styl':
case '.stylus':
case '.wxss':
case ".css":
case ".less":
case ".scss":
case ".styl":
case ".stylus":
case ".wxss":
reg = getRegExp(prefixPattenMap.css);
break;
// xml
case '.html':
case '.wxml':
case ".html":
case ".wxml":
reg = getRegExp(prefixPattenMap.xml);
break;
case '.jsx':
case '.tsx':
case ".jsx":
case ".tsx":
default:
reg = getRegExp(Object.keys(prefixPattenMap).map((k) => prefixPattenMap[k]).join('|'));
reg = getRegExp(
Object.keys(prefixPattenMap)
.map((k) => prefixPattenMap[k as keyof PrefixPattenMap])
.join("|")
);
break;
}

Expand Down Expand Up @@ -99,14 +112,14 @@ function replaceAll(file, pathname, aliasMap) {
}
*/

module.exports = function (options = {}) {
options = {
export default function (options: Options = {}) {
const _options: Required<Options> = {
cwd: process.cwd(),
paths: {},
...options,
};

const { paths } = options;
const { paths } = _options;
const emptyAlias = !Object.keys(paths).length;

return through.obj(function (file, _, cb) {
Expand All @@ -120,4 +133,4 @@ module.exports = function (options = {}) {

cb(null, file);
});
};
}
24 changes: 19 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "gulp-path-alias",
"version": "1.0.0",
"version": "1.0.2",
"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"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -34,11 +38,21 @@
"node": ">=8"
},
"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",
"typescript": "^4.1.3"
},
"dependencies": {
"replacestream": "^4.0.3"
Expand Down
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"allowJs": false,
"target": "ES2015",
"esModuleInterop": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"pretty": true,
"sourceMap": false,
"strict": true,
"moduleResolution": "node",
"baseUrl": ".",
"skipLibCheck": true,
"declaration": true,
"outDir": "./lib",
},
"include": ["index.ts"],
}

0 comments on commit b20688c

Please sign in to comment.