From 96a4348ab78fd4aab39384507a058735d66a45ce Mon Sep 17 00:00:00 2001 From: Ivan Karpan Date: Thu, 3 Apr 2025 12:05:00 +0100 Subject: [PATCH 1/3] Add CommonJS support while preserving ESM compatibility --- README.md | 11 +++++++++++ package.json | 41 ++++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 19 deletions(-) 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..6993398 100644 --- a/package.json +++ b/package.json @@ -2,40 +2,43 @@ "name": "regex-recursion", "version": "6.0.2", "description": "Recursive matching plugin for Regex+", - "author": "Steven Levithan", + "keywords": [ + "recursion", + "regex", + "regexp" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/slevithan/regex-recursion.git" + }, "license": "MIT", + "author": "Steven Levithan", "type": "module", "exports": { ".": { "types": "./types/index.d.ts", - "import": "./src/index.js" + "import": "./src/index.js", + "require": "./dist/cjs/index.js" } }, + "main": "./dist/cjs/index.js", "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", - "pretest": "pnpm run build", - "test": "jasmine", - "prepare": "pnpm test" - }, "files": [ "dist", "src", "types" ], - "repository": { - "type": "git", - "url": "git+https://github.com/slevithan/regex-recursion.git" + "scripts": { + "prebuild": "rm -rf dist/* 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.js", + "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", + "types": "tsc src/index.js --rootDir src --declaration --allowJs --emitDeclarationOnly --outDir types" }, - "keywords": [ - "recursion", - "regex", - "regexp" - ], "dependencies": { "regex-utilities": "^2.3.0" }, From 141c50a829c9c4531365764a90f1600d27e0d111 Mon Sep 17 00:00:00 2001 From: Ivan Karpan Date: Thu, 3 Apr 2025 12:08:53 +0100 Subject: [PATCH 2/3] Add tests for dual ESM and CommonJS support --- package.json | 6 +++--- spec/cjs-support-spec.cjs | 17 +++++++++++++++++ spec/esm-support-spec.js | 19 +++++++++++++++++++ spec/support/jasmine.json | 3 ++- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 spec/cjs-support-spec.cjs create mode 100644 spec/esm-support-spec.js diff --git a/package.json b/package.json index 6993398..68ab551 100644 --- a/package.json +++ b/package.json @@ -18,10 +18,10 @@ ".": { "types": "./types/index.d.ts", "import": "./src/index.js", - "require": "./dist/cjs/index.js" + "require": "./dist/cjs/index.cjs" } }, - "main": "./dist/cjs/index.js", + "main": "./dist/cjs/index.cjs", "browser": "./dist/regex-recursion.min.js", "types": "./types/index.d.ts", "files": [ @@ -32,7 +32,7 @@ "scripts": { "prebuild": "rm -rf dist/* 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.js", + "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", 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" From 02eb66d661939164a5193eaeff67ed1dcc5f4a78 Mon Sep 17 00:00:00 2001 From: Ivan Karpan Date: Fri, 4 Apr 2025 10:04:44 +0100 Subject: [PATCH 3/3] fix: updated exports to properly map the right types for each module format revert to original package.json formatting --- package.json | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 68ab551..0d2b82a 100644 --- a/package.json +++ b/package.json @@ -2,33 +2,24 @@ "name": "regex-recursion", "version": "6.0.2", "description": "Recursive matching plugin for Regex+", - "keywords": [ - "recursion", - "regex", - "regexp" - ], - "repository": { - "type": "git", - "url": "git+https://github.com/slevithan/regex-recursion.git" - }, - "license": "MIT", "author": "Steven Levithan", + "license": "MIT", "type": "module", "exports": { ".": { - "types": "./types/index.d.ts", - "import": "./src/index.js", - "require": "./dist/cjs/index.cjs" + "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", - "files": [ - "dist", - "src", - "types" - ], "scripts": { "prebuild": "rm -rf dist/* types/*", "build": "pnpm run bundle:global && pnpm run bundle:cjs && pnpm run types", @@ -37,8 +28,22 @@ "prepare": "pnpm test", "pretest": "pnpm run build", "test": "jasmine", - "types": "tsc src/index.js --rootDir src --declaration --allowJs --emitDeclarationOnly --outDir types" + "types": "tsc src/index.js --rootDir src --declaration --allowJs --emitDeclarationOnly --outDir types && cp types/index.d.ts types/index.d.cts" }, + "files": [ + "dist", + "src", + "types" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/slevithan/regex-recursion.git" + }, + "keywords": [ + "recursion", + "regex", + "regexp" + ], "dependencies": { "regex-utilities": "^2.3.0" },