Skip to content

Commit ea1c5d8

Browse files
Add submodule exports (#134)
* Add submodule exports * Add fallback exports declarations for Rollup
1 parent 4b10aa9 commit ea1c5d8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

config/node-13-exports.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fs = require('fs');
2+
3+
const snakeCaseToCamelCase = str =>
4+
str.replace(/([-_][a-z])/g, group => group.toUpperCase().replace('-', ''));
5+
6+
const copy = name => {
7+
// Copy .module.js --> .mjs for Node 13 compat.
8+
const filename = name.includes('-') ? snakeCaseToCamelCase(name) : name;
9+
fs.writeFileSync(
10+
`${process.cwd()}/dist/${filename}.mjs`,
11+
fs.readFileSync(`${process.cwd()}/dist/${filename}.module.js`)
12+
);
13+
};
14+
15+
copy('index');
16+
copy('jsx');

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,23 @@
77
"umd:main": "dist/index.js",
88
"module": "dist/index.module.js",
99
"jsnext:main": "dist/index.module.js",
10+
"exports": {
11+
".": {
12+
"require": "./dist/index.js",
13+
"import": "./dist/index.mjs",
14+
"browser": "./dist/index.module.js"
15+
},
16+
"./jsx": {
17+
"require": "./dist/jsx.js",
18+
"import": "./dist/jsx.mjs",
19+
"browser": "./dist/jsx.module.js"
20+
},
21+
"./package.json": "./package.json",
22+
"./": "./"
23+
},
1024
"scripts": {
1125
"build": "npm run -s transpile && npm run -s transpile:jsx && npm run -s copy-typescript-definition",
26+
"postbuild": "node ./config/node-13-exports.js",
1227
"transpile": "microbundle src/index.js -f es,umd --target web --external preact",
1328
"transpile:jsx": "microbundle src/jsx.js -o dist/jsx.js --target web --external none && microbundle dist/jsx.js -o dist/jsx.js -f cjs",
1429
"copy-typescript-definition": "copyfiles -f src/*.d.ts dist",

0 commit comments

Comments
 (0)