Skip to content

Commit

Permalink
Merge pull request #292 from preactjs/fix-cjs
Browse files Browse the repository at this point in the history
Fix commonjs entry in v6
  • Loading branch information
marvinhagemeister authored Mar 28, 2023
2 parents 2484cd4 + 8f4692c commit 953c053
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
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.

0 comments on commit 953c053

Please sign in to comment.