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

Fix commonjs entry in v6 #292

Merged
merged 3 commits into from
Mar 28, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/silver-lemons-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix error in commonjs entry point
27 changes: 19 additions & 8 deletions config/node-commonjs.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
const fs = require('fs');
const path = require('path');
const assert = require('assert/strict');

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

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

// Main entry
fs.copyFileSync(filePath('index.js'), filePath('commonjs.js'));
fs.copyFileSync(filePath('index.js.map'), filePath('commonjs.js.map'));

const source = `module.exports = require('./commonjs').default;`;
fs.writeFileSync(filePath('index.js'), source, 'utf-8');

// JSX entry
fs.copyFileSync(filePath('jsx.js'), filePath('jsx-entry.js'));
fs.copyFileSync(filePath('jsx.js.map'), filePath('jsx-entry.js.map'));

const sourceJsx = `module.exports = require('./jsx-entry').default;`;
const sourceJsx = [
`const entry = require('./jsx-entry');`,
`entry.default.render = entry.render;`,
`entry.default.shallowRender = entry.shallowRender;`,
`module.exports = entry.default;`
].join('\n');
fs.writeFileSync(filePath('jsx.js'), sourceJsx, 'utf-8');

// Verify CJS entries
const main = require(filePath('index.js'));
assert(typeof main === 'function', 'Default export is a function');

const jsx = require(filePath('jsx.js'));
assert(typeof jsx === 'function', 'Default export is a function');
assert(typeof jsx.render === 'function', 'render entry is a function');
assert(
typeof jsx.shallowRender === 'function',
'shallowRender entry is a function'
);
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.