Skip to content

Commit e088b1c

Browse files
committed
use rollup to bundle
1 parent 814b309 commit e088b1c

File tree

7 files changed

+186
-6
lines changed

7 files changed

+186
-6
lines changed

dist/leopard.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* leopard v0.0.1
3+
* (c) 2018 Ryan Liu
4+
* @license WTFPL
5+
*/
6+
(function (global, factory) {
7+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
8+
typeof define === 'function' && define.amd ? define(factory) :
9+
(global.leopard = factory());
10+
}(this, (function () { 'use strict';
11+
12+
var escape = function(str) {
13+
return String(str)
14+
.replace(/&/g, '&')
15+
.replace(/</g, '&lt;')
16+
.replace(/>/g, '&gt;')
17+
.replace(/"/g, '&quot;')
18+
};
19+
20+
var escapeQuotes = function(str) {
21+
return str.replace(/"/g, '\\"')
22+
};
23+
24+
/**
25+
* parse the given `tpl` and return Function body string
26+
*
27+
* @param {String} tpl
28+
* @param {Object} data
29+
* @return {String}
30+
*/
31+
var parser = function(tpl, data) {
32+
data = data || {};
33+
var delimeterRE = /<%(.+?)%>/g;
34+
var curMatched = null;
35+
var matched = null;
36+
var body = 'var lines = [];\n' +
37+
'var rst;\n' +
38+
'with(' + JSON.stringify(data) + ') {\n';
39+
40+
/**
41+
* push a string into lines
42+
*
43+
* @param {String} str
44+
*/
45+
function push(str) {
46+
body += 'lines.push(' + str + ');\n';
47+
}
48+
49+
/**
50+
* generate Function body
51+
*
52+
* @param {String} line
53+
*/
54+
var generate = function(line) {
55+
if (line.length > 0) {
56+
var type = line.charAt(0);
57+
switch (type) {
58+
case '=':
59+
push('escape(' + line.substr(1).trim() + ')');
60+
break
61+
case '-':
62+
push(line.substr(1).trim());
63+
break
64+
default:
65+
body += line + '\n';
66+
}
67+
}
68+
};
69+
70+
while (curMatched = delimeterRE.exec(tpl)) {
71+
// This is raw HTML
72+
var html = tpl.substring(
73+
matched !== null ? matched.index + matched[0].length : 0,
74+
curMatched.index
75+
);
76+
html && push('\"' + escapeQuotes(html) + '\"');
77+
var js = curMatched[1].trim();
78+
js && generate(js);
79+
matched = curMatched;
80+
}
81+
var end = tpl.substr(matched.index + matched[0].length);
82+
end && push('\"' + escapeQuotes(end) + '\"');
83+
body += 'rst = lines.join(\"\");\n' +
84+
'}\n' +
85+
'return rst;';
86+
87+
return body
88+
};
89+
90+
/**
91+
* parse the given template and return HTML string
92+
*
93+
* @param {String} tpl
94+
* @param {Object} data
95+
* @return {String}
96+
*/
97+
var compiler = function(tpl, data) {
98+
var body = parser(tpl, data);
99+
var fun = new Function('escape', body);
100+
return fun.call(this, escape)
101+
};
102+
103+
var leo = compiler;
104+
105+
return leo;
106+
107+
})));

dist/leopard.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 40 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "a simple HTML template engine",
55
"main": "index.js",
66
"scripts": {
7+
"build": "rollup -c",
78
"test": "./node_modules/.bin/mocha",
89
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -R spec",
910
"codecov": "node ./codecov.js",
@@ -13,7 +14,10 @@
1314
"type": "git",
1415
"url": "git+https://github.com/stop2stare/leopard.git"
1516
},
16-
"keywords": ["leopard", "template"],
17+
"keywords": [
18+
"leopard",
19+
"template"
20+
],
1721
"author": "liucheng",
1822
"license": "WTFPL",
1923
"bugs": {
@@ -23,6 +27,8 @@
2327
"devDependencies": {
2428
"codecov": "^3.0.0",
2529
"istanbul": "^0.4.5",
26-
"mocha": "^5.0.0"
30+
"mocha": "^5.0.0",
31+
"rollup": "^0.55.3",
32+
"rollup-plugin-uglify": "^3.0.0"
2733
}
2834
}

rollup.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var uglify = require('rollup-plugin-uglify')
2+
var version = require('./package.json').version
3+
var banner =
4+
`/**
5+
* leopard v${version}
6+
* (c) ${new Date().getFullYear()} Ryan Liu
7+
* @license WTFPL
8+
*/`
9+
10+
export default [{
11+
input: './src/index.js',
12+
output: {
13+
file: './dist/leopard.js',
14+
format: 'umd',
15+
name: 'leopard',
16+
banner: banner
17+
}
18+
}, {
19+
input: './src/index.js',
20+
output: {
21+
file: './dist/leopard.min.js',
22+
format: 'umd',
23+
name: 'leopard',
24+
banner: banner
25+
},
26+
plugins: [uglify()]
27+
}]

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var escape = require('./utils').escape
1+
import { escape } from './utils'
22
var escapeQuotes = function(str) {
33
return str.replace(/"/g, '\\"')
44
}
@@ -84,4 +84,4 @@ var compiler = function(tpl, data) {
8484

8585
var leo = compiler
8686

87-
module.exports = leo
87+
export default leo

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports.escape = function(str) {
1+
export var escape = function(str) {
22
return String(str)
33
.replace(/&/g, '&amp;')
44
.replace(/</g, '&lt;')

0 commit comments

Comments
 (0)