diff --git a/example/cjs/index.cjs b/example/cjs/index.cjs index 760cae9..c6f6024 100644 --- a/example/cjs/index.cjs +++ b/example/cjs/index.cjs @@ -1,10 +1,10 @@ -const Rand = require('../../dist/index.cjs').default; -const PRNG = require('../../dist/index.cjs'); +const Rand = require('../../dist/cjs/index.js').default; +const { PRNG } = require('../../dist/cjs/index.js'); let rand = new Rand('1234', PRNG.mulberry32); const result1 = Array.from({ length: 10 }, () => rand.next()); -rand = new Rand('1234'); +rand = new Rand('1234', PRNG.mulberry32); const result2 = Array.from({ length: 10 }, () => rand.next()); console.log('RESULT1:', result1); diff --git a/example/es/index.js b/example/es/index.js index 911cce4..d800028 100644 --- a/example/es/index.js +++ b/example/es/index.js @@ -1,9 +1,9 @@ -import Rand, { PRNG } from '../../dist/index.js'; +import Rand, { PRNG } from '../../dist/es/index.js'; let rand = new Rand('1234', PRNG.mulberry32); const result1 = Array.from({ length: 10 }, () => rand.next()); -rand = new Rand('1234'); +rand = new Rand('1234', PRNG.mulberry32); const result2 = Array.from({ length: 10 }, () => rand.next()); console.log('RESULT1:', result1); diff --git a/package.json b/package.json index 4d9fb4c..1409390 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,12 @@ "exports": { ".": { "import": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" + "types": "./dist/es/index.d.ts", + "default": "./dist/es/index.js" }, "require": { - "types": "./dist/index.d.ts", - "default": "./dist/index.cjs" + "types": "./dist/cjs/index.d.ts", + "default": "./dist/cjs/index.js" } } }, @@ -18,7 +18,6 @@ "files": [ "dist" ], - "types": "dist/index.d.ts", "scripts": { "test": "cross-env NODE_ENV=test ./node_modules/.bin/jest", "lint": "npx eslint .", diff --git a/rollup.config.ts b/rollup.config.ts index d5bc4f7..d46738e 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -1,3 +1,5 @@ +import fs from 'fs'; + import typescript from '@rollup/plugin-typescript'; import terser from '@rollup/plugin-terser'; import clean from '@rollup-extras/plugin-clean'; @@ -10,14 +12,14 @@ export default defineConfig({ output: [ { dir: './dist', - entryFileNames: 'index.cjs', + entryFileNames: 'cjs/index.js', format: 'cjs', sourcemap: local, exports: 'named', }, { dir: './dist', - entryFileNames: 'index.js', + entryFileNames: 'es/index.js', format: 'es', sourcemap: local, }, @@ -25,5 +27,49 @@ export default defineConfig({ watch: { include: 'src/**', }, - plugins: [clean('dist'), typescript({ sourceMap: local }), terser()], + plugins: [ + clean('dist'), + typescript({ sourceMap: local }), + terser(), + (() => ({ + name: 'collectCjsTypeDeclaration', + writeBundle: { + sequential: true, + order: 'post', + handler: ({ format }) => { + if (format !== 'cjs') { + return; + } + + fs.readdir('./dist/es', { recursive: true }, (err, list) => { + for (const item of list) { + if (item.toString().endsWith('.js')) { + continue; + } + + if (fs.lstatSync(`./dist/es/${item.toString()}`).isDirectory()) { + if (!fs.existsSync(`./dist/cjs/${item.toString()}`)) { + fs.mkdirSync(`./dist/cjs/${item.toString()}`, { recursive: true }); + } + + continue; + } + + fs.copyFile(`./dist/es/${item}`, `./dist/cjs/${item}`, (err) => { + if (err) { + console.error(err); + } + + fs.writeFile('./dist/cjs/package.json', '{\n\t"type": "commonjs"\n}', (err) => { + if (err) { + console.error(err); + } + }); + }); + } + }); + }, + }, + }))(), + ], }); diff --git a/src/Rand.ts b/src/Rand.ts index a09d037..c804a8e 100644 --- a/src/Rand.ts +++ b/src/Rand.ts @@ -28,7 +28,7 @@ class Rand { * The string that will be used for generating a suitable hash for any of * the provided PRNG algorithms. * - * @var {string} + * @var {string|undefined} */ private str?: string; @@ -49,8 +49,8 @@ class Rand { /** * Create a new rand instance. * - * @param {string} str - * @param {PRNG} prng + * @param {string|undefined} str + * @param {PRNG|undefined} prng */ public constructor(str?: string, prng: PRNG = PRNG.sfc32) { this.str = str; diff --git a/tsconfig.base.json b/tsconfig.base.json index c0b0d3e..f155a96 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -7,12 +7,11 @@ "resolveJsonModule": true, "verbatimModuleSyntax": true, "useDefineForClassFields": true, - "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "skipLibCheck": true, "strict": true, "declaration": true, - "declarationDir": "./dist", + "declarationDir": "./dist/es", "rootDir": "./src", "outDir": "./dist" }, diff --git a/tsconfig.json b/tsconfig.json index 82e63f2..50dfc0f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,3 +1,6 @@ { - "extends": "./tsconfig.base" + "extends": "./tsconfig.base", + "compilerOptions": { + "esModuleInterop": false + } } diff --git a/tsconfig.test.json b/tsconfig.test.json index 1cc5e6e..e28740f 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -1,6 +1,7 @@ { "extends": "./tsconfig.base", "compilerOptions": { + "esModuleInterop": true, "verbatimModuleSyntax": false }, "include": ["__tests__/**/*"]