Skip to content

Commit

Permalink
fix(path-alias): Fix aliasing of paths using ts/jsconfig (#9574)
Browse files Browse the repository at this point in the history
Co-authored-by: Dominic Saadi <dominiceliassaadi@gmail.com>
  • Loading branch information
2 people authored and Tobbe committed Jan 1, 2024
1 parent 68c1c05 commit e534e9f
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 210 deletions.
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"],"types": ["jest", "@testing-library/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

0 comments on commit e534e9f

Please sign in to comment.