diff --git a/config/node-13-exports.js b/config/node-13-exports.js new file mode 100644 index 0000000..3b0de94 --- /dev/null +++ b/config/node-13-exports.js @@ -0,0 +1,15 @@ +const fs = require('fs'); + +const snakeCaseToCamelCase = str => + str.replace(/([-_][a-z])/g, group => group.toUpperCase().replace('-', '')); + +const copy = name => { + // Copy .module.js --> .mjs for Node 13 compat. + const filename = name.includes('-') ? snakeCaseToCamelCase(name) : name; + fs.writeFileSync( + `${process.cwd()}/dist/${filename}.mjs`, + fs.readFileSync(`${process.cwd()}/dist/${filename}.module.js`) + ); +}; + +copy('index'); diff --git a/package.json b/package.json index ba581de..9d2f324 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,14 @@ "module": "dist/index.module.js", "umd:main": "dist/index.umd.js", "source": "src/index.js", + "exports": { + ".": { + "browser": "./dist/index.module.js", + "umd": "./dist/index.umd.js", + "import": "./dist/index.mjs", + "require": "./dist/index.js" + } + }, "types": "typings/index.d.ts", "files": [ "dist", @@ -31,7 +39,8 @@ "build:clean": "rimraf dist", "build:bundle": "microbundle -i src/index.js --external preact --compress --strict --name preactSsrPrepass", "build:flow": "flow-copy-source -i '*.test.js' src/ dist/", - "prepublishOnly": "npm run test && npm run build" + "prepublishOnly": "npm run test && npm run build", + "postbuild": "node ./config/node-13-exports.js" }, "peerDependencies": { "preact": ">=10 || ^10.0.0-beta.0 || ^10.0.0-alpha.0"