1
- import path from " path"
2
- import { TransformCallback , Transform } from " stream"
3
- import ts from " typescript"
1
+ import path from ' path'
2
+ import { TransformCallback , Transform } from ' stream'
3
+ import ts from ' typescript'
4
4
5
- import File = require( " vinyl" )
5
+ import File = require( ' vinyl' )
6
6
7
7
export type CompilerOptions = ts . CompilerOptions
8
8
@@ -19,8 +19,7 @@ export interface PluginOptions {
19
19
20
20
export type AliasPlugin = ( pluginOptions : PluginOptions ) => Transform
21
21
22
- const COMMENTED_PATTERN =
23
- / ( \/ \* (?: (? ! \* \/ ) .| [ \n \r ] ) * \* \/ ) | ( \/ \/ [ ^ \n \r ] * (?: [ \n \r ] + | $ ) ) /
22
+ const COMMENTED_PATTERN = / ( \/ \* (?: (? ! \* \/ ) .| [ \n \r ] ) * \* \/ ) | ( \/ \/ [ ^ \n \r ] * (?: [ \n \r ] + | $ ) ) /
24
23
const IMPORT_PATTERNS = [
25
24
/ f r o m ( [ " ' ] ) ( .* ?) \1/ g,
26
25
/ i m p o r t \( ( [ " ' ] ) ( .* ?) \1\) / g,
@@ -35,11 +34,9 @@ function parseImports(file: ReadonlyArray<string>, dir: string): FileData[] {
35
34
}
36
35
37
36
function findImports ( line : string ) : string [ ] | null {
38
- line = line . replace ( COMMENTED_PATTERN , "" )
37
+ line = line . replace ( COMMENTED_PATTERN , '' )
39
38
40
- return IMPORT_PATTERNS . flatMap ( ( pattern ) =>
41
- [ ...line . matchAll ( pattern ) ] . map ( ( match ) => match [ 2 ] )
42
- )
39
+ return IMPORT_PATTERNS . flatMap ( ( pattern ) => [ ...line . matchAll ( pattern ) ] . map ( ( match ) => match [ 2 ] ) )
43
40
}
44
41
45
42
function resolveImports (
@@ -54,8 +51,8 @@ function resolveImports(
54
51
/* istanbul ignore else */
55
52
if ( paths . hasOwnProperty ( alias ) ) {
56
53
let resolved = alias
57
- if ( alias . endsWith ( "/*" ) ) {
58
- resolved = alias . replace ( "/*" , "/" )
54
+ if ( alias . endsWith ( '/*' ) ) {
55
+ resolved = alias . replace ( '/*' , '/' )
59
56
}
60
57
61
58
aliases [ resolved ] = paths [ alias ]
@@ -66,16 +63,16 @@ function resolveImports(
66
63
for ( const imported of imports ) {
67
64
const line = file [ imported . index ]
68
65
69
- let resolved = ""
66
+ let resolved = ''
70
67
for ( const alias in aliases ) {
71
68
/* istanbul ignore else */
72
69
if ( aliases . hasOwnProperty ( alias ) && imported . import . startsWith ( alias ) ) {
73
70
const choices : string [ ] | undefined = aliases [ alias ]
74
71
75
72
if ( choices !== undefined ) {
76
73
resolved = choices [ 0 ]
77
- if ( resolved . endsWith ( "/*" ) ) {
78
- resolved = resolved . replace ( "/*" , "/" )
74
+ if ( resolved . endsWith ( '/*' ) ) {
75
+ resolved = resolved . replace ( '/*' , '/' )
79
76
}
80
77
81
78
resolved = imported . import . replace ( alias , resolved )
@@ -89,32 +86,24 @@ function resolveImports(
89
86
continue
90
87
}
91
88
92
- const dirname = path . dirname ( imported . path )
93
- let relative = path . join ( path . resolve ( baseUrl || "./" ) , cwd as string )
94
- relative = path . relative ( dirname , relative )
95
- relative = path . join ( relative , resolved )
96
- relative = path . relative ( dirname , path . join ( dirname , relative ) )
97
- relative = relative . replace ( / \\ / g, "/" )
89
+ const base = path . join ( cwd as string , path . relative ( cwd as string , baseUrl || './' ) )
90
+ const current = path . relative ( base , path . dirname ( imported . path ) )
91
+ const target = path . relative ( base , resolved )
98
92
99
- // if (relative.length === 0 || !relative.startsWith(".")) {
100
- // relative = "./" + relative
101
- // }
93
+ const relative = path . relative ( current , target ) . replace ( / \\ / g, '/' )
102
94
103
95
lines [ imported . index ] = line . replace ( imported . import , relative )
104
96
}
105
97
106
98
return lines
107
99
}
108
100
109
- function resolveConfig (
110
- config ?: string | ts . CompilerOptions ,
111
- cwd ?: string
112
- ) : ts . CompilerOptions {
101
+ function resolveConfig ( config ?: string | ts . CompilerOptions , cwd ?: string ) : ts . CompilerOptions {
113
102
if ( ! config ) {
114
103
let configPath : string | undefined
115
104
116
105
/* istanbul ignore if */
117
- if ( process . env . NODE_ENV !== " test" ) {
106
+ if ( process . env . NODE_ENV !== ' test' ) {
118
107
configPath = ts . findConfigFile ( cwd , ts . sys . fileExists )
119
108
}
120
109
@@ -123,22 +112,14 @@ function resolveConfig(
123
112
}
124
113
125
114
const configFile = ts . readConfigFile ( configPath , ts . sys . readFile )
126
- const { options } = ts . parseJsonConfigFileContent (
127
- configFile . config ,
128
- ts . sys ,
129
- cwd
130
- )
115
+ const { options } = ts . parseJsonConfigFileContent ( configFile . config , ts . sys , cwd )
131
116
132
117
return options
133
118
}
134
119
135
- if ( typeof config === " string" ) {
120
+ if ( typeof config === ' string' ) {
136
121
const configFile = ts . readConfigFile ( config , ts . sys . readFile )
137
- const { options } = ts . parseJsonConfigFileContent (
138
- configFile . config ,
139
- ts . sys ,
140
- cwd
141
- )
122
+ const { options } = ts . parseJsonConfigFileContent ( configFile . config , ts . sys , cwd )
142
123
143
124
return options
144
125
}
@@ -147,35 +128,26 @@ function resolveConfig(
147
128
}
148
129
149
130
const alias : AliasPlugin = ( { config, cwd } : PluginOptions ) => {
150
- cwd = cwd === undefined ? process . cwd ( ) : cwd === "." ? "./" : cwd
131
+ cwd = cwd === undefined ? process . cwd ( ) : cwd === '.' ? './' : cwd
151
132
152
133
const compilerOptions = resolveConfig ( config , cwd )
153
134
154
135
if ( ! compilerOptions . paths ) {
155
- throw new Error (
156
- "Unable to find the 'paths' property in the supplied configuration!"
157
- )
136
+ throw new Error ( "Unable to find the 'paths' property in the supplied configuration!" )
158
137
}
159
138
160
- if (
161
- compilerOptions . baseUrl === undefined ||
162
- compilerOptions . baseUrl === "."
163
- ) {
164
- compilerOptions . baseUrl = "./"
139
+ if ( compilerOptions . baseUrl === undefined || compilerOptions . baseUrl === '.' ) {
140
+ compilerOptions . baseUrl = './'
165
141
}
166
142
167
143
compilerOptions . cwd = cwd
168
144
169
145
return new Transform ( {
170
146
objectMode : true ,
171
- transform (
172
- file : File ,
173
- encoding : BufferEncoding ,
174
- callback : TransformCallback
175
- ) {
147
+ transform ( file : File , encoding : BufferEncoding , callback : TransformCallback ) {
176
148
/* istanbul ignore if */
177
149
if ( file . isStream ( ) ) {
178
- return callback ( new Error ( " Streaming is not supported." ) )
150
+ return callback ( new Error ( ' Streaming is not supported.' ) )
179
151
}
180
152
181
153
if ( file . isNull ( ) || ! file . contents ) {
@@ -184,13 +156,11 @@ const alias: AliasPlugin = ({ config, cwd }: PluginOptions) => {
184
156
185
157
if ( ! file . path ) {
186
158
return callback (
187
- new Error (
188
- "Received file with no path. Files must have path to be resolved."
189
- )
159
+ new Error ( 'Received file with no path. Files must have path to be resolved.' )
190
160
)
191
161
}
192
162
193
- const lines = file . contents . toString ( ) . split ( "\n" )
163
+ const lines = file . contents . toString ( ) . split ( '\n' )
194
164
const imports = parseImports ( lines , file . path )
195
165
196
166
if ( imports . length === 0 ) {
@@ -199,7 +169,7 @@ const alias: AliasPlugin = ({ config, cwd }: PluginOptions) => {
199
169
200
170
const resolved = resolveImports ( lines , imports , compilerOptions )
201
171
202
- file . contents = Buffer . from ( resolved . join ( "\n" ) )
172
+ file . contents = Buffer . from ( resolved . join ( '\n' ) )
203
173
204
174
callback ( undefined , file )
205
175
} ,
0 commit comments