diff --git a/README.md b/README.md index 6d29d3e..4bf0934 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,17 @@ import {recursion} from 'regex-recursion'; const re = regex({plugins: [recursion]})`…`; ``` +
+ Using CommonJS require + +```js +const {regex} = require('regex'); +const {recursion} = require('regex-recursion'); + +const re = regex({plugins: [recursion]})`…`; +``` +
+
Using a global name (no import) diff --git a/package.json b/package.json index 9505c17..0d2b82a 100644 --- a/package.json +++ b/package.json @@ -7,20 +7,28 @@ "type": "module", "exports": { ".": { - "types": "./types/index.d.ts", - "import": "./src/index.js" + "import": { + "types": "./types/index.d.ts", + "default": "./src/index.js" + }, + "require": { + "types": "./types/index.d.cts", + "default": "./dist/cjs/index.cjs" + } } }, + "main": "./dist/cjs/index.cjs", "browser": "./dist/regex-recursion.min.js", "types": "./types/index.d.ts", "scripts": { - "bundle:global": "esbuild src/index.js --global-name=Regex.plugins --bundle --minify --sourcemap --outfile=dist/regex-recursion.min.js", - "types": "tsc src/index.js --rootDir src --declaration --allowJs --emitDeclarationOnly --outDir types", "prebuild": "rm -rf dist/* types/*", - "build": "pnpm run bundle:global && pnpm run types", + "build": "pnpm run bundle:global && pnpm run bundle:cjs && pnpm run types", + "bundle:cjs": "esbuild src/index.js --format=cjs --bundle --outfile=dist/cjs/index.cjs", + "bundle:global": "esbuild src/index.js --global-name=Regex.plugins --bundle --minify --sourcemap --outfile=dist/regex-recursion.min.js", + "prepare": "pnpm test", "pretest": "pnpm run build", "test": "jasmine", - "prepare": "pnpm test" + "types": "tsc src/index.js --rootDir src --declaration --allowJs --emitDeclarationOnly --outDir types && cp types/index.d.ts types/index.d.cts" }, "files": [ "dist", diff --git a/spec/cjs-support-spec.cjs b/spec/cjs-support-spec.cjs new file mode 100644 index 0000000..58bd915 --- /dev/null +++ b/spec/cjs-support-spec.cjs @@ -0,0 +1,17 @@ +// This file uses CommonJS syntax to test the CJS compatibility +const {recursion} = require('../dist/cjs/index.cjs'); + +describe('CommonJS support', () => { + it('should export recursion function via require()', () => { + expect(typeof recursion).toBe('function'); + }); + + it('should have proper function behavior', () => { + const pattern = 'a(?R=2)?b'; + const result = recursion(pattern); + + expect(result.pattern).toBe('a(?:a(?:)?b)?b'); + expect(result.captureTransfers instanceof Map).toBe(true); + expect(Array.isArray(result.hiddenCaptures)).toBe(true); + }); +}); diff --git a/spec/esm-support-spec.js b/spec/esm-support-spec.js new file mode 100644 index 0000000..da92c52 --- /dev/null +++ b/spec/esm-support-spec.js @@ -0,0 +1,19 @@ +// This file uses ESM syntax to test the ESM compatibility +import { recursion } from '../src/index.js'; + +describe('ESM support', () => { + it('should export recursion function via import', () => { + expect(typeof recursion).toBe('function'); + }); + + it('should have proper function behavior', () => { + const pattern = 'a(?R=2)?b'; + const result = recursion(pattern); + + expect(result).toEqual({ + pattern: 'a(?:a(?:)?b)?b', + captureTransfers: jasmine.any(Map), + hiddenCaptures: [] + }); + }); +}); diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json index 0699cde..da890eb 100644 --- a/spec/support/jasmine.json +++ b/spec/support/jasmine.json @@ -1,7 +1,8 @@ { "spec_dir": "spec", "spec_files": [ - "*-spec.js" + "*-spec.js", + "*-spec.cjs" ], "helpers": [ "helpers/**/*.?(m)js"