Skip to content

Commit

Permalink
feat: impl ts resolve alias plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Jun 1, 2023
1 parent 4a2764e commit 2fc5c2f
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 40 deletions.
144 changes: 107 additions & 37 deletions packages/pack/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions packages/pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
"description": "A tool for building and prepare components for publishing",
"homepage": "https://github.com/bem/bem-react/tree/master/packages/pack",
"repository": "https://github.com/bem/bem-react",
"keywords": ["bem", "build", "react"],
"keywords": [
"bem",
"build",
"react"
],
"bin": {
"pack": "bin/pack"
},
"publishConfig": {
"access": "public"
},
"files": ["bin", "lib"],
"files": [
"bin",
"lib"
],
"license": "MPL-2.0",
"scripts": {
"build": "tsc",
Expand All @@ -26,7 +33,8 @@
"debug": "4.1.1",
"fast-glob": "3.2.5",
"fs-extra": "9.0.1",
"log-update": "4.0.0"
"log-update": "4.0.0",
"tsc-alias": "1.8.6"
},
"devDependencies": {
"@babel/cli": "7.14.8",
Expand Down
52 changes: 52 additions & 0 deletions packages/pack/src/plugins/TypeScriptResolvePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { exec } from 'child_process'
import { promisify } from 'util'
import { resolve } from 'path'
import { existsSync } from 'fs-extra'

import { Plugin, OnDone, HookOptions } from '../interfaces'
import { mark } from '../debug'

const execAsync = promisify(exec)

type Options = {
/**
* A path to typescript config.
*/
configPath?: string
}

class TypeScriptResolvePlugin implements Plugin {
name = 'TypescriptResolvePlugin'

constructor(public options: Options = {} as Options) {
mark('TypescriptResolvePlugin::constructor')
}

async onAfterRun(done: OnDone, { context }: HookOptions) {
mark('TypescriptResolvePlugin::onRun(start)')
const configPath = this.getConfigPath(context)

try {
await execAsync(`tsc-alias -p ${configPath}`)
} catch (error) {
throw new Error((error as any).stdout)
}

mark('TypescriptResolvePlugin::onRun(finish)')
done()
}

private getConfigPath(context: string) {
const configPath = resolve(context, this.options.configPath || 'tsconfig.json')

if (!existsSync(configPath)) {
throw new Error('Cannot find tsconfig.')
}

return configPath
}
}

export function useTypeScriptResolvePlugin(options: Options) {
return new TypeScriptResolvePlugin(options)
}

0 comments on commit 2fc5c2f

Please sign in to comment.