forked from flightcontrolhq/superjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cjs support (h/t @acheronfail in flightcontrolhq#301)
- Loading branch information
Showing
4 changed files
with
47 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
dist-cjs | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
function convertToCommonJs(filePath) { | ||
// update imports | ||
const content = fs.readFileSync(filePath, 'utf-8'); | ||
const updatedContent = content.replace( | ||
/(require\(['"])(.*)(\.js)(['"]\))/g, | ||
'$1$2.cjs$4' | ||
); | ||
fs.writeFileSync(filePath, updatedContent, 'utf-8'); | ||
|
||
// update file extension | ||
const commonJsPath = filePath.replace(/\.js$/, '.cjs'); | ||
fs.renameSync(filePath, commonJsPath); | ||
} | ||
|
||
function walk(dir) { | ||
fs.readdirSync(dir).forEach((file) => { | ||
const fullPath = path.join(dir, file); | ||
if (fs.lstatSync(fullPath).isDirectory()) { | ||
walk(fullPath); | ||
} else if (file.endsWith('.js')) { | ||
convertToCommonJs(fullPath); | ||
} | ||
}); | ||
} | ||
|
||
walk('./dist-cjs'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,16 +5,20 @@ | |
"typings": "dist/index.d.ts", | ||
"main": "./dist/index.js", | ||
"exports": { | ||
".": "./dist/index.js" | ||
"import": "./dist/index.js", | ||
"require": "./dist-cjs/index.cjs" | ||
}, | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"dist-cjs" | ||
], | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"build": "yarn run build:esm && yarn run build:cjs", | ||
"build:esm": "tsc -p tsconfig.json", | ||
"build:cjs": "tsc -p tsconfig.cjs.json && node commonjs.js", | ||
"test": "vitest run", | ||
"lint": "tsdx lint", | ||
"prepack": "yarn build", | ||
|
@@ -33,7 +37,8 @@ | |
"singleQuote": true, | ||
"trailingComma": "es5" | ||
}, | ||
"name": "superjson", | ||
"name": "@half0wl/superjson", | ||
"description": "fork of superjson with cjs support", | ||
"author": { | ||
"name": "Simon Knott", | ||
"email": "[email protected]", | ||
|
@@ -53,7 +58,7 @@ | |
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/blitz-js/superjson" | ||
"url": "https://github.com/half0wl/superjson-cjs" | ||
}, | ||
"devDependencies": { | ||
"@types/debug": "^4.1.5", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"outDir": "dist-cjs" | ||
} | ||
} |