Skip to content

Commit 953c053

Browse files
Merge pull request preactjs#292 from preactjs/fix-cjs
Fix commonjs entry in v6
2 parents 2484cd4 + 8f4692c commit 953c053

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

.changeset/silver-lemons-sort.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+
Fix error in commonjs entry point

config/node-commonjs.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const assert = require('assert/strict');
34

45
// This file will only export default exports in commonjs bundles
56
// instead of guarding them behind a `.default` property.
67

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

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 = `module.exports = require('./commonjs').default;`;
14-
fs.writeFileSync(filePath('index.js'), source, 'utf-8');
15-
1610
// JSX entry
1711
fs.copyFileSync(filePath('jsx.js'), filePath('jsx-entry.js'));
1812
fs.copyFileSync(filePath('jsx.js.map'), filePath('jsx-entry.js.map'));
1913

20-
const sourceJsx = `module.exports = require('./jsx-entry').default;`;
14+
const sourceJsx = [
15+
`const entry = require('./jsx-entry');`,
16+
`entry.default.render = entry.render;`,
17+
`entry.default.shallowRender = entry.shallowRender;`,
18+
`module.exports = entry.default;`
19+
].join('\n');
2120
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+
);

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.

0 commit comments

Comments
 (0)