Skip to content

Commit 36f506b

Browse files
committed
add module wrapper
1 parent 7ca52d5 commit 36f506b

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
lines changed

.jshintrc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
"maxlen": 120,
55
"undef": true,
66
"unused": true,
7-
"node": true
7+
"node": true,
8+
"browser": true,
9+
"predef": [
10+
"global",
11+
"define",
12+
"modules"
13+
]
814
}

index.js renamed to lib/posthtml-render.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
function render(tree, options) {
1+
(function(global) {
2+
3+
function postHTMLRender(tree, options) {
24
options = options || {};
35

46
/**
@@ -87,4 +89,31 @@ function traverse(tree, cb) {
8789
return tree;
8890
}
8991

90-
module.exports = render;
92+
var defineAsGlobal = true;
93+
94+
/* istanbul ignore next */
95+
if (typeof module === 'object' && typeof module.exports === 'object') {
96+
module.exports = postHTMLRender;
97+
defineAsGlobal = false;
98+
}
99+
100+
/* istanbul ignore next */
101+
if (typeof modules === 'object' && typeof modules.define === 'function') {
102+
modules.define('postHTMLRender', function(provide) {
103+
provide(postHTMLRender);
104+
});
105+
defineAsGlobal = false;
106+
}
107+
108+
/* istanbul ignore next */
109+
if (typeof define === 'function') {
110+
define(function(require, exports, module) {
111+
module.exports = postHTMLRender;
112+
});
113+
defineAsGlobal = false;
114+
}
115+
/* istanbul ignore next */
116+
defineAsGlobal && (global.postHTMLRender = postHTMLRender);
117+
118+
/* istanbul ignore next */
119+
})(typeof window !== 'undefined'? window: global);

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"name": "posthtml-render",
33
"version": "0.0.0",
44
"description": "Render PostHTMLTree to HTML/XML",
5-
"main": "index.js",
5+
"main": "lib/posthtml-render.js",
66
"scripts": {
7-
"test": "npm run lint && npm run coverage",
8-
"lint": "jshint . && jscs . -v",
9-
"coverage": "istanbul cover --report text --report html --report lcov node_modules/mocha/bin/_mocha"
7+
"test": "npm run build && npm run lint && npm run coverage",
8+
"lint": "jshint lib/ && jscs lib/ -v",
9+
"coverage": "istanbul cover --report text --report html --report lcov node_modules/mocha/bin/_mocha",
10+
"build": "uglifyjs lib/posthtml-render.js -c \"evaluate=false\" -o posthtml-render.min.js"
1011
},
1112
"repository": {
1213
"type": "git",
@@ -30,6 +31,7 @@
3031
"istanbul": "^0.4.0",
3132
"jscs": "^2.3.4",
3233
"jshint": "^2.8.0",
33-
"mocha": "^2.3.3"
34+
"mocha": "^2.3.3",
35+
"uglifyjs": "^2.4.10"
3436
}
3537
}

posthtml-render.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.

test/test.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
<script type="text/javascript" src="../posthtml-render.min.js"></script>
6+
<script type="text/javascript">
7+
window.onload = function() {
8+
document.body.innerHTML = postHTMLRender({ tag: 'h1', attrs: { id: 'header', style: 'color:red;', 'data-id': 'header' }, content: 'test' });
9+
};
10+
</script>
11+
</head>
12+
<body>
13+
14+
</body>
15+
</html>

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var render = require('..');
1+
var render = require('../lib/posthtml-render.js');
22
var describe = require('mocha').describe;
33
var it = require('mocha').it;
44
var expect = require('chai').expect;

0 commit comments

Comments
 (0)