Skip to content

Commit b3b6550

Browse files
authored
Merge pull request #74 from klerick/create-esm-module
build(json-api-nestjs): Create hyprid npm package
2 parents 5a5bbc8 + 87a949b commit b3b6550

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"error",
99
{
1010
"enforceBuildableLibDependency": true,
11-
"allow": [],
11+
"allow": ["json-shared-type", "shared-utils"],
1212
"depConstraints": [
1313
{
1414
"sourceTag": "*",

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: CI
22
on:
33
pull_request:
44
branches:
5-
- main
5+
- master
66
types:
77
- opened
88
- synchronize
@@ -18,7 +18,7 @@ jobs:
1818
# This line is needed for nx affected to work when CI is running on a PR
1919
- run: git branch --track main origin/master
2020
- name: Test and build
21-
run: npx nx affected -t lint test build --parallel=3 --exclude='json-api-front,json-api-server,json-api-server-e2e,shared-utils,json-shared-type,database'
21+
run: npx nx affected -t test build --parallel=3 --exclude='json-api-front,json-api-server,json-api-server-e2e,json-shared-type,database,@nestjs-json-api/source'
2222
- name: Cache npm dependencies
2323
id: cache-dependencies-save
2424
uses: actions/cache/save@v4

libs/json-api/json-api-nestjs/project.json

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,51 @@
55
"projectType": "library",
66
"targets": {
77
"build": {
8+
"executor": "nx:run-commands",
9+
"dependsOn": [
10+
{
11+
"target": "build-mjs"
12+
},
13+
{
14+
"target": "build-cjs"
15+
}
16+
],
17+
"options": {
18+
"outputPath": "dist/libs/json-api/json-api-nestjs",
19+
"commands": [
20+
"node tools/scripts/preparation-hybrid-npm-package.mjs json-api-nestjs",
21+
"node tools/scripts/preparation-npm-package.mjs json-api-nestjs"
22+
],
23+
"cwd": "./",
24+
"parallel": false
25+
}
26+
},
27+
"build-cjs": {
828
"executor": "@nx/js:tsc",
929
"outputs": ["{options.outputPath}"],
1030
"options": {
11-
"outputPath": "dist/libs/json-api/json-api-nestjs",
31+
"outputPath": "dist/libs/json-api/json-api-nestjs/cjs",
32+
"main": "libs/json-api/json-api-nestjs/src/index.ts",
1233
"tsConfig": "libs/json-api/json-api-nestjs/tsconfig.lib.json",
13-
"packageJson": "libs/json-api/json-api-nestjs/package.json",
34+
"assets": ["libs/json-api/json-api-nestjs/README.md"],
35+
"external": "none",
36+
"updateBuildableProjectDepsInPackageJson": true,
37+
"buildableProjectDepsInPackageJsonType": "peerDependencies",
38+
"generateExportsField": true
39+
}
40+
},
41+
"build-mjs": {
42+
"executor": "@nx/js:tsc",
43+
"outputs": ["{options.outputPath}"],
44+
"options": {
45+
"outputPath": "dist/libs/json-api/json-api-nestjs/mjs",
1446
"main": "libs/json-api/json-api-nestjs/src/index.ts",
15-
"assets": ["libs/json-api/json-api-nestjs/*.md"],
47+
"tsConfig": "libs/json-api/json-api-nestjs/tsconfig-mjs.lib.json",
48+
"assets": ["libs/json-api/json-api-nestjs/README.md"],
1649
"external": "none",
1750
"updateBuildableProjectDepsInPackageJson": true,
18-
"buildableProjectDepsInPackageJsonType": "peerDependencies"
51+
"buildableProjectDepsInPackageJsonType": "peerDependencies",
52+
"generateExportsField": true
1953
}
2054
},
2155
"publish": {

libs/json-api/json-api-nestjs/src/lib/helper/bind-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function bindController(
4242
if (!allowMethod.includes(name)) continue;
4343
}
4444

45-
if (!controller.prototype.hasOwnProperty(name)) {
45+
if (!Object.prototype.hasOwnProperty.call(controller, name)) {
4646
// need uniq descriptor for correct work swagger
4747
Reflect.defineProperty(controller.prototype, name, {
4848
value: function (

libs/json-api/json-api-nestjs/src/lib/helper/create-controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export function createController(
2727
const entityName =
2828
entity instanceof Function ? entity.name : entity.options.name;
2929

30-
if (!JsonBaseController.isPrototypeOf(controllerClass)) {
30+
if (
31+
!Object.prototype.isPrototypeOf.call(JsonBaseController, controllerClass)
32+
) {
3133
throw new Error(
3234
`Controller "${controller?.name}" should be inherited of "JsonBaseController"`
3335
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node"],
5+
"module": "es2015",
6+
"target": "ES2022",
7+
"removeComments": false,
8+
"declaration": true,
9+
"declarationMap": true
10+
},
11+
"include": ["src/**/*.ts"],
12+
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
13+
}

tools/scripts/preparation-hybrid-npm-package.mjs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ mjsJson.exports['.'] = {
6060
es2015: mjsJson.es2015,
6161
default: mjsJson.es2015,
6262
}
63-
64-
mjsJson.exports[angularModule] = {
65-
types: './mjs/src/json-api-nestjs-sdk.module.d.ts',
66-
node: addTypeToPath(angularPath, 'cjs'),
67-
require: addTypeToPath(angularPath, 'cjs'),
68-
es2015: addTypeToPath(angularPath),
69-
default: addTypeToPath(angularPath),
63+
if (mjsJson.namne === 'json-api-nestjs-sdk') {
64+
mjsJson.exports[angularModule] = {
65+
types: './mjs/src/json-api-nestjs-sdk.module.d.ts',
66+
node: addTypeToPath(angularPath, 'cjs'),
67+
require: addTypeToPath(angularPath, 'cjs'),
68+
es2015: addTypeToPath(angularPath),
69+
default: addTypeToPath(angularPath),
70+
}
7071
}
7172

73+
7274
writeFileSync(`package.json`, JSON.stringify(mjsJson, null, 2));
7375
writeFileSync(
7476
'README.md',

0 commit comments

Comments
 (0)