Skip to content

Commit 70f28b1

Browse files
committed
Conditionally compile out support for { pretty: true } from the default export. Pretty (whitespace) output is now only supported in /jsx.
1 parent 116f8e7 commit 70f28b1

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

env.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ENABLE_PRETTY = true;

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"jsnext:main": "dist/index.mjs",
1010
"scripts": {
1111
"build": "npm run -s transpile && npm run -s transpile:jsx && npm run -s copy-typescript-definition",
12-
"transpile": "microbundle src/index.js -f es,umd --target web --external none",
13-
"transpile:jsx": "microbundle src/jsx.js -o dist/jsx.js --target web --external none",
12+
"transpile": "echo 'export const ENABLE_PRETTY = false;'>env.js && microbundle src/index.js -f es,umd --target web --external none",
13+
"transpile:jsx": "echo 'export const ENABLE_PRETTY = true;'>env.js && microbundle src/jsx.js -o dist/jsx.js --target web --external none",
1414
"copy-typescript-definition": "copyfiles -f src/index.d.ts dist",
1515
"test": "eslint src test && mocha --compilers js:babel-register test/**/*.js",
1616
"prepublish": "npm run build",

src/index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { encodeEntities, indent, isLargeString, styleObjToCss, assign, getNodeProps } from './util';
2+
import { ENABLE_PRETTY } from '../env';
23

34
const SHALLOW = { shallow: true };
45

@@ -43,8 +44,8 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
4344
context = context || {};
4445
opts = opts || {};
4546

46-
let pretty = opts.pretty,
47-
indentChar = typeof pretty==='string' ? pretty : '\t';
47+
let pretty = ENABLE_PRETTY && opts.pretty,
48+
indentChar = pretty && typeof pretty==='string' ? pretty : '\t';
4849

4950
// #text nodes
5051
if (typeof vnode!=='object' && !nodeName) {
@@ -138,9 +139,11 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
138139
}
139140

140141
// account for >1 multiline attribute
141-
let sub = s.replace(/^\n\s*/, ' ');
142-
if (sub!==s && !~sub.indexOf('\n')) s = sub;
143-
else if (pretty && ~s.indexOf('\n')) s += '\n';
142+
if (pretty) {
143+
let sub = s.replace(/^\n\s*/, ' ');
144+
if (sub!==s && !~sub.indexOf('\n')) s = sub;
145+
else if (pretty && ~s.indexOf('\n')) s += '\n';
146+
}
144147

145148
s = `<${nodeName}${s}>`;
146149
if (String(nodeName).match(/[\s\n\\/='"\0<>]/)) throw s;
@@ -157,13 +160,13 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
157160
s += html;
158161
}
159162
else if (vnode.children) {
160-
let hasLarge = ~s.indexOf('\n');
163+
let hasLarge = pretty && ~s.indexOf('\n');
161164
for (let i=0; i<vnode.children.length; i++) {
162165
let child = vnode.children[i];
163166
if (child!=null && child!==false) {
164167
let childSvgMode = nodeName==='svg' ? true : nodeName==='foreignObject' ? false : isSvgMode,
165168
ret = renderToString(child, context, opts, true, childSvgMode);
166-
if (!hasLarge && pretty && isLargeString(ret)) hasLarge = true;
169+
if (pretty && !hasLarge && isLargeString(ret)) hasLarge = true;
167170
if (ret) pieces.push(ret);
168171
}
169172
}

0 commit comments

Comments
 (0)