Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resolve tsconfig paths #241

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/Serverless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ declare namespace Serverless {
getAllLayers(): string[]
custom?: {
serverlessPluginTypescript?: {
tsConfigFileLocation: string
tsConfigFileLocation: string,
paths?: boolean
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 9 additions & 2 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -70,11 +71,17 @@ export function extractFileNames(cwd: string, provider: string, functions?: { [k
})
}

export async function run(fileNames: string[], options: ts.CompilerOptions): Promise<string[]> {
export async function run(fileNames: string[], options: ts.CompilerOptions, { paths = false }: { paths?: boolean } = {}): Promise<string[]> {
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)

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"rootDir": ".",
"target": "es6",
"sourceMap": true,
"outDir": "dist"
"outDir": "dist",
"esModuleInterop": true
},
"exclude": [
"node_modules"
Expand Down