Skip to content

Instantly share code, notes, and snippets.

@kim3er
Created January 9, 2023 17:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kim3er/9671cc736c96fe22c916a57d3b271827 to your computer and use it in GitHub Desktop.
Save kim3er/9671cc736c96fe22c916a57d3b271827 to your computer and use it in GitHub Desktop.
TypeScript package with ES Module and CommonJS support
{
"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"
}
}
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 });
})();
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"declaration": false,
"outDir": "common"
}
}
{
"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"
]
}
@kim3er
Copy link
Author

kim3er commented Jan 9, 2023

  • post_build.cjs goes into a folder called 'scripts'.
  • The source TS files go into a folder called 'ts'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment