-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(collection): properly transform imports (#3523)
This commit adds a transformer to the transpilation process to resolve module imports and replace path aliases with auto-generated relative paths for `dist-collection` when `tsconfig.json#paths` is defined. Prior to this, the `dist-collection` output target would not transpile path aliases defined in a project's `tsconfig.json`. Instead, the import paths were left unchanged and thus reference modules that cannot be resolved. STENCIL-437 Output target collection does not transpile ts path config
- Loading branch information
1 parent
965323b
commit ac2c09e
Showing
10 changed files
with
608 additions
and
11 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
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
87 changes: 87 additions & 0 deletions
87
src/compiler/config/test/validate-output-dist-collection.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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type * as d from '@stencil/core/declarations'; | ||
import { validateConfig } from '../validate-config'; | ||
import { mockConfig, mockLoadConfigInit } from '@stencil/core/testing'; | ||
import { resolve, join } from 'path'; | ||
|
||
describe('validateDistCollectionOutputTarget', () => { | ||
let config: d.Config; | ||
|
||
const rootDir = resolve('/'); | ||
const defaultDir = join(rootDir, 'dist', 'collection'); | ||
|
||
beforeEach(() => { | ||
config = mockConfig(); | ||
}); | ||
|
||
it('sets correct default values', () => { | ||
const target: d.OutputTargetDistCollection = { | ||
type: 'dist-collection', | ||
empty: false, | ||
dir: null, | ||
collectionDir: null, | ||
}; | ||
config.outputTargets = [target]; | ||
|
||
const { config: validatedConfig } = validateConfig(config, mockLoadConfigInit()); | ||
|
||
expect(validatedConfig.outputTargets).toEqual([ | ||
{ | ||
type: 'dist-collection', | ||
empty: false, | ||
dir: defaultDir, | ||
collectionDir: null, | ||
transformAliasedImportPaths: false, | ||
}, | ||
]); | ||
}); | ||
|
||
it('sets specified directory', () => { | ||
const target: d.OutputTargetDistCollection = { | ||
type: 'dist-collection', | ||
empty: false, | ||
dir: '/my-dist', | ||
collectionDir: null, | ||
}; | ||
config.outputTargets = [target]; | ||
|
||
const { config: validatedConfig } = validateConfig(config, mockLoadConfigInit()); | ||
|
||
expect(validatedConfig.outputTargets).toEqual([ | ||
{ | ||
type: 'dist-collection', | ||
empty: false, | ||
dir: '/my-dist', | ||
collectionDir: null, | ||
transformAliasedImportPaths: false, | ||
}, | ||
]); | ||
}); | ||
|
||
describe('transformAliasedImportPaths', () => { | ||
it.each([false, true])( | ||
"sets option '%s' when explicitly '%s' in config", | ||
(transformAliasedImportPaths: boolean) => { | ||
const target: d.OutputTargetDistCollection = { | ||
type: 'dist-collection', | ||
empty: false, | ||
dir: null, | ||
collectionDir: null, | ||
transformAliasedImportPaths, | ||
}; | ||
config.outputTargets = [target]; | ||
|
||
const { config: validatedConfig } = validateConfig(config, mockLoadConfigInit()); | ||
|
||
expect(validatedConfig.outputTargets).toEqual([ | ||
{ | ||
type: 'dist-collection', | ||
empty: false, | ||
dir: defaultDir, | ||
collectionDir: null, | ||
transformAliasedImportPaths, | ||
}, | ||
]); | ||
} | ||
); | ||
}); | ||
}); |
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
73 changes: 73 additions & 0 deletions
73
src/compiler/output-targets/test/output-targets-collection.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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { outputCollection } from '../dist-collection'; | ||
import type * as d from '../../../declarations'; | ||
import { mockValidatedConfig, mockBuildCtx, mockCompilerCtx, mockModule } from '@stencil/core/testing'; | ||
import * as test from '../../transformers/map-imports-to-path-aliases'; | ||
import { normalize } from 'path'; | ||
|
||
describe('Dist Collection output target', () => { | ||
let mockConfig: d.ValidatedConfig; | ||
let mockedBuildCtx: d.BuildCtx; | ||
let mockedCompilerCtx: d.CompilerCtx; | ||
let changedModules: d.Module[]; | ||
|
||
let mapImportPathSpy: jest.SpyInstance; | ||
|
||
const mockTraverse = jest.fn().mockImplementation((source: any) => source); | ||
const mockMap = jest.fn().mockImplementation(() => mockTraverse); | ||
const target: d.OutputTargetDistCollection = { | ||
type: 'dist-collection', | ||
dir: '', | ||
collectionDir: '/dist/collection', | ||
}; | ||
|
||
beforeEach(() => { | ||
mockConfig = mockValidatedConfig({ | ||
srcDir: '/src', | ||
}); | ||
mockedBuildCtx = mockBuildCtx(); | ||
mockedCompilerCtx = mockCompilerCtx(); | ||
changedModules = [ | ||
mockModule({ | ||
staticSourceFileText: '', | ||
jsFilePath: '/src/main.js', | ||
sourceFilePath: '/src/main.ts', | ||
}), | ||
]; | ||
|
||
jest.spyOn(mockedCompilerCtx.fs, 'writeFile'); | ||
|
||
mapImportPathSpy = jest.spyOn(test, 'mapImportsToPathAliases'); | ||
mapImportPathSpy.mockReturnValue(mockMap); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('transform aliased import paths', () => { | ||
// These tests ensure that the transformer for import paths is called regardless | ||
// of the config value (the function will decided whether or not to actually do anything) to avoid | ||
// a race condition with duplicate file writes | ||
it.each([true, false])( | ||
'calls function to transform aliased import paths when the output target config flag is `%s`', | ||
async (transformAliasedImportPaths: boolean) => { | ||
mockConfig.outputTargets = [ | ||
{ | ||
...target, | ||
transformAliasedImportPaths, | ||
}, | ||
]; | ||
|
||
await outputCollection(mockConfig, mockedCompilerCtx, mockedBuildCtx, changedModules); | ||
|
||
expect(mapImportPathSpy).toHaveBeenCalledWith(mockConfig, normalize('/dist/collection/main.js'), { | ||
collectionDir: '/dist/collection', | ||
dir: '', | ||
transformAliasedImportPaths, | ||
type: 'dist-collection', | ||
}); | ||
expect(mapImportPathSpy).toHaveBeenCalledTimes(1); | ||
} | ||
); | ||
}); | ||
}); |
Oops, something went wrong.