Skip to content

Commit 2e2554b

Browse files
committed
refactor: generate a shared library as a client/server dependency
Generate a new nx:js library that contains shared models for client/server packages Update webpack config to resolve paths from root tsconfig. Install dev dependency `tsconfig-paths-webpack-plugin` to facilitate this.
1 parent ed2e4e2 commit 2e2554b

18 files changed

+190
-7
lines changed

nx.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependsOn": ["^build"]
2222
},
2323
"watch": {
24-
"dependsOn": ["^build"]
24+
"dependsOn": ["^watch"]
2525
}
2626
},
2727
"workspaceLayout": {
@@ -40,9 +40,5 @@
4040
"unitTestRunner": "jest"
4141
}
4242
},
43-
"pluginsConfig": {
44-
"@nrwl/js": {
45-
"analyzeSourceFiles": true
46-
}
47-
}
43+
"pluginsConfig": {}
4844
}

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"ts-jest": "28.0.5",
126126
"ts-loader": "^9.4.2",
127127
"ts-node": "10.9.1",
128+
"tsconfig-paths-webpack-plugin": "^4.0.0",
128129
"typescript": "^4.9.4"
129130
},
130131
"scripts": {

packages/client/webpack.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { composePlugins, withNx, withWeb } = require('@nrwl/webpack');
22

33
const path = require('path');
4+
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
45

56
// Nx plugins for webpack.
67
module.exports = composePlugins(withNx(), withWeb(), (config) => {
@@ -14,6 +15,11 @@ module.exports = composePlugins(withNx(), withWeb(), (config) => {
1415
};
1516

1617
config.resolve = {
18+
plugins: [
19+
new TsconfigPathsPlugin({
20+
configFile: path.join(__dirname, 'tsconfig.app.json'),
21+
}),
22+
],
1723
mainFields: ['module', 'main'],
1824
extensions: ['.ts', '.js'], // support ts-files and js-files
1925
};

packages/server/webpack.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { composePlugins, withNx, withWeb } = require('@nrwl/webpack');
22

33
const path = require('path');
4+
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
45

56
// Nx plugins for webpack.
67
module.exports = composePlugins(withNx(), withWeb(), (config) => {
@@ -13,6 +14,11 @@ module.exports = composePlugins(withNx(), withWeb(), (config) => {
1314
};
1415

1516
config.resolve = {
17+
plugins: [
18+
new TsconfigPathsPlugin({
19+
configFile: path.join(__dirname, 'tsconfig.app.json'),
20+
}),
21+
],
1622
mainFields: ['module', 'main'],
1723
extensions: ['.ts', '.js'], // support ts-files and js-files
1824
};

packages/shared/.babelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": [
3+
[
4+
"@nrwl/js/babel",
5+
{
6+
"useBuiltIns": "usage"
7+
}
8+
]
9+
]
10+
}

packages/shared/.eslintrc.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"parserOptions": {
8+
"project": ["packages/shared/tsconfig.*?.json"]
9+
},
10+
"rules": {}
11+
},
12+
{
13+
"files": ["*.ts", "*.tsx"],
14+
"rules": {}
15+
},
16+
{
17+
"files": ["*.js", "*.jsx"],
18+
"rules": {}
19+
}
20+
]
21+
}

packages/shared/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# shared
2+
3+
This library was generated with [Nx](https://nx.dev).
4+
5+
## Building
6+
7+
Run `nx build shared` to build the library.
8+
9+
## Running unit tests
10+
11+
Run `nx test shared` to execute the unit tests via [Jest](https://jestjs.io).

packages/shared/jest.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'shared',
4+
preset: '../../jest.preset.js',
5+
globals: {
6+
'ts-jest': {
7+
tsconfig: '<rootDir>/tsconfig.spec.json',
8+
},
9+
},
10+
transform: {
11+
'^.+\\.[tj]s$': 'ts-jest',
12+
},
13+
moduleFileExtensions: ['ts', 'js', 'html'],
14+
coverageDirectory: '../../coverage/packages/shared',
15+
};

packages/shared/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "shared",
3+
"version": "0.0.1",
4+
"type": "commonjs"
5+
}

0 commit comments

Comments
 (0)