Skip to content

Commit

Permalink
feat: add nx-webpack-plugin package
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa committed Sep 11, 2023
1 parent 48db3d6 commit 17ce109
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/nx-webpack-plugin/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"parserOptions": {
"project": ["packages/nx-webpack-plugin/tsconfig.*?.json"]
},
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
11 changes: 11 additions & 0 deletions packages/nx-webpack-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# nx-webpack-plugin

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build nx-webpack-plugin` to build the library.

## Running unit tests

Run `nx test nx-webpack-plugin` to execute the unit tests via [Jest](https://jestjs.io).
12 changes: 12 additions & 0 deletions packages/nx-webpack-plugin/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Config } from 'jest';

export default {
displayName: 'nx-webpack-plugin',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/nx-webpack-plugin',
} as Config;
20 changes: 20 additions & 0 deletions packages/nx-webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@deepkit/nx-webpack-plugin",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "https://github.com/marcus-sa/deepkit-community.git"
},
"dependencies": {
"tslib": "2.6.2",
"webpack": "5.88.2",
"ts-loader": "9.4.4"
},
"peerDependencies": {
"@deepkit/type-compiler": "*",
"@nx/webpack": "^16.7.4"
},
"publishConfig": {
"access": "public"
}
}
47 changes: 47 additions & 0 deletions packages/nx-webpack-plugin/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "nx-webpack-plugin",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/nx-webpack-plugin/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/{projectRoot}",
"tsConfig": "{projectRoot}/tsconfig.lib.json",
"packageJson": "{projectRoot}/package.json",
"main": "{projectRoot}/src/index.ts",
"assets": ["{projectRoot}/*.md"]
}
},
"release": {
"command": "release-it --config={projectRoot}/.release-it.json"
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"{projectRoot}/**/*.ts",
"{projectRoot}/package.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "{projectRoot}/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": ["nx-webpack-plugin"]
}
1 change: 1 addition & 0 deletions packages/nx-webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/with-deepkit';
50 changes: 50 additions & 0 deletions packages/nx-webpack-plugin/src/lib/with-deepkit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { NxWebpackPlugin } from '@nx/webpack';
import type { RuleSetRule } from 'webpack';
import type { LoaderOptions } from 'ts-loader/dist/interfaces';
import * as typeCompiler from '@deepkit/type-compiler';

interface TsLoaderRule extends RuleSetRule {
readonly options: LoaderOptions;
}

type GetCustomTransformers = Exclude<
LoaderOptions['getCustomTransformers'],
string
>;

function addDeepkitTransformer(
prevGetCustomTransformers: GetCustomTransformers,
): GetCustomTransformers {
return (program, getProgram) => {
const customTransformers = {
...prevGetCustomTransformers?.(program, getProgram),
};
customTransformers.before = [
typeCompiler.transformer,
...(customTransformers.before || []),
];
customTransformers.afterDeclarations = [
typeCompiler.declarationTransformer,
...(customTransformers.afterDeclarations || []),
];
return customTransformers;
};
}

export function withDeepkit(): NxWebpackPlugin {
return config => {
const rules = config.module!.rules! as readonly RuleSetRule[];

rules
.filter(
(rule): rule is TsLoaderRule => !!rule.loader?.includes('ts-loader'),
)
.forEach(tsRule => {
tsRule.options!.getCustomTransformers = addDeepkitTransformer(
tsRule.options.getCustomTransformers as GetCustomTransformers,
);
});

return config;
};
}
17 changes: 17 additions & 0 deletions packages/nx-webpack-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"reflection": true
}
12 changes: 12 additions & 0 deletions packages/nx-webpack-plugin/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"],
"reflection": true
}
15 changes: 15 additions & 0 deletions packages/nx-webpack-plugin/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
],
"reflection": true
}
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@deepkit/logger": ["packages/logger/src/index.ts"],
"@deepkit/mongo": ["packages/mongo/src/index.ts"],
"@deepkit/mysql": ["packages/mysql/src/index.ts"],
"@deepkit/nx-webpack-plugin": ["packages/nx-webpack-plugin/src/index.ts"],
"@deepkit/orm": ["packages/orm/src/index.ts"],
"@deepkit/orm-browser": ["packages/orm-browser/src/index.ts"],
"@deepkit/orm-browser-api": ["packages/orm-browser-api/src/index.ts"],
Expand Down

0 comments on commit 17ce109

Please sign in to comment.