2018-08-09 10:59:10 -04:00
/ * *
* @license
* Copyright Google Inc . All Rights Reserved .
*
* Use of this source code is governed by an MIT - style license that can be
* found in the LICENSE file at https : //angular.io/license
* /
2019-08-29 11:47:54 -04:00
import { DepGraph } from 'dependency-graph' ;
2020-02-25 10:51:54 -05:00
import { AbsoluteFsPath , FileSystem , absoluteFrom , getFileSystem , relativeFrom } from '../../../src/ngtsc/file_system' ;
2019-06-06 15:22:32 -04:00
import { runInEachFileSystem } from '../../../src/ngtsc/file_system/testing' ;
2019-12-19 17:43:12 -05:00
import { DependencyInfo } from '../../src/dependencies/dependency_host' ;
2019-04-28 15:47:57 -04:00
import { DependencyResolver , SortedEntryPointsInfo } from '../../src/dependencies/dependency_resolver' ;
2020-01-08 12:10:25 -05:00
import { DtsDependencyHost } from '../../src/dependencies/dts_dependency_host' ;
2019-04-28 15:47:57 -04:00
import { EsmDependencyHost } from '../../src/dependencies/esm_dependency_host' ;
import { ModuleResolver } from '../../src/dependencies/module_resolver' ;
2020-02-25 10:51:54 -05:00
import { NgccConfiguration } from '../../src/packages/configuration' ;
2018-08-09 10:59:10 -04:00
import { EntryPoint } from '../../src/packages/entry_point' ;
2019-03-29 06:13:14 -04:00
import { MockLogger } from '../helpers/mock_logger' ;
2018-08-09 10:59:10 -04:00
2019-08-29 11:47:54 -04:00
2019-06-06 15:22:32 -04:00
interface DepMap {
2020-02-25 10:51:54 -05:00
[ path : string ] : { resolved : string [ ] , missing : string [ ] , deepImports? : AbsoluteFsPath [ ] } ;
2019-06-06 15:22:32 -04:00
}
runInEachFileSystem ( ( ) = > {
describe ( 'DependencyResolver' , ( ) = > {
let _ : typeof absoluteFrom ;
let host : EsmDependencyHost ;
2019-12-19 17:43:13 -05:00
let dtsHost : EsmDependencyHost ;
2019-06-06 15:22:32 -04:00
let resolver : DependencyResolver ;
let fs : FileSystem ;
2020-02-25 10:51:54 -05:00
let config : NgccConfiguration ;
let logger : MockLogger ;
2019-06-06 15:22:32 -04:00
let module Resolver : ModuleResolver ;
beforeEach ( ( ) = > {
_ = absoluteFrom ;
fs = getFileSystem ( ) ;
2020-02-25 10:51:54 -05:00
config = new NgccConfiguration ( fs , _ ( '/' ) ) ;
logger = new MockLogger ( ) ;
2019-06-06 15:22:32 -04:00
module Resolver = new ModuleResolver ( fs ) ;
host = new EsmDependencyHost ( fs , module Resolver ) ;
2020-01-08 12:10:25 -05:00
dtsHost = new DtsDependencyHost ( fs ) ;
2020-02-25 10:51:54 -05:00
resolver = new DependencyResolver ( fs , logger , config , { esm5 : host , esm2015 : host } , dtsHost ) ;
2019-03-20 09:47:58 -04:00
} ) ;
2019-06-06 15:22:32 -04:00
describe ( 'sortEntryPointsByDependency()' , ( ) = > {
let first : EntryPoint ;
let second : EntryPoint ;
let third : EntryPoint ;
let fourth : EntryPoint ;
let fifth : EntryPoint ;
2019-10-02 03:32:57 -04:00
let sixthIgnoreMissing : EntryPoint ;
2019-06-06 15:22:32 -04:00
let dependencies : DepMap ;
2019-12-19 17:43:13 -05:00
let dtsDependencies : DepMap ;
2019-06-06 15:22:32 -04:00
beforeEach ( ( ) = > {
first = {
2020-02-25 10:51:54 -05:00
name : 'first' ,
2019-06-06 15:22:32 -04:00
path : _ ( '/first' ) ,
2020-02-25 10:51:54 -05:00
package : _ ( '/first' ) ,
2019-06-06 15:22:32 -04:00
packageJson : { esm5 : './index.js' } ,
2019-10-02 03:32:57 -04:00
compiledByAngular : true ,
ignoreMissingDependencies : false ,
2019-12-19 17:43:13 -05:00
typings : _ ( '/first/index.d.ts' ) ,
2019-06-06 15:22:32 -04:00
} as EntryPoint ;
second = {
path : _ ( '/second' ) ,
2020-02-25 10:51:54 -05:00
package : _ ( '/second' ) ,
2019-06-06 15:22:32 -04:00
packageJson : { esm2015 : './sub/index.js' } ,
2019-10-02 03:32:57 -04:00
compiledByAngular : true ,
ignoreMissingDependencies : false ,
2019-12-19 17:43:13 -05:00
typings : _ ( '/second/sub/index.d.ts' ) ,
2019-06-06 15:22:32 -04:00
} as EntryPoint ;
third = {
path : _ ( '/third' ) ,
2020-02-25 10:51:54 -05:00
package : _ ( '/third' ) ,
2019-06-06 15:22:32 -04:00
packageJson : { fesm5 : './index.js' } ,
2019-10-02 03:32:57 -04:00
compiledByAngular : true ,
ignoreMissingDependencies : false ,
2019-12-19 17:43:13 -05:00
typings : _ ( '/third/index.d.ts' ) ,
2019-06-06 15:22:32 -04:00
} as EntryPoint ;
fourth = {
path : _ ( '/fourth' ) ,
2020-02-25 10:51:54 -05:00
package : _ ( '/fourth' ) ,
2019-06-06 15:22:32 -04:00
packageJson : { fesm2015 : './sub2/index.js' } ,
2019-10-02 03:32:57 -04:00
compiledByAngular : true ,
ignoreMissingDependencies : false ,
2019-12-19 17:43:13 -05:00
typings : _ ( '/fourth/sub2/index.d.ts' ) ,
2019-06-06 15:22:32 -04:00
} as EntryPoint ;
fifth = {
path : _ ( '/fifth' ) ,
2020-02-25 10:51:54 -05:00
package : _ ( '/fifth' ) ,
2019-06-06 15:22:32 -04:00
packageJson : { module : './index.js' } ,
2019-10-02 03:32:57 -04:00
compiledByAngular : true ,
ignoreMissingDependencies : false ,
2019-12-19 17:43:13 -05:00
typings : _ ( '/fifth/index.d.ts' ) ,
2019-10-02 03:32:57 -04:00
} as EntryPoint ;
sixthIgnoreMissing = {
path : _ ( '/sixth' ) ,
2020-02-25 10:51:54 -05:00
package : _ ( '/sixth' ) ,
2019-10-02 03:32:57 -04:00
packageJson : { module : './index.js' } ,
compiledByAngular : true ,
ignoreMissingDependencies : true ,
2019-12-19 17:43:13 -05:00
typings : _ ( '/sixth/index.d.ts' ) ,
2019-06-06 15:22:32 -04:00
} as EntryPoint ;
dependencies = {
2019-12-19 17:43:12 -05:00
[ _ ( '/first/index.js' ) ] :
{ resolved : [ second . path , third . path , _ ( '/ignored-1' ) ] , missing : [ ] } ,
2019-06-06 15:22:32 -04:00
[ _ ( '/second/sub/index.js' ) ] : { resolved : [ third . path , fifth . path ] , missing : [ ] } ,
2019-12-19 17:43:12 -05:00
[ _ ( '/third/index.js' ) ] : { resolved : [ fourth . path , _ ( '/ignored-2' ) ] , missing : [ ] } ,
2019-06-06 15:22:32 -04:00
[ _ ( '/fourth/sub2/index.js' ) ] : { resolved : [ fifth . path ] , missing : [ ] } ,
[ _ ( '/fifth/index.js' ) ] : { resolved : [ ] , missing : [ ] } ,
} ;
2019-12-19 17:43:13 -05:00
dtsDependencies = {
[ _ ( '/first/index.d.ts' ) ] :
{ resolved : [ second . path , third . path , _ ( '/ignored-1' ) ] , missing : [ ] } ,
[ _ ( '/second/sub/index.d.ts' ) ] : { resolved : [ third . path , fifth . path ] , missing : [ ] } ,
[ _ ( '/third/index.d.ts' ) ] : { resolved : [ fourth . path , _ ( '/ignored-2' ) ] , missing : [ ] } ,
[ _ ( '/fourth/sub2/index.d.ts' ) ] : { resolved : [ fifth . path ] , missing : [ ] } ,
[ _ ( '/fifth/index.d.ts' ) ] : { resolved : [ ] , missing : [ ] } ,
} ;
2019-06-06 15:22:32 -04:00
} ) ;
it ( 'should order the entry points by their dependency on each other' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dependencies ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dtsDependencies ) ) ;
2019-06-06 15:22:32 -04:00
const result = resolver . sortEntryPointsByDependency ( [ fifth , first , fourth , second , third ] ) ;
expect ( result . entryPoints ) . toEqual ( [ fifth , fourth , third , second , first ] ) ;
} ) ;
it ( 'should remove entry-points that have missing direct dependencies' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
2019-06-06 15:22:32 -04:00
[ _ ( '/second/sub/index.js' ) ] : { resolved : [ ] , missing : [ ] } ,
} ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
[ _ ( '/second/sub/index.d.ts' ) ] : { resolved : [ ] , missing : [ ] } ,
} ) ) ;
2019-06-06 15:22:32 -04:00
const result = resolver . sortEntryPointsByDependency ( [ first , second ] ) ;
expect ( result . entryPoints ) . toEqual ( [ second ] ) ;
expect ( result . invalidEntryPoints ) . toEqual ( [
2019-12-19 17:43:12 -05:00
{ entryPoint : first , missingDependencies : [ _ ( '/missing' ) ] } ,
2019-06-06 15:22:32 -04:00
] ) ;
} ) ;
it ( 'should remove entry points that depended upon an invalid entry-point' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
2019-06-06 15:22:32 -04:00
[ _ ( '/first/index.js' ) ] : { resolved : [ second . path , third . path ] , missing : [ ] } ,
2019-12-19 17:43:12 -05:00
[ _ ( '/second/sub/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
2019-06-06 15:22:32 -04:00
[ _ ( '/third/index.js' ) ] : { resolved : [ ] , missing : [ ] } ,
} ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ second . path , third . path ] , missing : [ ] } ,
[ _ ( '/second/sub/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
[ _ ( '/third/index.d.ts' ) ] : { resolved : [ ] , missing : [ ] } ,
} ) ) ;
2019-06-06 15:22:32 -04:00
// Note that we will process `first` before `second`, which has the missing dependency.
const result = resolver . sortEntryPointsByDependency ( [ first , second , third ] ) ;
expect ( result . entryPoints ) . toEqual ( [ third ] ) ;
expect ( result . invalidEntryPoints ) . toEqual ( [
2019-12-19 17:43:12 -05:00
{ entryPoint : second , missingDependencies : [ _ ( '/missing' ) ] } ,
{ entryPoint : first , missingDependencies : [ _ ( '/missing' ) ] } ,
2019-06-06 15:22:32 -04:00
] ) ;
} ) ;
it ( 'should remove entry points that will depend upon an invalid entry-point' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
2019-06-06 15:22:32 -04:00
[ _ ( '/first/index.js' ) ] : { resolved : [ second . path , third . path ] , missing : [ ] } ,
2019-12-19 17:43:12 -05:00
[ _ ( '/second/sub/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
2019-06-06 15:22:32 -04:00
[ _ ( '/third/index.js' ) ] : { resolved : [ ] , missing : [ ] } ,
} ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ second . path , third . path ] , missing : [ ] } ,
[ _ ( '/second/sub/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
[ _ ( '/third/index.d.ts' ) ] : { resolved : [ ] , missing : [ ] } ,
} ) ) ;
2019-06-06 15:22:32 -04:00
// Note that we will process `first` after `second`, which has the missing dependency.
const result = resolver . sortEntryPointsByDependency ( [ second , first , third ] ) ;
expect ( result . entryPoints ) . toEqual ( [ third ] ) ;
expect ( result . invalidEntryPoints ) . toEqual ( [
2019-12-19 17:43:12 -05:00
{ entryPoint : second , missingDependencies : [ _ ( '/missing' ) ] } ,
2019-06-06 15:22:32 -04:00
{ entryPoint : first , missingDependencies : [ second . path ] } ,
] ) ;
} ) ;
2019-10-02 03:32:57 -04:00
it ( 'should cope with entry points that will depend upon an invalid entry-point, when told to ignore missing dependencies' ,
( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
2019-10-02 03:32:57 -04:00
[ _ ( '/first/index.js' ) ] : { resolved : [ sixthIgnoreMissing . path ] , missing : [ ] } ,
2019-12-19 17:43:12 -05:00
[ _ ( '/sixth/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
2019-10-02 03:32:57 -04:00
} ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ sixthIgnoreMissing . path ] , missing : [ ] } ,
[ _ ( '/sixth/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
} ) ) ;
2019-10-02 03:32:57 -04:00
// Note that we will process `first` after `second`, which has the missing dependency.
const result = resolver . sortEntryPointsByDependency ( [ sixthIgnoreMissing , first ] ) ;
expect ( result . entryPoints ) . toEqual ( [ sixthIgnoreMissing , first ] ) ;
expect ( result . invalidEntryPoints ) . toEqual ( [ ] ) ;
} ) ;
it ( 'should not transitively ignore missing dependencies' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
2019-10-02 03:32:57 -04:00
[ _ ( '/second/sub/index.js' ) ] : { resolved : [ first . path ] , missing : [ ] } ,
[ _ ( '/sixth/index.js' ) ] : { resolved : [ second . path ] , missing : [ ] } ,
} ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
[ _ ( '/second/sub/index.d.ts' ) ] : { resolved : [ first . path ] , missing : [ ] } ,
[ _ ( '/sixth/index.d.ts' ) ] : { resolved : [ second . path ] , missing : [ ] } ,
} ) ) ;
2019-10-02 03:32:57 -04:00
const result = resolver . sortEntryPointsByDependency ( [ first , second , sixthIgnoreMissing ] ) ;
// sixth has no missing dependencies, but it has _invalid_ dependencies, so it's not
// compiled.
expect ( result . entryPoints ) . toEqual ( [ ] ) ;
} ) ;
2019-06-26 04:16:15 -04:00
it ( 'should cope with entry points having multiple indirect missing dependencies' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing1' ) ] } ,
[ _ ( '/second/sub/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing2' ) ] } ,
2019-06-26 04:16:15 -04:00
[ _ ( '/third/index.js' ) ] : { resolved : [ first . path , second . path ] , missing : [ ] } ,
} ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing1' ) ] } ,
[ _ ( '/second/sub/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing2' ) ] } ,
[ _ ( '/third/index.d.ts' ) ] : { resolved : [ first . path , second . path ] , missing : [ ] } ,
} ) ) ;
2019-06-26 04:16:15 -04:00
const result = resolver . sortEntryPointsByDependency ( [ first , second , third ] ) ;
expect ( result . entryPoints ) . toEqual ( [ ] ) ;
expect ( result . invalidEntryPoints ) . toEqual ( [
2019-12-19 17:43:12 -05:00
{ entryPoint : first , missingDependencies : [ _ ( '/missing1' ) ] } ,
{ entryPoint : second , missingDependencies : [ _ ( '/missing2' ) ] } ,
2019-06-26 04:16:15 -04:00
{ entryPoint : third , missingDependencies : [ first . path ] } ,
] ) ;
} ) ;
2020-02-25 10:51:54 -05:00
it ( 'should log a warning for deep imports' , ( ) = > {
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.js' ) ] : { resolved : [ ] , missing : [ ] , deepImports : [ _ ( '/deep/one' ) ] } ,
} ) ) ;
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ ] , missing : [ ] } ,
} ) ) ;
const result = resolver . sortEntryPointsByDependency ( [ first ] ) ;
expect ( result . entryPoints ) . toEqual ( [ first ] ) ;
expect ( logger . logs . warn ) . toEqual ( [ [
` Entry point 'first' contains deep imports into ' ${ _ ( '/deep/one' ) } '. This is probably not a problem, but may cause the compilation of entry points to be out of order. `
] ] ) ;
} ) ;
it ( 'should not log a warning for ignored deep imports' , ( ) = > {
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/project/node_modules/test-package/index.js' ) ] : {
resolved : [ ] ,
missing : [ ] ,
deepImports : [
_ ( '/project/node_modules/deep/one' ) , _ ( '/project/node_modules/deep/two' ) ,
_ ( '/project/node_modules/deeper/one' ) , _ ( '/project/node_modules/deeper/two' )
]
} ,
} ) ) ;
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/project/node_modules/test-package/index.d.ts' ) ] : { resolved : [ ] , missing : [ ] } ,
} ) ) ;
// Setup the configuration to ignore deep imports that contain either "deep/" or "two".
fs . ensureDir ( _ ( '/project' ) ) ;
fs . writeFile (
_ ( '/project/ngcc.config.js' ) ,
` module.exports = { packages: { 'test-package': { ignorableDeepImportMatchers: [/deep \\ //, /two/] } } }; ` ) ;
config = new NgccConfiguration ( fs , _ ( '/project' ) ) ;
resolver = new DependencyResolver ( fs , logger , config , { esm5 : host , esm2015 : host } , dtsHost ) ;
const testEntryPoint = {
name : 'test-package' ,
path : _ ( '/project/node_modules/test-package' ) ,
package : _ ( '/project/node_modules/test-package' ) ,
packageJson : { esm5 : './index.js' } ,
compiledByAngular : true ,
ignoreMissingDependencies : false ,
typings : _ ( '/project/node_modules/test-package/index.d.ts' ) ,
} as EntryPoint ;
const result = resolver . sortEntryPointsByDependency ( [ testEntryPoint ] ) ;
expect ( result . entryPoints ) . toEqual ( [ testEntryPoint ] ) ;
expect ( logger . logs . warn ) . toEqual ( [ [
` Entry point 'test-package' contains deep imports into ' ${ _ ( '/project/node_modules/deeper/one' ) } '. This is probably not a problem, but may cause the compilation of entry points to be out of order. `
] ] ) ;
} ) ;
2019-06-06 15:22:32 -04:00
it ( 'should error if the entry point does not have a suitable format' , ( ) = > {
expect ( ( ) = > resolver . sortEntryPointsByDependency ( [
{ path : '/first' , packageJson : { } , compiledByAngular : true } as EntryPoint
] ) ) . toThrowError ( ` There is no appropriate source code format in '/first' entry-point. ` ) ;
} ) ;
it ( 'should error if there is no appropriate DependencyHost for the given formats' , ( ) = > {
2020-02-25 10:51:54 -05:00
resolver = new DependencyResolver ( fs , new MockLogger ( ) , config , { esm2015 : host } , host ) ;
2019-06-06 15:22:32 -04:00
expect ( ( ) = > resolver . sortEntryPointsByDependency ( [ first ] ) )
. toThrowError (
` Could not find a suitable format for computing dependencies of entry-point: ' ${ first . path } '. ` ) ;
} ) ;
it ( 'should capture any dependencies that were ignored' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dependencies ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dtsDependencies ) ) ;
2019-06-06 15:22:32 -04:00
const result = resolver . sortEntryPointsByDependency ( [ fifth , first , fourth , second , third ] ) ;
expect ( result . ignoredDependencies ) . toEqual ( [
2019-12-19 17:43:12 -05:00
{ entryPoint : first , dependencyPath : _ ( '/ignored-1' ) } ,
{ entryPoint : third , dependencyPath : _ ( '/ignored-2' ) } ,
2019-06-06 15:22:32 -04:00
] ) ;
} ) ;
2019-08-29 11:47:54 -04:00
it ( 'should return the computed dependency graph' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dependencies ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dtsDependencies ) ) ;
2019-08-29 11:47:54 -04:00
const result = resolver . sortEntryPointsByDependency ( [ fifth , first , fourth , second , third ] ) ;
expect ( result . graph ) . toEqual ( jasmine . any ( DepGraph ) ) ;
expect ( result . graph . size ( ) ) . toBe ( 5 ) ;
expect ( result . graph . dependenciesOf ( third . path ) ) . toEqual ( [ fifth . path , fourth . path ] ) ;
} ) ;
2019-06-06 15:22:32 -04:00
it ( 'should only return dependencies of the target, if provided' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dependencies ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dtsDependencies ) ) ;
2019-06-06 15:22:32 -04:00
const entryPoints = [ fifth , first , fourth , second , third ] ;
let sorted : SortedEntryPointsInfo ;
sorted = resolver . sortEntryPointsByDependency ( entryPoints , first ) ;
expect ( sorted . entryPoints ) . toEqual ( [ fifth , fourth , third , second , first ] ) ;
sorted = resolver . sortEntryPointsByDependency ( entryPoints , second ) ;
expect ( sorted . entryPoints ) . toEqual ( [ fifth , fourth , third , second ] ) ;
sorted = resolver . sortEntryPointsByDependency ( entryPoints , third ) ;
expect ( sorted . entryPoints ) . toEqual ( [ fifth , fourth , third ] ) ;
sorted = resolver . sortEntryPointsByDependency ( entryPoints , fourth ) ;
expect ( sorted . entryPoints ) . toEqual ( [ fifth , fourth ] ) ;
sorted = resolver . sortEntryPointsByDependency ( entryPoints , fifth ) ;
expect ( sorted . entryPoints ) . toEqual ( [ fifth ] ) ;
} ) ;
2019-07-27 15:02:04 -04:00
it ( 'should not process the provided target if it has missing dependencies' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
2019-07-27 15:02:04 -04:00
} ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing' ) ] } ,
} ) ) ;
2019-07-27 15:02:04 -04:00
const entryPoints = [ first ] ;
let sorted : SortedEntryPointsInfo ;
sorted = resolver . sortEntryPointsByDependency ( entryPoints , first ) ;
expect ( sorted . entryPoints ) . toEqual ( [ ] ) ;
expect ( sorted . invalidEntryPoints [ 0 ] . entryPoint ) . toEqual ( first ) ;
2019-12-19 17:43:12 -05:00
expect ( sorted . invalidEntryPoints [ 0 ] . missingDependencies ) . toEqual ( [ _ ( '/missing' ) ] ) ;
2019-07-27 15:02:04 -04:00
} ) ;
2019-07-27 15:37:04 -04:00
it ( 'should not consider builtin NodeJS modules as missing dependency' , ( ) = > {
2019-12-19 17:43:12 -05:00
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
2019-07-27 15:37:04 -04:00
[ _ ( '/first/index.js' ) ] : { resolved : [ ] , missing : [ 'fs' ] } ,
} ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ ] , missing : [ 'fs' ] } ,
} ) ) ;
2019-07-27 15:37:04 -04:00
const entryPoints = [ first ] ;
let sorted : SortedEntryPointsInfo ;
sorted = resolver . sortEntryPointsByDependency ( entryPoints , first ) ;
expect ( sorted . entryPoints ) . toEqual ( [ first ] ) ;
expect ( sorted . invalidEntryPoints ) . toEqual ( [ ] ) ;
expect ( sorted . ignoredDependencies ) . toEqual ( [ ] ) ;
} ) ;
2019-06-06 15:22:32 -04:00
it ( 'should use the appropriate DependencyHost for each entry-point' , ( ) = > {
const esm5Host = new EsmDependencyHost ( fs , module Resolver ) ;
const esm2015Host = new EsmDependencyHost ( fs , module Resolver ) ;
2020-01-08 12:10:25 -05:00
const dtsHost = new DtsDependencyHost ( fs ) ;
2019-12-19 17:43:13 -05:00
resolver = new DependencyResolver (
2020-02-25 10:51:54 -05:00
fs , new MockLogger ( ) , config , { esm5 : esm5Host , esm2015 : esm2015Host } , dtsHost ) ;
2019-12-19 17:43:12 -05:00
spyOn ( esm5Host , 'collectDependencies' )
2019-06-06 15:22:32 -04:00
. and . callFake ( createFakeComputeDependencies ( dependencies ) ) ;
2019-12-19 17:43:12 -05:00
spyOn ( esm2015Host , 'collectDependencies' )
2019-06-06 15:22:32 -04:00
. and . callFake ( createFakeComputeDependencies ( dependencies ) ) ;
2019-12-19 17:43:13 -05:00
spyOn ( dtsHost , 'collectDependencies' )
. and . callFake ( createFakeComputeDependencies ( dtsDependencies ) ) ;
2019-06-06 15:22:32 -04:00
const result = resolver . sortEntryPointsByDependency ( [ fifth , first , fourth , second , third ] ) ;
expect ( result . entryPoints ) . toEqual ( [ fifth , fourth , third , second , first ] ) ;
2019-12-19 17:43:12 -05:00
expect ( esm5Host . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( first . path , 'index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm5Host . collectDependencies )
. not . toHaveBeenCalledWith ( fs . resolve ( second . path , 'sub/index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm5Host . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( third . path , 'index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm5Host . collectDependencies )
. not . toHaveBeenCalledWith (
fs . resolve ( fourth . path , 'sub2/index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm5Host . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( fifth . path , 'index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm2015Host . collectDependencies )
. not . toHaveBeenCalledWith ( fs . resolve ( first . path , 'index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm2015Host . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( second . path , 'sub/index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm2015Host . collectDependencies )
. not . toHaveBeenCalledWith ( fs . resolve ( third . path , 'index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm2015Host . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( fourth . path , 'sub2/index.js' ) , jasmine . any ( Object ) ) ;
expect ( esm2015Host . collectDependencies )
. not . toHaveBeenCalledWith ( fs . resolve ( fifth . path , 'index.js' ) , jasmine . any ( Object ) ) ;
2019-12-19 17:43:13 -05:00
expect ( dtsHost . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( first . path , 'index.d.ts' ) , jasmine . any ( Object ) ) ;
expect ( dtsHost . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( second . path , 'sub/index.d.ts' ) , jasmine . any ( Object ) ) ;
expect ( dtsHost . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( third . path , 'index.d.ts' ) , jasmine . any ( Object ) ) ;
expect ( dtsHost . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( fourth . path , 'sub2/index.d.ts' ) , jasmine . any ( Object ) ) ;
expect ( dtsHost . collectDependencies )
. toHaveBeenCalledWith ( fs . resolve ( fifth . path , 'index.d.ts' ) , jasmine . any ( Object ) ) ;
} ) ;
it ( 'should merge "typings-only" dependencies with source dependencies' , ( ) = > {
spyOn ( host , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.js' ) ] : { resolved : [ ] , missing : [ ] } ,
[ _ ( '/second/sub/index.js' ) ] : { resolved : [ ] , missing : [ _ ( '/missing1' ) ] } ,
[ _ ( '/third/index.js' ) ] : { resolved : [ first . path ] , missing : [ ] } ,
} ) ) ;
spyOn ( dtsHost , 'collectDependencies' ) . and . callFake ( createFakeComputeDependencies ( {
[ _ ( '/first/index.d.ts' ) ] : { resolved : [ ] , missing : [ ] } ,
[ _ ( '/second/sub/index.d.ts' ) ] : { resolved : [ ] , missing : [ _ ( '/missing2' ) ] } ,
[ _ ( '/third/index.d.ts' ) ] : { resolved : [ second . path ] , missing : [ ] } ,
} ) ) ;
const entryPoints = [ first , second , third ] ;
const sorted = resolver . sortEntryPointsByDependency ( entryPoints ) ;
expect ( sorted . entryPoints ) . toEqual ( [ first ] ) ;
expect ( sorted . invalidEntryPoints ) . toEqual ( [
{ entryPoint : second , missingDependencies : [ _ ( '/missing1' ) , _ ( '/missing2' ) ] } ,
{ entryPoint : third , missingDependencies : [ _ ( '/second' ) ] } ,
] ) ;
2019-06-06 15:22:32 -04:00
} ) ;
function createFakeComputeDependencies ( deps : DepMap ) {
2019-12-19 17:43:12 -05:00
return ( entryPointPath : string , { dependencies , missing , deepImports } : DependencyInfo ) = > {
deps [ entryPointPath ] . resolved . forEach ( dep = > dependencies . add ( absoluteFrom ( dep ) ) ) ;
deps [ entryPointPath ] . missing . forEach (
dep = > missing . add ( fs . isRooted ( dep ) ? absoluteFrom ( dep ) : relativeFrom ( dep ) ) ) ;
2020-02-25 10:51:54 -05:00
if ( deps [ entryPointPath ] . deepImports ) {
deps [ entryPointPath ] . deepImports ! . forEach ( dep = > deepImports . add ( dep ) ) ;
}
2019-06-06 15:22:32 -04:00
return { dependencies , missing , deepImports } ;
} ;
}
2019-04-28 15:48:35 -04:00
} ) ;
2018-08-09 10:59:10 -04:00
} ) ;
} ) ;