Skip to content

Commit e10a69b

Browse files
committed
Drop some more bytes. Switch to microbundle and eslint-config-developit.
1 parent 46e72e9 commit e10a69b

File tree

8 files changed

+94
-152
lines changed

8 files changed

+94
-152
lines changed

.babelrc

Lines changed: 0 additions & 10 deletions
This file was deleted.

.eslintrc

Lines changed: 0 additions & 55 deletions
This file was deleted.

package.json

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
"module": "dist/index.mjs",
99
"jsnext:main": "dist/index.mjs",
1010
"scripts": {
11-
"x-build": "microbundle src/index.js src/jsx.js --target web --external none --no-compress && npm run -s copy-typescript-definition",
1211
"build": "npm run -s transpile && npm run -s transpile:jsx && npm run -s copy-typescript-definition",
1312
"transpile": "microbundle src/index.js -f es,umd --target web --external none",
1413
"transpile:jsx": "microbundle src/jsx.js -o dist/jsx.js --target web --external none",
1514
"copy-typescript-definition": "copyfiles -f src/index.d.ts dist",
16-
"x-transpile": "rollup -c rollup.config.js -m ${npm_package_main}.map -f umd -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_main",
17-
"x-transpile:jsx": "ENTRY=jsx rollup -c rollup.config.js -m dist/jsx.js.map -f umd -n $npm_package_amdName src/jsx.js -o dist/jsx.js",
1815
"test": "eslint src test && mocha --compilers js:babel-register test/**/*.js",
1916
"prepublish": "npm run build",
2017
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
@@ -25,44 +22,55 @@
2522
"universal",
2623
"isomorphic"
2724
],
25+
"eslintConfig": {
26+
"extends": "developit",
27+
"rules": {
28+
"react/prefer-stateless-function": 0,
29+
"react/jsx-no-bind": 0,
30+
"react/no-danger": 0,
31+
"jest/valid-expect": 0,
32+
"new-cap": 0
33+
}
34+
},
35+
"babel": {
36+
"presets": [
37+
"env"
38+
],
39+
"plugins": [
40+
[
41+
"transform-react-jsx",
42+
{
43+
"pragma": "h"
44+
}
45+
],
46+
"transform-object-rest-spread"
47+
]
48+
},
2849
"author": "Jason Miller <[email protected]>",
2950
"license": "MIT",
3051
"typings": "src/index.d.ts",
31-
"repository": {
32-
"type": "git",
33-
"url": "https://github.com/developit/preact-render-to-string.git"
34-
},
35-
"bugs": {
36-
"url": "https://github.com/developit/preact-render-to-string/issues"
37-
},
52+
"repository": "developit/preact-render-to-string",
53+
"bugs": "https://github.com/developit/preact-render-to-string/issues",
3854
"homepage": "https://github.com/developit/preact-render-to-string",
3955
"peerDependencies": {
4056
"preact": "*"
4157
},
4258
"devDependencies": {
43-
"babel-cli": "^6.11.4",
44-
"babel-core": "^6.11.4",
45-
"babel-eslint": "^6.1.2",
46-
"babel-plugin-transform-class-properties": "^6.10.2",
47-
"babel-plugin-transform-react-jsx": "^6.8.0",
48-
"babel-preset-es2015": "^6.9.0",
49-
"babel-preset-stage-0": "^6.5.0",
50-
"babel-register": "^6.9.0",
59+
"babel-plugin-transform-object-rest-spread": "^6.26.0",
60+
"babel-plugin-transform-react-jsx": "^6.24.1",
61+
"babel-preset-env": "^1.7.0",
62+
"babel-register": "^6.26.0",
5163
"chai": "^3.5.0",
5264
"copyfiles": "^1.2.0",
53-
"eslint": "^3.2.2",
54-
"mocha": "^3.0.0",
65+
"eslint": "^4.19.1",
66+
"eslint-config-developit": "^1.1.1",
67+
"microbundle": "^0.6.0",
68+
"mocha": "^5.2.0",
5569
"preact": "^8.1.0",
56-
"rollup": "^0.34.3",
57-
"rollup-plugin-babel": "^2.6.1",
58-
"rollup-plugin-commonjs": "^3.3.1",
59-
"rollup-plugin-memory": "^1.0.0",
60-
"rollup-plugin-node-resolve": "^2.0.0",
6170
"sinon": "^1.17.5",
6271
"sinon-chai": "^2.8.0"
6372
},
6473
"dependencies": {
65-
"microbundle": "^0.6.0",
6674
"pretty-format": "^3.5.1"
6775
}
6876
}

src/index.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const SHALLOW = { shallow: true };
55
// components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names.
66
const UNNAMED = [];
77

8-
const EMPTY = {};
9-
108
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
119

1210

@@ -34,19 +32,20 @@ let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);
3432

3533

3634
/** The default export is an alias of `render()`. */
37-
export default function renderToString(vnode, context, opts, inner, isSvgMode) {
38-
let { nodeName, attributes, children } = vnode || EMPTY,
35+
function renderToString(vnode, context, opts, inner, isSvgMode) {
36+
if (vnode==null || typeof vnode==='boolean') {
37+
return '';
38+
}
39+
40+
let nodeName = vnode.nodeName,
41+
attributes = vnode.attributes,
3942
isComponent = false;
4043
context = context || {};
4144
opts = opts || {};
4245

4346
let pretty = opts.pretty,
4447
indentChar = typeof pretty==='string' ? pretty : '\t';
4548

46-
if (vnode==null || typeof vnode==='boolean') {
47-
return '';
48-
}
49-
5049
// #text nodes
5150
if (typeof vnode!=='object' && !nodeName) {
5251
return encodeEntities(vnode);
@@ -99,16 +98,16 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {
9998
v = attributes[name];
10099
if (name==='children') continue;
101100

102-
if (name.match(/[\s\n\/='"\0<>]/)) continue;
101+
if (name.match(/[\s\n\\/='"\0<>]/)) continue;
103102

104103
if (!(opts && opts.allAttributes) && (name==='key' || name==='ref')) continue;
105104

106105
if (name==='className') {
107-
if (attributes['class']) continue;
106+
if (attributes.class) continue;
108107
name = 'class';
109108
}
110-
else if (isSvgMode && name.match(/^xlink\:?./)) {
111-
name = name.toLowerCase().replace(/^xlink\:?/, 'xlink:');
109+
else if (isSvgMode && name.match(/^xlink:?./)) {
110+
name = name.toLowerCase().replace(/^xlink:?/, 'xlink:');
112111
}
113112

114113
if (name==='style' && v && typeof v==='object') {
@@ -144,24 +143,23 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {
144143
else if (pretty && ~s.indexOf('\n')) s += '\n';
145144

146145
s = `<${nodeName}${s}>`;
147-
if (nodeName.match(/[\s\n\/='"\0<>]/)) throw s;
146+
if (nodeName.match(/[\s\n\\/='"\0<>]/)) throw s;
148147

149148
let isVoid = nodeName.match(VOID_ELEMENTS);
150149
if (isVoid) s = s.replace(/>$/, ' />');
151150

151+
let pieces = [];
152152
if (html) {
153153
// if multiline, indent.
154154
if (pretty && isLargeString(html)) {
155155
html = '\n' + indentChar + indent(html, indentChar);
156156
}
157157
s += html;
158158
}
159-
else {
160-
let len = children && children.length,
161-
pieces = [],
162-
hasLarge = ~s.indexOf('\n');
163-
for (let i=0; i<len; i++) {
164-
let child = children[i];
159+
else if (vnode.children) {
160+
let hasLarge = ~s.indexOf('\n');
161+
for (let i=0; i<vnode.children.length; i++) {
162+
let child = vnode.children[i];
165163
if (child!=null && child!==false) {
166164
let childSvgMode = nodeName==='svg' ? true : nodeName==='foreignObject' ? false : isSvgMode,
167165
ret = renderToString(child, context, opts, true, childSvgMode);
@@ -174,12 +172,13 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {
174172
pieces[i] = '\n' + indentChar + indent(pieces[i], indentChar);
175173
}
176174
}
177-
if (pieces.length) {
178-
s += pieces.join('');
179-
}
180-
else if (opts && opts.xml) {
181-
return s.substring(0, s.length-1) + ' />';
182-
}
175+
}
176+
177+
if (pieces.length) {
178+
s += pieces.join('');
179+
}
180+
else if (opts && opts.xml) {
181+
return s.substring(0, s.length-1) + ' />';
183182
}
184183

185184
if (!isVoid) {
@@ -216,6 +215,7 @@ function getFallbackComponentName(component) {
216215
}
217216
renderToString.shallowRender = shallowRender;
218217

218+
export default renderToString;
219219

220220
export {
221221
renderToString as render,

test/jsx.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import render from '../src/jsx';
22
import { h, Component } from 'preact';
33
import chai, { expect } from 'chai';
4-
import { spy, match } from 'sinon';
54
import sinonChai from 'sinon-chai';
65
chai.use(sinonChai);
76

@@ -62,7 +61,7 @@ describe('jsx', () => {
6261
function F(){}
6362
expect(renderJsx(
6463
<F b={false}>bar</F>,
65-
{ shallow:true, renderRootComponent:false }
64+
{ shallow: true, renderRootComponent: false }
6665
)).to.equal(dedent`
6766
<F b={false}>bar</F>
6867
`);
@@ -108,7 +107,8 @@ describe('jsx', () => {
108107
<a b={[
109108
<c />,
110109
<d f="g" />
111-
]}>bar</a>
110+
]}
111+
>bar</a>
112112
)).to.equal(dedent`
113113
<a
114114
b={
@@ -148,7 +148,7 @@ describe('jsx', () => {
148148
it('should skip null siblings', () => {
149149
expect(renderJsx(
150150
<jsx>
151-
<span/>
151+
<span />
152152
{null}
153153
</jsx>
154154
)).to.deep.equal(dedent`
@@ -161,19 +161,19 @@ describe('jsx', () => {
161161
it('should skip functions if functions=false', () => {
162162
expect(renderJsx(
163163
<div onClick={() => {}} />,
164-
{ functions:false }
164+
{ functions: false }
165165
)).to.equal('<div></div>');
166166
});
167167

168168
it('should skip function names if functionNames=false', () => {
169169
expect(renderJsx(
170170
<div onClick={() => {}} />,
171-
{ functionNames:false }
171+
{ functionNames: false }
172172
)).to.equal('<div onClick={Function}></div>');
173173

174174
expect(renderJsx(
175175
<div onClick={function foo(){}} />,
176-
{ functionNames:false }
176+
{ functionNames: false }
177177
)).to.equal('<div onClick={Function}></div>');
178178
});
179179

test/pretty.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { render, shallowRender } from '../src';
2-
import { h, Component } from 'preact';
1+
import { render } from '../src';
2+
import { h } from 'preact';
33
import chai, { expect } from 'chai';
4-
import { spy, match } from 'sinon';
54
import sinonChai from 'sinon-chai';
65
chai.use(sinonChai);
76

87
describe('pretty', () => {
9-
let prettyRender = jsx => render(jsx, {}, { pretty:true });
8+
let prettyRender = jsx => render(jsx, {}, { pretty: true });
109

1110
it('should render no whitespace by default', () => {
1211
let rendered = render(

0 commit comments

Comments
 (0)