Skip to content

Commit 04be332

Browse files
Fix default commonjs entries
1 parent 2484cd4 commit 04be332

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

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+
);

0 commit comments

Comments
 (0)