-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(compiler): add docs and tests for path transformer
This commit adds code comments and tests for the import path TS transformer. It also reverts some temp changes in a previous commit
- Loading branch information
1 parent
2922c0b
commit 44bf1d6
Showing
4 changed files
with
102 additions
and
60 deletions.
There are no files selected for viewing
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
92 changes: 77 additions & 15 deletions
92
src/compiler/transformers/test/map-imports-to-path-aliases.spec.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 |
---|---|---|
@@ -1,35 +1,97 @@ | ||
import { mockValidatedConfig } from '@stencil/core/testing'; | ||
import { ValidatedConfig } from '../../../internal'; | ||
import { transpileModule } from './transpile'; | ||
import ts, { Extension } from 'typescript'; | ||
import { mapImportsToPathAliases } from '../map-imports-to-path-aliases'; | ||
|
||
describe('mapImportsToPathAliases', () => { | ||
let module: ReturnType<typeof transpileModule>; | ||
let config: ValidatedConfig; | ||
let resolveModuleNameSpy: jest.SpyInstance< | ||
ReturnType<typeof ts.resolveModuleName>, | ||
Parameters<typeof ts.resolveModuleName> | ||
>; | ||
|
||
beforeEach(() => { | ||
config = mockValidatedConfig({ tsconfig: './tsconfig.json', tsCompilerOptions: {} }); | ||
|
||
resolveModuleNameSpy = jest.spyOn(ts, 'resolveModuleName'); | ||
}); | ||
|
||
/** | ||
* This test fails | ||
* The `transpileModule` helper method is designed to only transpile a single module's (file's) | ||
* content. However, with the solution implemented to fix module imports not getting transformed respective of | ||
* their path aliases in the `tsconfig.json` file for a project, this helper cannot resolve the defined imports so | ||
* the text returned from the helper keeps the original import path intact. | ||
*/ | ||
it('should replace the path alias with the generated relative path', () => { | ||
config.tsCompilerOptions.paths = { | ||
'@utils': ['./utils'], | ||
}; | ||
config.tsCompilerOptions.baseUrl = '.'; | ||
afterEach(() => { | ||
resolveModuleNameSpy.mockReset(); | ||
}); | ||
|
||
it('should ignore relative imports', () => { | ||
resolveModuleNameSpy.mockReturnValue({ | ||
resolvedModule: { | ||
isExternalLibraryImport: false, | ||
extension: Extension.Ts, | ||
resolvedFileName: 'utils.js', | ||
}, | ||
}); | ||
const inputText = ` | ||
import * as dateUtils from '@utils'; | ||
import * as dateUtils from "../utils"; | ||
dateUtils.test(); | ||
`; | ||
|
||
module = transpileModule(inputText, config, null, [], []); | ||
module = transpileModule(inputText, config, null, [], [mapImportsToPathAliases(config)]); | ||
|
||
expect(module.outputText).toContain('import * as dateUtils from "../utils";'); | ||
}); | ||
|
||
it('should ignore external imports', () => { | ||
resolveModuleNameSpy.mockReturnValue({ | ||
resolvedModule: { | ||
isExternalLibraryImport: true, | ||
extension: Extension.Ts, | ||
resolvedFileName: 'utils.js', | ||
}, | ||
}); | ||
const inputText = ` | ||
import { utils } from "@stencil/core"; | ||
utils.test(); | ||
`; | ||
|
||
module = transpileModule(inputText, config, null, [], [mapImportsToPathAliases(config)]); | ||
|
||
expect(module.outputText).toContain('import { utils } from "@stencil/core";'); | ||
}); | ||
|
||
it('should do nothing if there is no resolved module', () => { | ||
resolveModuleNameSpy.mockReturnValue({ | ||
resolvedModule: undefined, | ||
}); | ||
const inputText = ` | ||
import { utils } from "@utils"; | ||
utils.test(); | ||
`; | ||
|
||
module = transpileModule(inputText, config, null, [], [mapImportsToPathAliases(config)]); | ||
|
||
expect(module.outputText).toContain('import { utils } from "@utils";'); | ||
}); | ||
|
||
// TODO(STENCIL-223): remove spy to test actual resolution behavior | ||
it('should replace the path alias with the generated relative path', () => { | ||
resolveModuleNameSpy.mockReturnValue({ | ||
resolvedModule: { | ||
isExternalLibraryImport: false, | ||
extension: Extension.Ts, | ||
resolvedFileName: 'utils.ts', | ||
}, | ||
}); | ||
const inputText = ` | ||
import { utils } from "@utils"; | ||
utils.test(); | ||
`; | ||
|
||
module = transpileModule(inputText, config, null, [], [mapImportsToPathAliases(config)]); | ||
|
||
expect(module.outputText).toEqual(`import * as dateUtils from '../../utils/date'`); | ||
expect(module.outputText).toContain('import { utils } from "utils";'); | ||
}); | ||
}); |
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