Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support alias paths on vite #9242

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion packages/babel-config/src/__tests__/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe('common', () => {
expect(webPaths).toMatchInlineSnapshot(`{}`)
})

it('gets and formats paths', () => {
it('gets and formats paths (legacy:webpack)', () => {
const apiTSConfig =
'{"compilerOptions":{"baseUrl":"./","paths":{"@services/*":["./src/services/*"]}}}'
const webTSConfig =
Expand Down Expand Up @@ -210,6 +210,41 @@ describe('common', () => {
`"src/ui"`
)
})

it('gets and formats paths for vite', () => {
const apiTSConfig =
'{"compilerOptions":{"baseUrl":"./","paths":{"@services/*":["./src/services/*"]}}}'
const webTSConfig =
'{"compilerOptions":{"baseUrl":"./","paths":{"@ui/*":["./src/ui/*"]}}}'

vol.fromNestedJSON(
{
'redwood.toml': '',
api: {
'tsconfig.json': apiTSConfig,
},
web: {
'tsconfig.json': webTSConfig,
},
},
redwoodProjectPath
)

const typeScriptConfig = parseTypeScriptConfigFiles()

const apiPaths = getPathsFromTypeScriptConfig(typeScriptConfig.api)
expect(ensurePosixPath(apiPaths['@services'])).toMatchInlineSnapshot(
`"src/services"`
)

const webPaths = getPathsFromTypeScriptConfig(
typeScriptConfig.web,
true
)
expect(ensurePosixPath(webPaths['@ui'])).toMatchInlineSnapshot(
`"./src/ui"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @esteban-url just to check - why is the ./ required for vite? src/ui should still be working I believe?

)
})
})

it('handles invalid JSON', () => {
Expand Down
21 changes: 14 additions & 7 deletions packages/babel-config/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ export const parseTypeScriptConfigFiles = () => {
* @param config The config object
* @returns {Record<string, string>} The paths object
*/
export const getPathsFromTypeScriptConfig = (config: {
compilerOptions: { baseUrl: string; paths: string }
}): Record<string, string> => {
export const getPathsFromTypeScriptConfig = (
config: {
compilerOptions: { baseUrl: string; paths: string }
},
forVite = false
): Record<string, string> => {
if (!config) {
return {}
}
Expand All @@ -150,11 +153,15 @@ export const getPathsFromTypeScriptConfig = (config: {
if (key.match(/src\/|\$api\/\*|types\/\*|\@redwoodjs\/.*/g)) {
continue
}
let aliasValue = ''
const aliasKey = key.replace('/*', '')
const aliasValue = path.join(
baseUrl,
(value as string)[0].replace('/*', '')
)

// keeping legacy (webpack) compatibility
if (forVite) {
aliasValue = (value as string)[0].replace('/*', '')
} else {
aliasValue = path.join(baseUrl, (value as string)[0].replace('/*', ''))
}
pathsObj[aliasKey] = aliasValue
}
return pathsObj
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-config/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const getWebSideBabelPlugins = (
// the `cwd`: https://github.com/facebook/jest/issues/7359
forJest ? rwjsPaths.web.src : './src',
// adds the paths from [ts|js]config.json to the module resolver
...getPathsFromTypeScriptConfig(tsConfigs.web),
...getPathsFromTypeScriptConfig(tsConfigs.web, forVite),
$api: rwjsPaths.api.base,
},
root: [rwjsPaths.web.base],
Expand Down
Loading