diff --git a/packages/nx-webpack-plugin/.eslintrc.json b/packages/nx-webpack-plugin/.eslintrc.json new file mode 100644 index 000000000..f0f8c2dc4 --- /dev/null +++ b/packages/nx-webpack-plugin/.eslintrc.json @@ -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" + } + } + ] +} diff --git a/packages/nx-webpack-plugin/README.md b/packages/nx-webpack-plugin/README.md new file mode 100644 index 000000000..d2303f4d3 --- /dev/null +++ b/packages/nx-webpack-plugin/README.md @@ -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). diff --git a/packages/nx-webpack-plugin/jest.config.ts b/packages/nx-webpack-plugin/jest.config.ts new file mode 100644 index 000000000..7b263a484 --- /dev/null +++ b/packages/nx-webpack-plugin/jest.config.ts @@ -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: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../coverage/packages/nx-webpack-plugin', +} as Config; diff --git a/packages/nx-webpack-plugin/package.json b/packages/nx-webpack-plugin/package.json new file mode 100644 index 000000000..949ec6169 --- /dev/null +++ b/packages/nx-webpack-plugin/package.json @@ -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" + } +} diff --git a/packages/nx-webpack-plugin/project.json b/packages/nx-webpack-plugin/project.json new file mode 100644 index 000000000..e6c21ea03 --- /dev/null +++ b/packages/nx-webpack-plugin/project.json @@ -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"] +} diff --git a/packages/nx-webpack-plugin/src/index.ts b/packages/nx-webpack-plugin/src/index.ts new file mode 100644 index 000000000..aabfed313 --- /dev/null +++ b/packages/nx-webpack-plugin/src/index.ts @@ -0,0 +1 @@ +export * from './lib/with-deepkit'; diff --git a/packages/nx-webpack-plugin/src/lib/with-deepkit.ts b/packages/nx-webpack-plugin/src/lib/with-deepkit.ts new file mode 100644 index 000000000..a3d28a282 --- /dev/null +++ b/packages/nx-webpack-plugin/src/lib/with-deepkit.ts @@ -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; + }; +} diff --git a/packages/nx-webpack-plugin/tsconfig.json b/packages/nx-webpack-plugin/tsconfig.json new file mode 100644 index 000000000..4a955f678 --- /dev/null +++ b/packages/nx-webpack-plugin/tsconfig.json @@ -0,0 +1,17 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "module": "commonjs" + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ], + "reflection": true +} diff --git a/packages/nx-webpack-plugin/tsconfig.lib.json b/packages/nx-webpack-plugin/tsconfig.lib.json new file mode 100644 index 000000000..ae507b2d8 --- /dev/null +++ b/packages/nx-webpack-plugin/tsconfig.lib.json @@ -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 +} diff --git a/packages/nx-webpack-plugin/tsconfig.spec.json b/packages/nx-webpack-plugin/tsconfig.spec.json new file mode 100644 index 000000000..0a3e02d10 --- /dev/null +++ b/packages/nx-webpack-plugin/tsconfig.spec.json @@ -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 +} diff --git a/tsconfig.base.json b/tsconfig.base.json index a91e53fb7..4c8ebe310 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -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"],