Skip to content

Commit

Permalink
fix: #2
Browse files Browse the repository at this point in the history
  • Loading branch information
CryUshio committed Jan 8, 2021
1 parent 08619fb commit 592cfed
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
package-lock.json
lib
dist
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "path";
import through from "through2";
import replace from "replacestream";
import slash from "slash";

type AliasMapType = Record<string, string>;
type Options = {
Expand All @@ -27,7 +28,7 @@ function getRegExp(prefixPatten: string): GetRegExpReturn {
}

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

if (!relativePath) {
return ".";
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
"LICENSE",
"CHANGELOG.md"
],
"husky": {
"hooks": {
"pre-commit": "npm run test"
}
},
"devDependencies": {
"@types/node": "^14.14.19",
"@types/replacestream": "^4.0.0",
Expand All @@ -52,11 +57,13 @@
"@typescript-eslint/parser": "^4.11.1",
"ava": "^3.13.0",
"gulp": "^4.0.2",
"husky": "^4.3.7",
"rimraf": "^3.0.2",
"standard-version": "^9.1.0",
"typescript": "^4.1.3"
},
"dependencies": {
"replacestream": "^4.0.3"
"replacestream": "^4.0.3",
"slash": "^3.0.0"
}
}
1 change: 1 addition & 0 deletions test-files/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ import('@libs-b/foo');
import('src/@libs');
import('@lib/foo');
require('@utils/b');
const b = require('@utils/b');
1 change: 1 addition & 0 deletions test-files/app.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

<audio poster="@utils/img/bg.png"></audio>
<live-pusher url="@utils/pusher"></live-pusher>
<a href="@utils/www"></a>
18 changes: 18 additions & 0 deletions test-files/expected/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@import "libs";
@import "./libs";
@import "./libs/var";
@import "@libs-b/var";
@import "@lib/var";
@import "src/@libs/var";
@import"./libs/var";

.bg {
background-image: url(libs);
background-image: url(./libs);
background-image: url(./libs/img/bg.png);
background-image: url(@libs-b/var);
background-image: url(src/@libs/var);
background-image: url( ./utils/img/bg.png );
background-image: url( './utils/img/bg.png' );
background-image: url( ' ./utils/img/bg.png ' );
}
16 changes: 16 additions & 0 deletions test-files/expected/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import foo from 'libs';
import foo from './libs';
import foo from './libs/foo';
import foo from '@libs-b/foo';
import foo from 'src/@libs';
import foo from '@lib/foo';
import{a}from'./libs/a';

import('libs');
import('./libs');
import('./libs/foo');
import('@libs-b/foo');
import('src/@libs');
import('@lib/foo');
require('./utils/b');
const b = require('./utils/b');
10 changes: 10 additions & 0 deletions test-files/expected/app.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<image src="libs"/>
<image src="./libs"/>
<image src="./libs/img/bg.png"/>
<image src="@libs-b/img/bg.png"/>
<image src="src/@libs/img/bg.png"/>
<image src="@lib/img/bg.png"/>

<audio poster="./utils/img/bg.png"></audio>
<live-pusher url="./utils/pusher"></live-pusher>
<a href="./utils/www"></a>
108 changes: 91 additions & 17 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,94 @@
const path = require('path');
const fs = require('fs');
const readline = require('readline');
const gulp = require('gulp');
const through = require('through2');
const test = require('ava');
const alias = require('./index');

// TODO
test('functional test .css', async (t) => {
gulp.src('./test-files/app.css')
.pipe(alias({
path: {
'@libs': './libs',
'@utils': './utils',
}
}))
.pipe(through.obj(function (file, _, cb) {
console.log(file.isStream());
}));

t.is('', '');
const { dest } = require('gulp');
const alias = require('./lib');

const fileValidator = async (filepath, expectedFilepath, t) => {
const fileArray = [];
const expectFileArray = [];

const readFileLine = (f, arrayBuffer) => {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: fs.createReadStream(f),
});
rl.on('line', (line) => {
arrayBuffer.push(line);
});
rl.on('close', resolve);
})
}

await Promise.all([
readFileLine(filepath, fileArray),
readFileLine(expectedFilepath, expectFileArray),
]);

for(let i = 0; i < expectFileArray.length; i++) {
const expectLine = expectFileArray[i];
const fileLine = fileArray[i];

if (expectLine !== fileLine) {
t.fail(`Validate failed at line ${i}`);
}
}
t.pass();
}

const BASE_PATH = path.join(__dirname, './test-files');
const paths = {
'@libs': path.join(BASE_PATH, 'libs'),
'@utils': path.join(BASE_PATH, 'utils'),
}


test('functional test .css', (t) => {
const filepath = path.join(BASE_PATH, 'app.css');
const distFilepath = path.join(BASE_PATH, 'dist/app.css');
const expectFilepath = path.join(BASE_PATH, 'expected/app.css');

return new Promise((resolve) => {
gulp.src(filepath)
.pipe(alias({ paths }))
.pipe(dest(path.join(BASE_PATH, 'dist')))
.on('end', async () => {
await fileValidator(distFilepath, expectFilepath, t);
resolve();
});
});
});

test('functional test .js', (t) => {
const filepath = path.join(BASE_PATH, 'app.js');
const distFilepath = path.join(BASE_PATH, 'dist/app.js');
const expectFilepath = path.join(BASE_PATH, 'expected/app.js');

return new Promise((resolve) => {
gulp.src(filepath)
.pipe(alias({ paths }))
.pipe(dest(path.join(BASE_PATH, 'dist')))
.on('end', async () => {
await fileValidator(distFilepath, expectFilepath, t);
resolve();
});
});
});

test('functional test .wxml', (t) => {
const filepath = path.join(BASE_PATH, 'app.wxml');
const distFilepath = path.join(BASE_PATH, 'dist/app.wxml');
const expectFilepath = path.join(BASE_PATH, 'expected/app.wxml');

return new Promise((resolve) => {
gulp.src(filepath)
.pipe(alias({ paths }))
.pipe(dest(path.join(BASE_PATH, 'dist')))
.on('end', async () => {
await fileValidator(distFilepath, expectFilepath, t);
resolve();
});
});
});

0 comments on commit 592cfed

Please sign in to comment.