@@ -16,62 +16,41 @@ export interface TSConfig {
16
16
export interface CompilerOptions {
17
17
baseUrl ?: string ;
18
18
paths : { [ key : string ] : string [ ] | undefined ; } ;
19
+ cwd ?: string ;
19
20
}
20
21
21
22
export interface PluginOptions {
22
23
configuration : TSConfig | CompilerOptions ;
24
+ cwd ?: string ;
23
25
}
24
26
25
27
export type AliasPlugin = ( pluginOptions : PluginOptions ) => any ;
26
28
27
29
const COMMENTED_PATTERN = / ( \/ \* (?: (? ! \* \/ ) .| [ \n \r ] ) * \* \/ ) | ( \/ \/ [ ^ \n \r ] * (?: [ \n \r ] + | $ ) ) / ;
30
+ const IMPORT_PATTERNS = [ / f r o m ( [ " ' ] ) ( .* ?) \1/ , / i m p o r t \( ( [ " ' ] ) ( .* ?) \1\) / , / r e q u i r e \( ( [ " ' ] ) ( .* ?) \1\) / ] ;
28
31
29
32
function parseImports ( file : ReadonlyArray < string > , dir : string ) : FileData [ ] {
30
- const results = file . map ( ( line : string , index : number ) => {
31
- const imports = findImport ( line ) ;
32
-
33
- if ( imports === null ) {
34
- return null ;
35
- }
36
-
37
- return {
38
- path : dir ,
39
- index,
40
- import : imports ,
41
- } ;
42
- } ) ;
43
-
44
- return results . filter ( ( value : { path : string ; index : number ; import : string ; } | null ) : value is FileData => {
45
- return value !== null && value !== undefined ;
46
- } ) ;
33
+ return file
34
+ . map ( ( line , index ) => findImports ( line )
35
+ . map ( ( i ) => ( { path : dir , index, import : i } ) ) ,
36
+ )
37
+ . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] ) ;
47
38
}
48
39
49
- function findImport ( line : string ) : string | null {
50
- const matches = line . match ( / f r o m ( [ " ' ] ) ( .* ?) \1/ ) || line . match ( / i m p o r t \( ( [ " ' ] ) ( .* ?) \1\) / ) || line . match ( / r e q u i r e \( ( [ " ' ] ) ( .* ?) \1\) / ) ;
51
-
52
- if ( ! matches ) {
53
- return null ;
54
- }
55
-
40
+ function findImports ( line : string ) : string [ ] | null {
56
41
if ( line . match ( COMMENTED_PATTERN ) ) {
57
- return null ;
58
- }
59
-
60
- const multiple = [ / f r o m ( [ " ' ] ) ( .* ?) \1/ g, / i m p o r t \( ( [ " ' ] ) ( .* ?) \1\) / g, / r e q u i r e \( ( [ " ' ] ) ( .* ?) \1\) / g] . some ( ( exp ) => {
61
- const results = line . match ( exp ) ;
62
-
63
- return results && results . length > 1 ;
64
- } ) ;
65
-
66
- if ( multiple ) {
67
- throw new Error ( 'Multiple imports on the same line are currently not supported!' ) ;
42
+ return [ ] ;
68
43
}
69
44
70
- return matches [ 2 ] ;
45
+ return IMPORT_PATTERNS
46
+ . map ( ( pattern ) => line . match ( RegExp ( pattern , 'g' ) ) )
47
+ . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] )
48
+ . filter ( ( value ) : value is any => value !== null )
49
+ . map ( ( match ) => IMPORT_PATTERNS . reduce ( ( matched , pattern ) => matched || match . match ( pattern ) , null ) [ 2 ] ) ;
71
50
}
72
51
73
52
function resolveImports ( file : ReadonlyArray < string > , imports : FileData [ ] , options : CompilerOptions ) : string [ ] {
74
- const { baseUrl, paths } = options ;
53
+ const { baseUrl, paths, cwd } = options ;
75
54
76
55
const aliases : { [ key : string ] : string [ ] | undefined } = { } ;
77
56
for ( const alias in paths ) {
@@ -113,9 +92,11 @@ function resolveImports(file: ReadonlyArray<string>, imports: FileData[], option
113
92
continue ;
114
93
}
115
94
116
- let relative = path . relative ( path . dirname ( imported . path ) , baseUrl || './' ) ;
95
+ const dirname = path . dirname ( imported . path ) ;
96
+ let relative = path . join ( path . resolve ( baseUrl || './' ) , cwd ) ;
97
+ relative = path . relative ( dirname , relative ) ;
117
98
relative = path . join ( relative , resolved ) ;
118
- relative = path . relative ( path . dirname ( imported . path ) , path . resolve ( path . dirname ( imported . path ) , relative ) ) ;
99
+ relative = path . relative ( dirname , path . join ( dirname , relative ) ) ;
119
100
relative = relative . replace ( / \\ / g, '/' ) ;
120
101
121
102
if ( relative . length === 0 || ! relative . startsWith ( '.' ) ) {
@@ -145,6 +126,12 @@ const aliasPlugin: AliasPlugin = (pluginOptions: PluginOptions) => {
145
126
compilerOptions . baseUrl = './' ;
146
127
}
147
128
129
+ if ( pluginOptions . cwd === undefined || pluginOptions . cwd === '.' ) {
130
+ compilerOptions . cwd = './' ;
131
+ } else {
132
+ compilerOptions . cwd = pluginOptions . cwd ;
133
+ }
134
+
148
135
return ObjectStream . transform ( {
149
136
onEntered : ( args : EnteredArgs < File , File > ) => {
150
137
const file = args . object ;
0 commit comments