Skip to content

Commit 0574085

Browse files
committed
cut the library size in half
1 parent 8ac9a63 commit 0574085

File tree

5 files changed

+28
-50
lines changed

5 files changed

+28
-50
lines changed

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
"version": "3.7.0",
55
"description": "Render JSX to an HTML string, with support for Preact components.",
66
"main": "dist/index.js",
7-
"jsnext:main": "src/index.js",
7+
"umd:main": "dist/index.js",
8+
"module": "dist/index.mjs",
9+
"jsnext:main": "dist/index.mjs",
810
"scripts": {
11+
"x-build": "microbundle src/index.js src/jsx.js --target web --external none --no-compress && npm run -s copy-typescript-definition",
912
"build": "npm run -s transpile && npm run -s transpile:jsx && npm run -s copy-typescript-definition",
13+
"transpile": "microbundle src/index.js -f es,umd --target web --external none",
14+
"transpile:jsx": "microbundle src/jsx.js -o dist/jsx.js --target web --external none",
1015
"copy-typescript-definition": "copyfiles -f src/index.d.ts dist",
11-
"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",
12-
"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",
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",
1318
"test": "eslint src test && mocha --compilers js:babel-register test/**/*.js",
1419
"prepublish": "npm run build",
1520
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
@@ -57,6 +62,7 @@
5762
"sinon-chai": "^2.8.0"
5863
},
5964
"dependencies": {
65+
"microbundle": "^0.6.0",
6066
"pretty-format": "^3.5.1"
6167
}
6268
}

src/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { objectKeys, encodeEntities, falsey, memoize, indent, isLargeString, styleObjToCss, hashToClassName, assign, getNodeProps } from './util';
1+
import { objectKeys, encodeEntities, indent, isLargeString, styleObjToCss, assign, getNodeProps } from './util';
22

33
const SHALLOW = { shallow: true };
44

@@ -119,14 +119,11 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {
119119
if (attributes['class']) continue;
120120
name = 'class';
121121
}
122-
else if (isSvgMode && name.match(/^xlink\:?(.+)/)) {
123-
name = name.toLowerCase().replace(/^xlink\:?(.+)/, 'xlink:$1');
122+
else if (isSvgMode && name.match(/^xlink\:?./)) {
123+
name = name.toLowerCase().replace(/^xlink\:?/, 'xlink:');
124124
}
125125

126-
if (name==='class' && v && typeof v==='object') {
127-
v = hashToClassName(v);
128-
}
129-
else if (name==='style' && v && typeof v==='object') {
126+
if (name==='style' && v && typeof v==='object') {
130127
v = styleObjToCss(v);
131128
}
132129

@@ -177,7 +174,7 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {
177174
hasLarge = ~s.indexOf('\n');
178175
for (let i=0; i<len; i++) {
179176
let child = children[i];
180-
if (!falsey(child)) {
177+
if (child!=null && child!==false) {
181178
let childSvgMode = nodeName==='svg' ? true : nodeName==='foreignObject' ? false : isSvgMode,
182179
ret = renderToString(child, context, opts, true, childSvgMode);
183180
if (!hasLarge && pretty && isLargeString(ret)) hasLarge = true;

src/jsx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import './polyfills';
2-
import renderToString from '.';
2+
import renderToString from './index';
33
import { indent, encodeEntities, assign } from './util';
44
import prettyFormat from 'pretty-format';
55

src/util.js

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,36 @@
11
// DOM properties that should NOT have "px" added when numeric
2-
export const NON_DIMENSION_PROPS = {
3-
boxFlex:1, boxFlexGroup:1, columnCount:1, fillOpacity:1, flex:1, flexGrow:1,
4-
flexPositive:1, flexShrink:1, flexNegative:1, fontWeight:1, lineClamp:1, lineHeight:1,
5-
opacity:1, order:1, orphans:1, strokeOpacity:1, widows:1, zIndex:1, zoom:1
6-
};
7-
8-
const ESC = {
9-
'<': '&lt;',
10-
'>': '&gt;',
11-
'"': '&quot;',
12-
'&': '&amp;'
13-
};
2+
export const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i;
143

154
export const objectKeys = Object.keys || (obj => {
165
let keys = [];
176
for (let i in obj) if (obj.hasOwnProperty(i)) keys.push(i);
187
return keys;
198
});
209

21-
export let encodeEntities = s => String(s).replace(/[<>"&]/g, escapeChar);
22-
23-
let escapeChar = a => ESC[a] || a;
24-
25-
export let falsey = v => v==null || v===false;
26-
27-
export let memoize = (fn, mem={}) => v => mem[v] || (mem[v] = fn(v));
10+
export let encodeEntities = s => String(s)
11+
.replace(/</g, '&lt;')
12+
.replace(/>/g, '&gt;')
13+
.replace(/"/g, '&quot;')
14+
.replace(/&/g, '&amp;');
2815

2916
export let indent = (s, char) => String(s).replace(/(\n+)/g, '$1' + (char || '\t'));
3017

3118
export let isLargeString = (s, length, ignoreLines) => (String(s).length>(length || 40) || (!ignoreLines && String(s).indexOf('\n')!==-1) || String(s).indexOf('<')!==-1);
3219

20+
const JS_TO_CSS = {};
21+
3322
// Convert an Object style to a CSSText string
3423
export function styleObjToCss(s) {
3524
let str = '';
3625
for (let prop in s) {
3726
let val = s[prop];
3827
if (val!=null) {
3928
if (str) str += ' ';
40-
str += jsToCss(prop);
29+
// str += jsToCss(prop);
30+
str += JS_TO_CSS[prop] || (JS_TO_CSS[prop] = prop.replace(/([A-Z])/g,'-$1').toLowerCase());
4131
str += ': ';
4232
str += val;
43-
if (typeof val==='number' && !NON_DIMENSION_PROPS[prop]) {
33+
if (typeof val==='number' && IS_NON_DIMENSIONAL.test(prop)===false) {
4434
str += 'px';
4535
}
4636
str += ';';
@@ -49,22 +39,6 @@ export function styleObjToCss(s) {
4939
return str || undefined;
5040
}
5141

52-
53-
// See https://github.com/developit/preact/blob/master/src/util.js#L61
54-
export function hashToClassName(c) {
55-
let str = '';
56-
for (let prop in c) {
57-
if (c[prop]) {
58-
if (str) str += ' ';
59-
str += prop;
60-
}
61-
}
62-
return str;
63-
}
64-
65-
// Convert a JavaScript camel-case CSS property name to a CSS property name
66-
export let jsToCss = memoize( s => s.replace(/([A-Z])/g,'-$1').toLowerCase() );
67-
6842
export function assign(obj, props) {
6943
for (let i in props) obj[i] = props[i];
7044
return obj;

test/jsx.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import render from '../src/jsx';
1+
// import render from '../src/jsx';
2+
import render from '../dist/jsx';
23
import { h, Component } from 'preact';
34
import chai, { expect } from 'chai';
45
import { spy, match } from 'sinon';

0 commit comments

Comments
 (0)