Created
January 9, 2023 17:33
-
-
Save kim3er/9671cc736c96fe22c916a57d3b271827 to your computer and use it in GitHub Desktop.
TypeScript package with ES Module and CommonJS support
This file contains hidden or 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
{ | |
"name": "example-proj", | |
"version": "0.0.1", | |
"description": "", | |
"exports": { | |
"./*": { | |
"import": "./js/*.js", | |
"require": "./js/*.cjs" | |
} | |
}, | |
"typesVersions": { | |
"*": { | |
"*": [ | |
"js/*" | |
] | |
} | |
}, | |
"scripts": { | |
"build": "tsc && tsc -p ./tsconfig.common.json && node ./scripts/post_build.cjs", | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"tslib": "^2.4.1" | |
}, | |
"devDependencies": { | |
"typescript": "^4.9.3" | |
} | |
} |
This file contains hidden or 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
const fs = require('fs/promises'), | |
path = require('path'); | |
const commonDir = path.join(__dirname, '../common'); | |
async function processDir(dir) { | |
const commonFiles = await fs.readdir(dir); | |
for (const file of commonFiles) { | |
if (!file.endsWith('.js')) { | |
const subDir = path.join(dir, file); | |
if ((await fs.stat(subDir)).isDirectory()) { | |
await processDir(subDir); | |
} | |
continue; | |
} | |
await fs.copyFile(path.join(dir, file), path.join(dir.replace('common', 'js'), | |
file.replace('.js', '.cjs'))); | |
} | |
} | |
(async () => { | |
await processDir(commonDir); | |
await fs.rmdir(commonDir, { recursive: true }); | |
})(); |
This file contains hidden or 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
{ | |
"extends": "./tsconfig.json", | |
"compilerOptions": { | |
"module": "CommonJS", | |
"declaration": false, | |
"outDir": "common" | |
} | |
} |
This file contains hidden or 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
{ | |
"compilerOptions": { | |
"target": "ES2016", | |
"module": "ESNext", | |
"lib": [ | |
"ESNext", | |
"DOM", | |
"DOM.Iterable" | |
], | |
"declaration": true, | |
"outDir": "js", | |
"importHelpers": true, | |
"downlevelIteration": true, | |
"strict": true, | |
"noImplicitAny": true, | |
"noUnusedLocals": true, | |
"moduleResolution": "Node", | |
"esModuleInterop": true, | |
"forceConsistentCasingInFileNames": true | |
}, | |
"include": [ | |
"ts/**/*" | |
], | |
"exclude": [ | |
"tests", | |
"js", | |
"node_modules" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
post_build.cjs
goes into a folder called 'scripts'.