Skip to content

Commit

Permalink
fix(#168): cjs default export for typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
commenthol committed Dec 10, 2022
1 parent 18a1a4b commit 714e796
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ lib/
.eslintcache
.idea/
.DS_Store
pnpm-lock.yaml
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@
},
"scripts": {
"all": "npm-run-all clean yaml build lint test doc:tree doc:attrib webpack",
"build": "rollup -c",
"build:rollup": "rollup -c",
"build:fixIndexCjs": "node ./scripts/postbuild.cjs",
"build": "npm-run-all build:rollup build:fixIndexCjs",
"changelog": "contributors && node scripts/gitlog.cjs",
"ci": "TEST_XXL=1 npm-run-all yaml build test",
"clean": "rimraf lib dist src/data.js",
Expand Down
16 changes: 16 additions & 0 deletions scripts/postbuild.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* get rid of this script if module will be switched to pure ESM with only named
* exports
*/

const path = require('path')
const fs = require('fs')

const indexCjs = `
var Holidays = require('./Holidays.cjs');
module.exports = Holidays.Holidays;
module.exports.default = Holidays.Holidays;
`

fs.writeFileSync(path.resolve(__dirname, '../lib/index.cjs'), indexCjs, 'utf8')
17 changes: 17 additions & 0 deletions test/indexCjs.spec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const assert = require('assert')

describe('cjs', function () {
it('module.exports', function () {
const Holidays = require('../lib/index.cjs')
const hd = new Holidays()
const countries = hd.getCountries()
assert.ok(Object.keys(countries).length > 100)
})

it('exports.default', function () {
const Holidays = require('../lib/index.cjs').default
const hd = new Holidays()
const countries = hd.getCountries()
assert.ok(Object.keys(countries).length > 100)
})
})

0 comments on commit 714e796

Please sign in to comment.