Skip to content

Commit

Permalink
feat: 更新链接处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Aug 23, 2017
1 parent 5a52f8f commit cd70255
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ yarn add scrat-parser-pagelet --dev
scrat.match('{widget, page, app/view}/**.tpl', {
parser: [
scrat.plugin('pagelet', {
baseDir: path.resolve(__dirname, './app/component/'),
compress: true,
attrAlias: {
_id: '$id',
Expand All @@ -40,7 +39,7 @@ scrat.match('{widget, page, app/view}/**.tpl', {

|Name|Type|Describe|Default|
|----|----|--------|-------|
| baseDir | String | 模板的 root 目录,一般是 app/component 或者 app/components 下 | - |
| root | String | 项目根目录 | process.cwd() |
| compress | Boolean | 是否压缩模板 | false |
| attrAlias | PlainObject | 属性映射 | - |
| alias | PlainObject | 别名 | - |
Expand Down Expand Up @@ -87,7 +86,7 @@ alias: {
{% require 'index' %}
```

如果 baseDir 为 `path.resolve(__dirname, './app/component/')`,则上面的代码则会转为
上面的代码则会转为

```html
{% require 'widget/B' %}
Expand Down
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function normalizeUrl(url, options) {
}).trim();

// ignore url if url was variable, having operator or ext name
if (!quot || path.extname(url) || url.indexOf(quot) >= 0) {
if (!quot || url.indexOf(quot) >= 0) {
return _url;
}

Expand All @@ -70,20 +70,22 @@ function normalizeUrl(url, options) {
// replace alias
url = options._fullAlias[url] || url.replace(options._alias, (_, key) => alias[key]);

// handle relative path only
if (url.charAt(0) === '.' && !path.extname(url)) {
url = path.resolve(path.dirname(file.realpath), url)
.replace(options.baseDir, '');
} else if (path.isAbsolute(url)) {
url = path.relative(options.baseDir, url);
if (url.charAt(0) === '.') {
url = path.resolve(path.dirname(file.realpath), url);
}

if (path.sep !== '/') {
url = url.replace(/\\/g, '/');
const ext = path.extname(url);
if (path.isAbsolute(url)) {
url = path.relative(
ext ? options.appDir : path.resolve(options.appDir, './component'),
url
);
} else if (ext) {
return _url;
}

if (url.charAt(0) === '/') {
url = url.substring(1);
if (path.sep !== '/') {
url = url.replace(/\\/g, '/');
}

return quot + url + quot;
Expand All @@ -93,6 +95,7 @@ module.exports = function(content, file, options) {
options.file = file;
options.alias = options.alias || {};
options.attrAlias = options.attrAlias || {};
options.appDir = path.resolve(options.root || process.cwd(), './app');

const tree = utils.parseTemplate(content, {
blockStart: options.blockStart,
Expand Down
16 changes: 7 additions & 9 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ const template = `
</div>
<div class="right-side">
{% require "./w-rank-list" %}
{% require $id="./w-rank-list" %}
{{ abc }}
</div>
{% require __pageUrl__ %}
{% require "./lib.js" %}
{% require _id="./lib.js" %}
{% require "app/lib.js" %}
{% require "./" + "lib.js" %}
{% if aaa %}
abc
Expand All @@ -48,7 +49,6 @@ const template = `
describe('parse', () => {
it('should parse without error', () => {
const options = {
baseDir: path.join(__dirname, '../app/component/'),
compress: true,
attrAlias: {
_id: '$id',
Expand All @@ -64,15 +64,15 @@ describe('parse', () => {
realpath: path.join(__dirname, '../app/component/page/test/test.tpl')
}, options);

options.baseDir = path.join(__dirname, '../app/component');
const nstring2 = pageletParser(template, {
realpath: path.join(__dirname, '../app/component/page/test/test.tpl')
}, options);

assert(nstring.match(/\n/g).length === 1);
assert(nstring.indexOf('\nvar a = 123;') >= 0);
assert(nstring.indexOf('require "./" + "lib.js"') >= 0);
assert(nstring.indexOf('"./lib.js"') >= 0);
assert(nstring.indexOf('"component/page/test/lib.js"') >= 0);
assert(nstring.indexOf('{% require "app/lib.js" %}') >= 0);
assert(nstring.indexOf('require __pageUrl__') >= 0);
assert(nstring.indexOf('"page/test/w-game-list"') >= 0);
assert(nstring.indexOf('"p/widget/navigation"') >= 0);
Expand All @@ -89,9 +89,9 @@ describe('parse', () => {
{% require "./w-rank-list" %}
{% endpagelet %}
`, {
realpath: path.join(__dirname, '../app/component/page/test/test.tpl')
realpath: path.join(__dirname, './ref/app/component/page/test/test.tpl')
}, {
baseDir: path.join(__dirname, '../app/component'),
root: path.join(__dirname, './ref/'),
split: ',',
attrAlias: {
_id: '$id',
Expand All @@ -112,7 +112,6 @@ describe('parse', () => {
`, {
realpath: path.join(__dirname, '../app/component/page/test/test.tpl')
}, {
baseDir: path.join(__dirname, '../app/component'),
split: ',',
attrAlias: {
_id: '$id',
Expand All @@ -133,7 +132,6 @@ describe('parse', () => {
pageletParser('{% css %}{% endcss %}', {
realpath: path.join(__dirname, '../app/component/page/test/test.tpl')
}, {
baseDir: path.join(__dirname, '../app/component/'),
processor: {
css() {
done();
Expand Down

0 comments on commit cd70255

Please sign in to comment.