Skip to content

Commit dfee177

Browse files
Merge pull request #294 from preactjs/fix-exports
2 parents 2e1c08c + 637b302 commit dfee177

File tree

8 files changed

+70
-20
lines changed

8 files changed

+70
-20
lines changed

.changeset/curvy-rings-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'preact-render-to-string': patch
3+
---
4+
5+
Bring back exports from 5.x to make migration easier

config/node-commonjs.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const assert = require('assert/strict');
43

54
// This file will only export default exports in commonjs bundles
65
// instead of guarding them behind a `.default` property.
76

87
const filePath = (file) => path.join(process.cwd(), 'dist', file);
98

9+
// Main entry
10+
fs.copyFileSync(filePath('index.js'), filePath('commonjs.js'));
11+
fs.copyFileSync(filePath('index.js.map'), filePath('commonjs.js.map'));
12+
13+
const source = [
14+
`const mod = require('./commonjs');`,
15+
`mod.default.renderToStaticMarkup = mod.default;`,
16+
`mod.default.renderToString = mod.default;`,
17+
`mod.default.render = mod.default;`,
18+
`module.exports = mod.default;`
19+
].join('\n');
20+
fs.writeFileSync(filePath('index.js'), source, 'utf-8');
21+
1022
// JSX entry
1123
fs.copyFileSync(filePath('jsx.js'), filePath('jsx-entry.js'));
1224
fs.copyFileSync(filePath('jsx.js.map'), filePath('jsx-entry.js.map'));
@@ -18,15 +30,3 @@ const sourceJsx = [
1830
`module.exports = entry.default;`
1931
].join('\n');
2032
fs.writeFileSync(filePath('jsx.js'), sourceJsx, 'utf-8');
21-
22-
// Verify CJS entries
23-
const main = require(filePath('index.js'));
24-
assert(typeof main === 'function', 'Default export is a function');
25-
26-
const jsx = require(filePath('jsx.js'));
27-
assert(typeof jsx === 'function', 'Default export is a function');
28-
assert(typeof jsx.render === 'function', 'render entry is a function');
29-
assert(
30-
typeof jsx.shallowRender === 'function',
31-
'shallowRender entry is a function'
32-
);

config/node-verify-exports.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const path = require('path');
2+
const assert = require('assert/strict');
3+
4+
const filePath = (file) => path.join(process.cwd(), 'dist', file);
5+
6+
// Main CJS
7+
const mainCjs = require(filePath('index.js'));
8+
assert(typeof mainCjs === 'function');
9+
assert(typeof mainCjs.renderToString === 'function');
10+
assert(typeof mainCjs.renderToStaticMarkup === 'function');
11+
assert(typeof mainCjs.render === 'function');
12+
13+
// Main ESM
14+
(async () => {
15+
const mainESM = await import(filePath('index.mjs'));
16+
assert(typeof mainESM.default === 'function');
17+
assert(typeof mainESM.renderToString === 'function');
18+
assert(typeof mainESM.renderToStaticMarkup === 'function');
19+
assert(typeof mainESM.render === 'function');
20+
})();
21+
22+
// JSX CJS
23+
const jsxCjs = require(filePath('jsx.js'));
24+
assert(typeof jsxCjs === 'function');
25+
assert(typeof jsxCjs.render === 'function');
26+
assert(typeof jsxCjs.shallowRender === 'function');
27+
28+
// JSX ESM
29+
(async () => {
30+
const jsxESM = await import(filePath('jsx.mjs'));
31+
assert(typeof jsxESM.default === 'function');
32+
assert(typeof jsxESM.render === 'function');
33+
assert(typeof jsxESM.shallowRender === 'function');
34+
})();

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"bench": "BABEL_ENV=test node -r @babel/register benchmarks index.js",
3030
"bench:v8": "BABEL_ENV=test microbundle benchmarks/index.js -f modern --alias benchmarkjs-pretty=benchmarks/lib/benchmark-lite.js --external none --target node --no-compress --no-sourcemap --raw -o benchmarks/.v8.mjs && v8 --module benchmarks/.v8.mjs",
3131
"build": "npm run -s transpile && npm run -s transpile:jsx && npm run -s copy-typescript-definition",
32-
"postbuild": "node ./config/node-13-exports.js && node ./config/node-commonjs.js",
32+
"postbuild": "node ./config/node-13-exports.js && node ./config/node-commonjs.js && node ./config/node-verify-exports.js",
3333
"transpile": "microbundle src/index.js -f es,cjs,umd --target web --external preact",
3434
"transpile:jsx": "microbundle src/jsx.js -o dist/jsx.js --target web --external preact && microbundle dist/jsx.js -o dist/jsx.js -f cjs --external preact",
3535
"copy-typescript-definition": "copyfiles -f src/*.d.ts dist",

src/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { VNode } from 'preact';
22

33
export default function renderToString(vnode: VNode, context?: any): string;
4+
5+
export function render(vnode: VNode, context?: any): string;
6+
export function renderToString(vnode: VNode, context?: any): string;
7+
export function renderToStaticMarkup(vnode: VNode, context?: any): string;

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let beforeDiff, afterDiff, renderHook, ummountHook;
2929
* @param {Object} [context={}] Initial root context object
3030
* @returns {string} serialized HTML
3131
*/
32-
export default function renderToString(vnode, context) {
32+
export function renderToString(vnode, context) {
3333
// Performance optimization: `renderToString` is synchronous and we
3434
// therefore don't execute any effects. To do that we pass an empty
3535
// array to `options._commit` (`__c`). But we can go one step further
@@ -379,3 +379,7 @@ const SELF_CLOSING = new Set([
379379
'track',
380380
'wbr'
381381
]);
382+
383+
export default renderToString;
384+
export const render = renderToString;
385+
export const renderToStaticMarkup = renderToString;

src/jsx.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export default function renderToStringPretty(
1515
context?: any,
1616
options?: Options
1717
): string;
18+
export function render(vnode: VNode, context?: any, options?: Options): string;
1819

19-
export function shallowRender(vnode: VNode, context?: any): string;
20-
21-
export default render;
20+
export function shallowRender(
21+
vnode: VNode,
22+
context?: any,
23+
options?: Options
24+
): string;

0 commit comments

Comments
 (0)