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

fix(path-alias): Fix aliasing of paths using ts/jsconfig #9574

Merged
merged 19 commits into from
Dec 27, 2023
Merged
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
208 changes: 4 additions & 204 deletions packages/babel-config/src/__tests__/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { vol } from 'memfs'

import { ensurePosixPath } from '@redwoodjs/project-config'

import {
getCommonPlugins,
getPathsFromTypeScriptConfig,
parseTypeScriptConfigFiles,
} from '../common'

jest.mock('fs', () => require('memfs').fs)
import { getCommonPlugins } from '../common'

const redwoodProjectPath = '/redwood-app'
process.env.RWJS_CWD = redwoodProjectPath
Expand All @@ -17,11 +9,10 @@ afterEach(() => {
vol.reset()
})

describe('common', () => {
it("common plugins haven't changed unintentionally", () => {
const commonPlugins = getCommonPlugins()
test("common plugins haven't changed unintentionally", () => {
const commonPlugins = getCommonPlugins()

expect(commonPlugins).toMatchInlineSnapshot(`
expect(commonPlugins).toMatchInlineSnapshot(`
[
[
"@babel/plugin-transform-class-properties",
Expand All @@ -43,195 +34,4 @@ describe('common', () => {
],
]
`)
})

describe('TypeScript config files', () => {
it("returns `null` if it can't find TypeScript config files", () => {
vol.fromNestedJSON(
{
'redwood.toml': '',
api: {},
web: {},
},
redwoodProjectPath
)

const typeScriptConfig = parseTypeScriptConfigFiles()
expect(typeScriptConfig).toHaveProperty('api', null)
expect(typeScriptConfig).toHaveProperty('web', null)
})

it('finds and parses tsconfig.json files', () => {
const apiTSConfig = '{"compilerOptions": {"noEmit": true}}'
const webTSConfig = '{"compilerOptions": {"allowJs": true}}'

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

const typeScriptConfig = parseTypeScriptConfigFiles()
expect(typeScriptConfig.api).toMatchObject(JSON.parse(apiTSConfig))
expect(typeScriptConfig.web).toMatchObject(JSON.parse(webTSConfig))
})

it('finds and parses jsconfig.json files', () => {
const apiJSConfig = '{"compilerOptions": {"noEmit": true}}'
const webJSConfig = '{"compilerOptions": {"allowJs": true}}'

vol.fromNestedJSON(
{
'redwood.toml': '',
api: {
'jsconfig.json': apiJSConfig,
},
web: {
'jsconfig.json': webJSConfig,
},
},
redwoodProjectPath
)

const typeScriptConfig = parseTypeScriptConfigFiles()
expect(typeScriptConfig.api).toMatchObject(JSON.parse(apiJSConfig))
expect(typeScriptConfig.web).toMatchObject(JSON.parse(webJSConfig))
})

describe('getPathsFromTypeScriptConfig', () => {
it("returns an empty object if there's no TypeScript config files", () => {
vol.fromNestedJSON(
{
'redwood.toml': '',
api: {},
web: {},
},
redwoodProjectPath
)

const typeScriptConfig = parseTypeScriptConfigFiles()

const apiPaths = getPathsFromTypeScriptConfig(typeScriptConfig.api)
expect(apiPaths).toMatchObject({})

const webPaths = getPathsFromTypeScriptConfig(typeScriptConfig.web)
expect(webPaths).toMatchObject({})
})

it("returns an empty object if there's no compilerOptions, baseUrl, or paths", () => {
const apiTSConfig = '{}'
const webTSConfig = '{"compilerOptions":{"allowJs": true}}'

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

const typeScriptConfig = parseTypeScriptConfigFiles()

const apiPaths = getPathsFromTypeScriptConfig(typeScriptConfig.api)
expect(apiPaths).toMatchInlineSnapshot(`{}`)

const webPaths = getPathsFromTypeScriptConfig(typeScriptConfig.web)
expect(webPaths).toMatchInlineSnapshot(`{}`)
})

it('excludes "src/*", "$api/*", "types/*", and "@redwoodjs/testing"', () => {
const apiTSConfig =
'{"compilerOptions":{"baseUrl":"./","paths":{"src/*":["./src/*","../.redwood/types/mirror/api/src/*"],"types/*":["./types/*","../types/*"],"@redwoodjs/testing":["../node_modules/@redwoodjs/testing/api"]}}}'
const webTSConfig =
'{"compilerOptions":{"baseUrl":"./","paths":{"src/*":["./src/*","../.redwood/types/mirror/web/src/*"],"$api/*":[ "../api/*" ],"types/*":["./types/*", "../types/*"],"@redwoodjs/testing":["../node_modules/@redwoodjs/testing/web"]}}}'

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

const typeScriptConfig = parseTypeScriptConfigFiles()

const apiPaths = getPathsFromTypeScriptConfig(typeScriptConfig.api)
expect(apiPaths).toMatchInlineSnapshot(`{}`)

const webPaths = getPathsFromTypeScriptConfig(typeScriptConfig.web)
expect(webPaths).toMatchInlineSnapshot(`{}`)
})

it('gets and formats paths', () => {
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)
expect(ensurePosixPath(webPaths['@ui'])).toMatchInlineSnapshot(
`"src/ui"`
)
})
})

it('handles invalid JSON', () => {
const apiTSConfig =
'{"compilerOptions": {"noEmit": true,"allowJs": true,"esModuleInterop": true,"target": "esnext","module": "esnext","moduleResolution": "node","baseUrl": "./","rootDirs": ["./src","../.redwood/types/mirror/api/src"],"paths": {"src/*": ["./src/*","../.redwood/types/mirror/api/src/*"],"types/*": ["./types/*", "../types/*"],"@redwoodjs/testing": ["../node_modules/@redwoodjs/testing/api"]},"typeRoots": ["../node_modules/@types","./node_modules/@types"],"types": ["jest"],},"include": ["src","../.redwood/types/includes/all-*","../.redwood/types/includes/api-*","../types"]}'
const webTSConfig =
'{"compilerOptions": {"noEmit": true,"allowJs": true,"esModuleInterop": true,"target": "esnext","module": "esnext","moduleResolution": "node","baseUrl": "./","rootDirs": ["./src","../.redwood/types/mirror/web/src","../api/src","../.redwood/types/mirror/api/src"],"paths": {"src/*": ["./src/*","../.redwood/types/mirror/web/src/*","../api/src/*","../.redwood/types/mirror/api/src/*"],"$api/*": [ "../api/*" ],"types/*": ["./types/*", "../types/*"],"@redwoodjs/testing": ["../node_modules/@redwoodjs/testing/web"]},"typeRoots": ["../node_modules/@types", "./node_modules/@types", "../node_modules/@testing-library"],"types": ["jest-dom"],"jsx": "preserve",},"include": ["src","../.redwood/types/includes/all-*","../.redwood/types/includes/web-*","../types","./types"]}'

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

expect(parseTypeScriptConfigFiles).not.toThrow()
})
})
})
Loading
Loading