Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all default exports (major) #185

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Works in Node & the browser, making it useful for universal/isomorphic rendering
### Render JSX/VDOM to HTML

```js
import render from 'preact-render-to-string';
import { render } from 'preact-render-to-string';
import { h } from 'preact';
/** @jsx h */

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

```js
import render from 'preact-render-to-string';
import { render } from 'preact-render-to-string';
import { h, Component } from 'preact';
/** @jsx h */

Expand Down Expand Up @@ -66,7 +66,7 @@ console.log(html);
```js
import express from 'express';
import { h } from 'preact';
import render from 'preact-render-to-string';
import { render } from 'preact-render-to-string';
/** @jsx h */

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

---

## Migration guide

### License
### Migrating from 5.x to 6.x

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.

```diff
- import render from 'preact-render-to-string';
+ import { render } from 'preact-render-to-string';
```

Similarily if you've been using the `jsx` renderer, the default import needs to be swapped with a named import:

```diff
- import render from 'preact-render-to-string/jsx';
+ import { render } from 'preact-render-to-string/jsx';
```

_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!_

---


## License

[MIT]

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h } from 'preact';
import Suite from 'benchmarkjs-pretty';
import renderToString from '../src/index';
import { renderToString } from '../src/index';
import TextApp from './text';
// import StackApp from './stack';
import { App as IsomorphicSearchResults } from './isomorphic-ui-search-results';
Expand Down
21 changes: 0 additions & 21 deletions config/node-commonjs.js

This file was deleted.

15 changes: 7 additions & 8 deletions jsx.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { VNode } from 'preact';

interface Options {
jsx?: boolean;
xml?: boolean;
functions?: boolean
functionNames?: boolean,
skipFalseAttributes?: boolean
pretty?: boolean | string;
jsx?: boolean;
xml?: boolean;
functions?: boolean;
functionNames?: boolean;
skipFalseAttributes?: boolean;
pretty?: boolean | string;
}

export function render(vnode: VNode, context?: any, options?: Options):string;
export default render;
export function render(vnode: VNode, context?: any, options?: Options): string;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"scripts": {
"bench": "node -r @babel/register benchmarks index.js",
"build": "npm run -s transpile && npm run -s transpile:jsx && npm run -s copy-typescript-definition",
"postbuild": "node ./config/node-13-exports.js && node ./config/node-commonjs.js",
"postbuild": "node ./config/node-13-exports.js",
"transpile": "microbundle src/index.js -f es,umd --target web --external preact",
"transpile:jsx": "microbundle src/jsx.js -o dist/jsx.js --target web --external none && microbundle dist/jsx.js -o dist/jsx.js -f cjs",
"copy-typescript-definition": "copyfiles -f src/*.d.ts dist",
Expand Down
3 changes: 1 addition & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ interface Options {
pretty?: boolean | string;
}

export function render(vnode: VNode, context?: any, options?: Options): string;
export function renderToString(
vnode: VNode,
context?: any,
options?: Options
): string;
export const render: typeof renderToString;
export function shallowRender(vnode: VNode, context?: any): string;
export default render;
46 changes: 20 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,42 @@ const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;

const noop = () => {};

/** Render Preact JSX + Components to an HTML string.
* @name render
* @function
* @param {VNode} vnode JSX VNode to render.
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
* @param {Object} [options={}] Rendering options
* @param {Boolean} [options.shallow=false] If `true`, renders nested Components as HTML elements (`<Foo a="b" />`).
* @param {Boolean} [options.xml=false] If `true`, uses self-closing tags for elements without children.
* @param {Boolean} [options.pretty=false] If `true`, adds whitespace for readability
* @param {RegEx|undefined} [options.voidElements] RegeEx that matches elements that are considered void (self-closing)
*/
renderToString.render = renderToString;

/** Only render elements, leaving Components inline as `<ComponentName ... />`.
* This method is just a convenience alias for `render(vnode, context, { shallow:true })`
* @name shallow
* @function
* @param {VNode} vnode JSX VNode to render.
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
*/
let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);
export let shallowRender = (vnode, context) =>
renderToString(vnode, context, SHALLOW);

const EMPTY_ARR = [];
function renderToString(vnode, context, opts) {

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

/** The default export is an alias of `render()`. */
export {
// Aliased export for React compat
renderToString as renderToStaticMarkup,
renderToString as render
};

function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
if (vnode == null || typeof vnode === 'boolean') {
return '';
Expand Down Expand Up @@ -407,13 +411,3 @@ function getFallbackComponentName(component) {
}
return name;
}
renderToString.shallowRender = shallowRender;

export default renderToString;

export {
renderToString as render,
renderToString as renderToStaticMarkup,
renderToString,
shallowRender
};
1 change: 0 additions & 1 deletion src/jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ interface Options {
}

export function render(vnode: VNode, context?: any, options?: Options): string;
export default render;
3 changes: 1 addition & 2 deletions src/jsx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './polyfills';
import renderToString from './index';
import { renderToString } from './index';
import { indent, encodeEntities, assign } from './util';
import prettyFormat from 'pretty-format';

Expand Down Expand Up @@ -72,5 +72,4 @@ function renderToJsxString(vnode, context, opts, inner) {
return renderToString(vnode, context, opts, inner);
}

export default renderToJsxString;
export { renderToJsxString as render };
2 changes: 1 addition & 1 deletion test/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import render from '../src/jsx';
import { render } from '../src/jsx';
import { h, createContext, Component } from 'preact';
import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
Expand Down
12 changes: 4 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import renderToString, {
import {
render,
shallowRender,
renderToStaticMarkup,
renderToString as _renderToString
renderToString
} from '../src';
import { expect } from 'chai';

describe('render-to-string', () => {
describe('exports', () => {
it('exposes renderToString as default', () => {
expect(renderToString).to.be.a('function');
});

it('exposes render as a named export', () => {
expect(render).to.be.a('function');
expect(render).to.equal(renderToString);
});

it('exposes renderToString as a named export', () => {
expect(_renderToString).to.be.a('function');
expect(_renderToString).to.equal(renderToString);
expect(renderToString).to.be.a('function');
expect(renderToString).to.equal(renderToString);
});

it('exposes renderToStaticMarkup as a named export', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/jsx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import render from '../src/jsx';
import { render } from '../src/jsx';
import { h } from 'preact';
import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
Expand Down
2 changes: 1 addition & 1 deletion test/preact-render-to-string-tests.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import render from '../src';
import { render } from '../src';
import { h } from 'preact';

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