Skip to content

Commit

Permalink
wp
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldzjap committed Aug 18, 2024
1 parent 78c12e9 commit 6f005cf
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 19 deletions.
6 changes: 3 additions & 3 deletions example/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 2 additions & 2 deletions example/es/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
"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"
}
}
},
"type": "module",
"files": [
"dist"
],
"types": "dist/index.d.ts",
"scripts": {
"test": "cross-env NODE_ENV=test ./node_modules/.bin/jest",
"lint": "npx eslint .",
Expand Down
52 changes: 49 additions & 3 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,20 +12,64 @@ 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,
},
],
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);
}
});
});
}
});
},
},
}))(),
],
});
6 changes: 3 additions & 3 deletions src/Rand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "./tsconfig.base"
"extends": "./tsconfig.base",
"compilerOptions": {
"esModuleInterop": false
}
}
1 change: 1 addition & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.base",
"compilerOptions": {
"esModuleInterop": true,
"verbatimModuleSyntax": false
},
"include": ["__tests__/**/*"]
Expand Down

0 comments on commit 6f005cf

Please sign in to comment.