-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from cloudbeds/substitute-aliased-paths
feat: substitute aliased modules
- Loading branch information
Showing
7 changed files
with
144 additions
and
8 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/compileTypes/helpers/__tests__/includeTypesFromNodeModules.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { FederationConfig } from '../../../models'; | ||
import { setLogger } from '../../../helpers'; | ||
import { includeTypesFromNodeModules } from '../includeTypesFromNodeModules'; | ||
|
||
// Assuming logger mock setup is similar to previous example | ||
const mockLogger = { | ||
log: jest.fn(), | ||
warn: jest.fn(), | ||
}; | ||
|
||
setLogger(mockLogger); | ||
|
||
describe('includeTypesFromNodeModules', () => { | ||
test('correctly includes typings for exposed NPM packages', () => { | ||
const federationConfig: FederationConfig = { | ||
name: 'myApp', | ||
exposes: { | ||
ModuleA: './node_modules/libraryA', | ||
ModuleB: './node_modules/libraryB', | ||
}, | ||
}; | ||
const initialTypings = 'initial typings content'; | ||
|
||
const result = includeTypesFromNodeModules(federationConfig, initialTypings); | ||
|
||
const moduleADeclaration = [ | ||
'declare module "myApp/ModuleA" {', | ||
' export * from "libraryA"', | ||
'}', | ||
].join('\n'); | ||
const moduleBDeclaration = [ | ||
'declare module "myApp/ModuleB" {', | ||
' export * from "libraryB"', | ||
'}', | ||
].join('\n'); | ||
|
||
expect(result).toBe([initialTypings, moduleADeclaration, moduleBDeclaration].join('\n')); | ||
expect(mockLogger.log).toHaveBeenCalledWith('Including typings for npm packages:', [ | ||
['ModuleA', 'libraryA'], | ||
['ModuleB', 'libraryB'], | ||
]); | ||
}); | ||
|
||
test('does not modify typings when there are no NPM package paths', () => { | ||
const federationConfig: FederationConfig = { | ||
name: 'myApp', | ||
exposes: { | ||
LocalModule: './src/LocalModule', | ||
}, | ||
}; | ||
const initialTypings = 'initial typings content'; | ||
|
||
const result = includeTypesFromNodeModules(federationConfig, initialTypings); | ||
|
||
expect(result).toBe(initialTypings); | ||
expect(mockLogger.log).not.toHaveBeenCalledWith(); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/compileTypes/helpers/__tests__/substituteAliasedModules.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { substituteAliasedModules } from '../substituteAliasedModules'; | ||
import { | ||
getLogger, setLogger, | ||
} from '../../../helpers'; | ||
import { PREFIX_NOT_FOR_IMPORT } from '../../../constants'; | ||
|
||
const mockLogger = { | ||
log: jest.fn(), | ||
}; | ||
|
||
setLogger(mockLogger); | ||
|
||
describe('substituteAliasedModules', () => { | ||
const federatedModuleName = 'myCommon'; | ||
const logger = getLogger(); | ||
|
||
test('substitutes import path when #not-for-import version exists', () => { | ||
const modulePath = 'libs/currency'; | ||
const typings = ` | ||
Some import("${modulePath}") more content | ||
declare module "${PREFIX_NOT_FOR_IMPORT}/${federatedModuleName}/${modulePath}" | ||
`; | ||
|
||
const subsituted = substituteAliasedModules(federatedModuleName, typings); | ||
|
||
expect(subsituted).toBe(` | ||
Some import("${PREFIX_NOT_FOR_IMPORT}/${federatedModuleName}/${modulePath}") more content | ||
declare module "${PREFIX_NOT_FOR_IMPORT}/${federatedModuleName}/${modulePath}" | ||
`); | ||
expect(logger.log).toHaveBeenCalledWith(`Substituting import path: ${modulePath}`); | ||
}); | ||
|
||
test('does not modify typings when a #not-for-import version does not exist', () => { | ||
const originalTypings = 'Some content import("another/module") more content'; | ||
|
||
const result = substituteAliasedModules(federatedModuleName, originalTypings); | ||
|
||
expect(result).toBe(originalTypings); | ||
expect(logger.log).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './getTSConfigCompilerOptions'; | ||
export * from './includeTypesFromNodeModules'; | ||
export * from './reportCompileDiagnostic'; | ||
export * from './substituteAliasedModules'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { getLogger } from '../../helpers'; | ||
import { PREFIX_NOT_FOR_IMPORT } from '../../constants'; | ||
|
||
export function substituteAliasedModules(federatedModuleName: string, typings: string): string { | ||
const logger = getLogger(); | ||
|
||
// Collect all instances of `import("...")` | ||
const regexImportPaths = /import\("([^"]*)"\)/g; | ||
const uniqueImportPaths = new Set(); | ||
|
||
let match = regexImportPaths.exec(typings); | ||
while (match) { | ||
uniqueImportPaths.add(match[1]); | ||
match = regexImportPaths.exec(typings); | ||
} | ||
|
||
uniqueImportPaths.forEach(importPath => { | ||
const notForImportPath = `${PREFIX_NOT_FOR_IMPORT}/${federatedModuleName}/${importPath}`; | ||
|
||
if (typings.includes(`declare module "${notForImportPath}"`)) { | ||
logger.log(`Substituting import path: ${importPath}`); | ||
|
||
typings = typings.replace( | ||
new RegExp(`import\\("${importPath}"\\)`, 'g'), | ||
`import("${notForImportPath}")`, | ||
); | ||
} | ||
}); | ||
|
||
return typings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters