1
+ /**
2
+ * @license
3
+ * Copyright Google Inc. All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import { absoluteFrom , getFileSystem } from '@angular/compiler-cli/src/ngtsc/file_system' ;
9
+
10
+ import { runInEachFileSystem } from '../../../src/ngtsc/file_system/testing' ;
11
+ import { getBasePaths } from '../../src/entry_point_finder/utils' ;
12
+ import { MockLogger } from '../helpers/mock_logger' ;
13
+
14
+ runInEachFileSystem ( ( ) => {
15
+ let _ : typeof absoluteFrom ;
16
+ let logger : MockLogger ;
17
+
18
+ beforeEach ( ( ) => {
19
+ _ = absoluteFrom ;
20
+ logger = new MockLogger ( ) ;
21
+ } ) ;
22
+
23
+ describe ( 'getBasePaths' , ( ) => {
24
+ it ( 'should just return the `sourceDirectory if there are no `pathMappings' , ( ) => {
25
+ const sourceDirectory = _ ( '/path/to/project/node_modules' ) ;
26
+ const basePaths = getBasePaths ( logger , sourceDirectory , undefined ) ;
27
+ expect ( basePaths ) . toEqual ( [ sourceDirectory ] ) ;
28
+ } ) ;
29
+
30
+ it ( 'should use each path mapping prefix and sort in descending order' , ( ) => {
31
+ const projectDirectory = _ ( '/path/to/project' ) ;
32
+ const fs = getFileSystem ( ) ;
33
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'dist-1' ) ) ;
34
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'sub-folder/dist-2' ) ) ;
35
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'libs' ) ) ;
36
+
37
+ const sourceDirectory = _ ( '/path/to/project/node_modules' ) ;
38
+ const pathMappings = {
39
+ baseUrl : projectDirectory ,
40
+ paths : { '@dist' : [ 'dist-1' , 'sub-folder/dist-2' ] , '@lib/*' : [ 'libs/*' ] }
41
+ } ;
42
+ const basePaths = getBasePaths ( logger , sourceDirectory , pathMappings ) ;
43
+ expect ( basePaths ) . toEqual ( [
44
+ fs . resolve ( projectDirectory , 'sub-folder/dist-2' ) ,
45
+ sourceDirectory ,
46
+ fs . resolve ( projectDirectory , 'libs' ) ,
47
+ fs . resolve ( projectDirectory , 'dist-1' ) ,
48
+ ] ) ;
49
+ } ) ;
50
+
51
+ it ( 'should discard paths that are already contained by another path' , ( ) => {
52
+ const projectDirectory = _ ( '/path/to/project' ) ;
53
+ const fs = getFileSystem ( ) ;
54
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'dist-1' ) ) ;
55
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'dist-1/sub-folder' ) ) ;
56
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'node_modules/libs' ) ) ;
57
+
58
+ const sourceDirectory = _ ( '/path/to/project/node_modules' ) ;
59
+ const pathMappings = {
60
+ baseUrl : projectDirectory ,
61
+ paths : { '@dist' : [ 'dist-1' , 'dist-1/sub-folder' ] , '@lib/*' : [ 'node_modules/libs/*' ] }
62
+ } ;
63
+ const basePaths = getBasePaths ( logger , sourceDirectory , pathMappings ) ;
64
+ expect ( basePaths ) . toEqual ( [
65
+ sourceDirectory ,
66
+ fs . resolve ( projectDirectory , 'dist-1' ) ,
67
+ ] ) ;
68
+ } ) ;
69
+
70
+ it ( 'should use the containing directory of path mapped files' , ( ) => {
71
+ const projectDirectory = _ ( '/path/to/project' ) ;
72
+ const fs = getFileSystem ( ) ;
73
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'dist-1' ) ) ;
74
+ fs . writeFile ( fs . resolve ( projectDirectory , 'dist-1/file.js' ) , 'dummy content' ) ;
75
+
76
+ const sourceDirectory = _ ( '/path/to/project/node_modules' ) ;
77
+ const pathMappings = { baseUrl : projectDirectory , paths : { '@dist' : [ 'dist-1/file.js' ] } } ;
78
+ const basePaths = getBasePaths ( logger , sourceDirectory , pathMappings ) ;
79
+ expect ( basePaths ) . toEqual ( [
80
+ sourceDirectory ,
81
+ fs . resolve ( projectDirectory , 'dist-1' ) ,
82
+ ] ) ;
83
+ } ) ;
84
+
85
+ it ( 'should always include the `sourceDirectory` if it is a node_modules directory in the returned basePaths, even if it is contained by another basePath' ,
86
+ ( ) => {
87
+ const projectDirectory = _ ( '/path/to/project' ) ;
88
+ const sourceDirectory = _ ( '/path/to/project/node_modules' ) ;
89
+ const fs = getFileSystem ( ) ;
90
+ fs . ensureDir ( fs . resolve ( sourceDirectory ) ) ;
91
+
92
+ const pathMappings = { baseUrl : projectDirectory , paths : { '*' : [ './*' ] } } ;
93
+ const basePaths = getBasePaths ( logger , sourceDirectory , pathMappings ) ;
94
+ expect ( basePaths ) . toEqual ( [
95
+ sourceDirectory ,
96
+ projectDirectory ,
97
+ ] ) ;
98
+ } ) ;
99
+
100
+ it ( 'should log a warning if baseUrl is the root path' , ( ) => {
101
+ const fs = getFileSystem ( ) ;
102
+ fs . ensureDir ( fs . resolve ( '/dist' ) ) ;
103
+
104
+ const sourceDirectory = _ ( '/path/to/project/node_modules' ) ;
105
+ const pathMappings = { baseUrl : _ ( '/' ) , paths : { '@dist' : [ 'dist' ] } } ;
106
+ const basePaths = getBasePaths ( logger , sourceDirectory , pathMappings ) ;
107
+ expect ( basePaths ) . toEqual ( [
108
+ sourceDirectory ,
109
+ fs . resolve ( '/dist' ) ,
110
+ ] ) ;
111
+ expect ( logger . logs . warn ) . toEqual ( [
112
+ [ `The provided pathMappings baseUrl is the root path ${ _ ( '/' ) } .\n` +
113
+ `This is likely to mess up how ngcc finds entry-points and is probably not correct.\n` +
114
+ `Please check your path mappings configuration such as in the tsconfig.json file.` ]
115
+ ] ) ;
116
+ } ) ;
117
+
118
+ it ( 'should discard basePaths that do not exists and log a warning' , ( ) => {
119
+ const projectDirectory = _ ( '/path/to/project' ) ;
120
+ const fs = getFileSystem ( ) ;
121
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'dist-1' ) ) ;
122
+ fs . ensureDir ( fs . resolve ( projectDirectory , 'sub-folder' ) ) ;
123
+
124
+ const sourceDirectory = _ ( '/path/to/project/node_modules' ) ;
125
+ const pathMappings = {
126
+ baseUrl : projectDirectory ,
127
+ paths : { '@dist' : [ 'dist-1' , 'sub-folder/dist-2' ] , '@lib/*' : [ 'libs/*' ] }
128
+ } ;
129
+ const basePaths = getBasePaths ( logger , sourceDirectory , pathMappings ) ;
130
+ expect ( basePaths ) . toEqual ( [
131
+ sourceDirectory ,
132
+ fs . resolve ( projectDirectory , 'dist-1' ) ,
133
+ ] ) ;
134
+ expect ( logger . logs . warn ) . toEqual ( [
135
+ [ `The basePath "${ fs . resolve ( projectDirectory , 'sub-folder/dist-2' ) } " computed from baseUrl "${ projectDirectory } " and path mapping "sub-folder/dist-2" does not exist in the file-system.\n` +
136
+ `It will not be scanned for entry-points.` ] ,
137
+ [ `The basePath "${ fs . resolve ( projectDirectory , 'libs' ) } " computed from baseUrl "${ projectDirectory } " and path mapping "libs/*" does not exist in the file-system.\n` +
138
+ `It will not be scanned for entry-points.` ] ,
139
+ ] ) ;
140
+ } ) ;
141
+ } ) ;
142
+ } ) ;
0 commit comments