Skip to content

Commit

Permalink
Hybrid build for ESM and CommonJS - Credits to https://www.sensedeep.…
Browse files Browse the repository at this point in the history
  • Loading branch information
JanST123 committed Jan 13, 2022
1 parent d61cfd9 commit f1a7cbb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
"name": "@gridscale/gsclient-js",
"version": "1.2.11",
"description": "gridscale API Wrapper",
"type": "module",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"exports": {
".": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js"
}
},
"types": "dist/cjs/index.d.ts",
"files": [
"dist/**/*"
],
"private": false,
"scripts": {
"build": "node ./scripts/clean_dist.js && tsc",
"build": "node ./scripts/clean_dist.js && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && ./scripts/fixup",
"build-browser": "npm run build && webpack",
"updateFromSpec": "node ./scripts/types_from_apispec.js"
},
Expand Down Expand Up @@ -43,7 +49,7 @@
"merge2": "^1.0.2",
"openapi-typescript-codegen": "^0.7.2",
"ts-loader": "^8.0.14",
"typescript": "^4.1.5",
"typescript": "~4.3.5",
"webpack": "^5.15.0",
"webpack-cli": "^4.3.1"
}
Expand Down
13 changes: 13 additions & 0 deletions scripts/fixup
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
# credits: https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html
cat >dist/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >dist/mjs/package.json <<!EOF
{
"type": "module"
}
!EOF
27 changes: 27 additions & 0 deletions tsconfig-base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"declaration": true,
"sourceMap": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "dist/src/",
"noImplicitAny": false,
"allowJs": false,
"rootDir": "./src",
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
],
"lib": [
"esnext",
"dom"
]
},
"exclude": ["node_modules", "dist"],
"include": [
"src/**/*",
]
}
8 changes: 8 additions & 0 deletions tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist/cjs",
"target": "es2015"
}
}
34 changes: 7 additions & 27 deletions tsconfig.json
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"declaration": true,
"module": "commonjs",
"sourceMap": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "dist/src/",
"noImplicitAny": false,
"allowJs": false,
"rootDir": "./src",
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2015",
"dom"
]
},
"include": [
"src/**/*",
]
}
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/mjs",
"target": "esnext"
}
}

0 comments on commit f1a7cbb

Please sign in to comment.