Skip to content

Commit 45c2938

Browse files
Remove all default exports (major)
Mixing both default and named exports has no clearly defined behaviour and can easily lead to unexpected results. Some tools use a combination of "default" + "__esModule" property and others ignore named exports completely if both are found. Those issues are difficult to debug when multiple tools are used together. This change makes some code redundant by not having to patch a hand crafted exports object for commonjs environments.
1 parent 872fe18 commit 45c2938

13 files changed

+57
-70
lines changed

Diff for: README.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Works in Node & the browser, making it useful for universal/isomorphic rendering
1616
### Render JSX/VDOM to HTML
1717

1818
```js
19-
import render from 'preact-render-to-string';
19+
import { render } from 'preact-render-to-string';
2020
import { h } from 'preact';
2121
/** @jsx h */
2222

@@ -31,7 +31,7 @@ console.log(html);
3131
### Render Preact Components to HTML
3232

3333
```js
34-
import render from 'preact-render-to-string';
34+
import { render } from 'preact-render-to-string';
3535
import { h, Component } from 'preact';
3636
/** @jsx h */
3737

@@ -66,7 +66,7 @@ console.log(html);
6666
```js
6767
import express from 'express';
6868
import { h } from 'preact';
69-
import render from 'preact-render-to-string';
69+
import { render } from 'preact-render-to-string';
7070
/** @jsx h */
7171

7272
// silly example component:
@@ -92,8 +92,30 @@ app.get('/:fox', (req, res) => {
9292

9393
---
9494

95+
## Migration guide
9596

96-
### License
97+
### Migrating from 5.x to 6.x
98+
99+
The only breaking change introduced with the `6.x` is that the `default` exports have been removed in favor of named exports. To update, replace the default import in your code with a named one.
100+
101+
```diff
102+
- import render from 'preact-render-to-string';
103+
+ import { render } from 'preact-render-to-string';
104+
```
105+
106+
Similarily if you've been using the `jsx` renderer, the default import needs to be swapped with a named import:
107+
108+
```diff
109+
- import render from 'preact-render-to-string/jsx';
110+
+ import { render } from 'preact-render-to-string/jsx';
111+
```
112+
113+
_Note: The named exports were already present in the `5.x` release line. So if you can't update today for any reason, you can apply the above changes safely to make a future update to `6.x` easier!_
114+
115+
---
116+
117+
118+
## License
97119

98120
[MIT]
99121

Diff for: benchmarks/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { h } from 'preact';
22
import Suite from 'benchmarkjs-pretty';
3-
import renderToString from '../src/index';
3+
import { renderToString } from '../src/index';
44
import TextApp from './text';
55
// import StackApp from './stack';
66
import { App as IsomorphicSearchResults } from './isomorphic-ui-search-results';

Diff for: config/node-commonjs.js

-21
This file was deleted.

Diff for: jsx.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ interface Options {
1010
}
1111

1212
export function render(vnode: VNode, context?: any, options?: Options): string;
13-
export default render;

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"scripts": {
2525
"bench": "node -r @babel/register benchmarks index.js",
2626
"build": "npm run -s transpile && npm run -s transpile:jsx && npm run -s copy-typescript-definition",
27-
"postbuild": "node ./config/node-13-exports.js && node ./config/node-commonjs.js",
27+
"postbuild": "node ./config/node-13-exports.js",
2828
"transpile": "microbundle src/index.js -f es,umd --target web --external preact",
2929
"transpile:jsx": "microbundle src/jsx.js -o dist/jsx.js --target web --external none && microbundle dist/jsx.js -o dist/jsx.js -f cjs",
3030
"copy-typescript-definition": "copyfiles -f src/*.d.ts dist",

Diff for: src/index.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ interface Options {
66
pretty?: boolean | string;
77
}
88

9-
export function render(vnode: VNode, context?: any, options?: Options): string;
109
export function renderToString(
1110
vnode: VNode,
1211
context?: any,
1312
options?: Options
1413
): string;
14+
export const render: typeof renderToString;
1515
export function shallowRender(vnode: VNode, context?: any): string;
16-
export default render;

Diff for: src/index.js

+20-26
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,42 @@ const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
1919

2020
const noop = () => {};
2121

22-
/** Render Preact JSX + Components to an HTML string.
23-
* @name render
24-
* @function
25-
* @param {VNode} vnode JSX VNode to render.
26-
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
27-
* @param {Object} [options={}] Rendering options
28-
* @param {Boolean} [options.shallow=false] If `true`, renders nested Components as HTML elements (`<Foo a="b" />`).
29-
* @param {Boolean} [options.xml=false] If `true`, uses self-closing tags for elements without children.
30-
* @param {Boolean} [options.pretty=false] If `true`, adds whitespace for readability
31-
* @param {RegEx|undefined} [options.voidElements] RegeEx that matches elements that are considered void (self-closing)
32-
*/
33-
renderToString.render = renderToString;
34-
3522
/** Only render elements, leaving Components inline as `<ComponentName ... />`.
3623
* This method is just a convenience alias for `render(vnode, context, { shallow:true })`
3724
* @name shallow
3825
* @function
3926
* @param {VNode} vnode JSX VNode to render.
4027
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
4128
*/
42-
let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);
29+
export let shallowRender = (vnode, context) =>
30+
renderToString(vnode, context, SHALLOW);
4331

4432
const EMPTY_ARR = [];
45-
function renderToString(vnode, context, opts) {
33+
34+
/**
35+
* Render Preact JSX + Components to an HTML string.
36+
* @param {VNode} vnode JSX VNode to render.
37+
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
38+
* @param {Object} [options={}] Rendering options
39+
* @param {Boolean} [options.shallow=false] If `true`, renders nested Components as HTML elements (`<Foo a="b" />`).
40+
* @param {Boolean} [options.xml=false] If `true`, uses self-closing tags for elements without children.
41+
* @param {Boolean} [options.pretty=false] If `true`, adds whitespace for readability
42+
* @param {RegEx|undefined} [options.voidElements] RegeEx that matches elements that are considered void (self-closing)
43+
*/
44+
export function renderToString(vnode, context, opts) {
4645
const res = _renderToString(vnode, context, opts);
4746
// options._commit, we don't schedule any effects in this library right now,
4847
// so we can pass an empty queue to this hook.
4948
if (options.__c) options.__c(vnode, EMPTY_ARR);
5049
return res;
5150
}
5251

53-
/** The default export is an alias of `render()`. */
52+
export {
53+
// Aliased export for React compat
54+
renderToString as renderToStaticMarkup,
55+
renderToString as render
56+
};
57+
5458
function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
5559
if (vnode == null || typeof vnode === 'boolean') {
5660
return '';
@@ -407,13 +411,3 @@ function getFallbackComponentName(component) {
407411
}
408412
return name;
409413
}
410-
renderToString.shallowRender = shallowRender;
411-
412-
export default renderToString;
413-
414-
export {
415-
renderToString as render,
416-
renderToString as renderToStaticMarkup,
417-
renderToString,
418-
shallowRender
419-
};

Diff for: src/jsx.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ interface Options {
1010
}
1111

1212
export function render(vnode: VNode, context?: any, options?: Options): string;
13-
export default render;

Diff for: src/jsx.js

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

@@ -72,5 +72,4 @@ function renderToJsxString(vnode, context, opts, inner) {
7272
return renderToString(vnode, context, opts, inner);
7373
}
7474

75-
export default renderToJsxString;
7675
export { renderToJsxString as render };

Diff for: test/context.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import render from '../src/jsx';
1+
import { render } from '../src/jsx';
22
import { h, createContext, Component } from 'preact';
33
import chai, { expect } from 'chai';
44
import sinonChai from 'sinon-chai';

Diff for: test/index.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
import renderToString, {
1+
import {
22
render,
33
shallowRender,
44
renderToStaticMarkup,
5-
renderToString as _renderToString
5+
renderToString
66
} from '../src';
77
import { expect } from 'chai';
88

99
describe('render-to-string', () => {
1010
describe('exports', () => {
11-
it('exposes renderToString as default', () => {
12-
expect(renderToString).to.be.a('function');
13-
});
14-
1511
it('exposes render as a named export', () => {
1612
expect(render).to.be.a('function');
1713
expect(render).to.equal(renderToString);
1814
});
1915

2016
it('exposes renderToString as a named export', () => {
21-
expect(_renderToString).to.be.a('function');
22-
expect(_renderToString).to.equal(renderToString);
17+
expect(renderToString).to.be.a('function');
18+
expect(renderToString).to.equal(renderToString);
2319
});
2420

2521
it('exposes renderToStaticMarkup as a named export', () => {

Diff for: test/jsx.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import render from '../src/jsx';
1+
import { render } from '../src/jsx';
22
import { h } from 'preact';
33
import chai, { expect } from 'chai';
44
import sinonChai from 'sinon-chai';

Diff for: test/preact-render-to-string-tests.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import render from '../src';
1+
import { render } from '../src';
22
import { h } from 'preact';
33

44
let vdom = <div class="foo">content</div>;

0 commit comments

Comments
 (0)