diff --git a/README.md b/README.md index fd86b7bb..62383402 100644 --- a/README.md +++ b/README.md @@ -177,3 +177,13 @@ module.exports = { } ``` +### Resolve tsconfig paths + +You can enable `tsconfig` [paths](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) +resolution with the following option: + +```yml +custom: + serverlessPluginTypescript: + paths: true +``` diff --git a/package.json b/package.json index 850c51ce..fc02191b 100644 --- a/package.json +++ b/package.json @@ -58,13 +58,15 @@ "rimraf": "2.6.3", "standard-version": "^9.5.0", "ts-jest": "24.0.2", + "ts-node": "^10.9.1", "tslint": "5.14.0", "typescript": "^4.8.4" }, "dependencies": { "fs-extra": "^7.0.1", - "globby": "^10.0.2", - "lodash": "^4.17.21" + "globby": "^11.1.0", + "lodash": "^4.17.21", + "typescript-transform-paths": "^3.4.6" }, "peerDependencies": { "serverless": "2 || 3", diff --git a/src/Serverless.d.ts b/src/Serverless.d.ts index dade3099..e0d47d9a 100644 --- a/src/Serverless.d.ts +++ b/src/Serverless.d.ts @@ -22,7 +22,8 @@ declare namespace Serverless { getAllLayers(): string[] custom?: { serverlessPluginTypescript?: { - tsConfigFileLocation: string + tsConfigFileLocation: string, + paths?: boolean } } } diff --git a/src/index.ts b/src/index.ts index 90f95a2e..0a760052 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import * as path from 'path' import * as fs from 'fs-extra' import * as _ from 'lodash' -import * as globby from 'globby' +import globby from 'globby' import * as typescript from './typescript' import { watchFiles } from './watchFiles' @@ -187,7 +187,11 @@ export class TypeScriptPlugin { tsconfig.outDir = BUILD_FOLDER - const emitedFiles = await typescript.run(this.rootFileNames, tsconfig) + const emitedFiles = await typescript.run( + this.rootFileNames, + tsconfig, + { paths: this.serverless.service?.custom?.serverlessPluginTypescript?.paths ?? false } + ) this.serverless.cli.log('Typescript compiled.') return emitedFiles } diff --git a/src/typescript.ts b/src/typescript.ts index b3e37d25..8f2a9203 100644 --- a/src/typescript.ts +++ b/src/typescript.ts @@ -2,6 +2,7 @@ import * as ts from 'typescript' import * as fs from 'fs-extra' import * as _ from 'lodash' import * as path from 'path' +import transformPaths from 'typescript-transform-paths'; export function makeDefaultTypescriptConfig() { const defaultTypescriptConfig: ts.CompilerOptions = { @@ -70,11 +71,17 @@ export function extractFileNames(cwd: string, provider: string, functions?: { [k }) } -export async function run(fileNames: string[], options: ts.CompilerOptions): Promise { +export async function run(fileNames: string[], options: ts.CompilerOptions, { paths = false }: { paths?: boolean } = {}): Promise { options.listEmittedFiles = true const program = ts.createProgram(fileNames, options) - const emitResult = program.emit() + const emitResult = program.emit( + undefined, + undefined, + undefined, + false, + paths ? { before: [transformPaths(program, {}, { ts })] } : {} + ) const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics) diff --git a/tsconfig.json b/tsconfig.json index 4eb97c5d..b4bb3452 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,8 @@ "rootDir": ".", "target": "es6", "sourceMap": true, - "outDir": "dist" + "outDir": "dist", + "esModuleInterop": true }, "exclude": [ "node_modules"