Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed Apr 25, 2017
2 parents c56e1f6 + 4b568ca commit 6d1b329
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v4.4.3

1. 支持对 `options.imports` 的深拷贝 [#1](https://github.com/aui/express-art-template/issues/1)

## v4.4.2

1. 兼容 IE8
Expand Down
43 changes: 27 additions & 16 deletions lib/template-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! [email protected].2 | https://github.com/aui/art-template */
/*! [email protected].3 | https://github.com/aui/art-template */
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
Expand Down Expand Up @@ -235,6 +235,31 @@ var nativeRule = __webpack_require__(13);
var htmlMinifier = __webpack_require__(7);
var resolveFilename = __webpack_require__(11);

var toString = Object.prototype.toString;
var isObject = function isObject(value) {
return value !== null && toString.call(value).slice(8, -1) === 'Object';
};

/**
* 继承默认配置
* @param {Object} options
* @return {Object}
*/
function extend(options) {
var copy = Object.create(this);

for (var name in options) {
var value = options[name];
if (isObject(value)) {
copy[name] = extend.call(copy[name], value);
} else {
copy[name] = value;
}
}

return copy;
};

/** 模板编译器默认配置 */
var defaults = {

Expand Down Expand Up @@ -296,21 +321,7 @@ var defaults = {

};

/**
* 继承默认配置
* @param {Object} options
* @return {Object}
*/
defaults.$extend = function (options) {
var copy = Object.create(this);

for (var name in options) {
copy[name] = options[name];
}

return copy;
};

defaults.$extend = extend;
module.exports = defaults;

/***/ }),
Expand Down
4 changes: 2 additions & 2 deletions lib/template-web.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "art-template",
"description": "JavaScript Template Engine",
"homepage": "http://aui.github.com/art-template/",
"version": "4.4.2",
"version": "4.4.3",
"keywords": [
"template"
],
Expand Down
44 changes: 28 additions & 16 deletions src/compile/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ const htmlMinifier = require('./adapter/html-minifier');
const resolveFilename = require('./adapter/resolve-filename');


const toString = Object.prototype.toString;
const isObject = value => {
return value !== null && toString.call(value).slice(8, -1) === 'Object';
};


/**
* 继承默认配置
* @param {Object} options
* @return {Object}
*/
function extend(options) {
const copy = Object.create(this);

for (let name in options) {
let value = options[name];
if (isObject(value)) {
copy[name] = extend.call(copy[name], value);
} else {
copy[name] = value;
}
}

return copy;
};


/** 模板编译器默认配置 */
const defaults = {

Expand Down Expand Up @@ -73,20 +100,5 @@ const defaults = {
};


/**
* 继承默认配置
* @param {Object} options
* @return {Object}
*/
defaults.$extend = function(options) {
const copy = Object.create(this);

for (let name in options) {
copy[name] = options[name]
}

return copy;
};


defaults.$extend = extend;
module.exports = defaults;
36 changes: 36 additions & 0 deletions test/compile/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,41 @@ module.exports = {

before: () => {
console.log('#compile/defaults');
},

'defaults': {
'imports': () => {
const imports = defaults.imports;
const options = defaults.$extend({
imports: {
console
}
});

assert.deepEqual(console, options.imports.console);
assert.deepEqual(imports.$escape, options.imports.$escape);
assert.deepEqual(undefined, imports.console);
},

'rules': () => {
// const rules = defaults.rules;
// const length = rules.length;
// const options = defaults.$extend({
// rules: [null]
// });

// assert.deepEqual(null, options.rules[2]);
// assert.deepEqual(rules[0], options.rules[0]);
// assert.deepEqual(length, rules.length);

const rules = defaults.rules;
const length = rules.length;
const options = defaults.$extend({
rules: []
});

assert.deepEqual(0, options.rules.length);
assert.deepEqual(length, rules.length);
}
}
};

0 comments on commit 6d1b329

Please sign in to comment.