forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(remix-dev): add ability to use tsconfig
paths
aliases other th…
…an `~` (remix-run#2412) Co-authored-by: Jacob Ebey <jacob.ebey@live.com> Co-authored-by: Logan McAnsh <logan@mcan.sh>
- Loading branch information
1 parent
f75866f
commit 43ebd00
Showing
11 changed files
with
912 additions
and
513 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,7 @@ | |
- eps1lon | ||
- evanwinter | ||
- exegeteio | ||
- F3n67u | ||
- fergusmeiklejohn | ||
- fgiuliani | ||
- fishel-feng | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { | ||
createAppFixture, | ||
createFixture, | ||
js, | ||
json, | ||
mdx, | ||
} from "./helpers/create-fixture"; | ||
import type { Fixture, AppFixture } from "./helpers/create-fixture"; | ||
|
||
let fixture: Fixture; | ||
let app: AppFixture; | ||
|
||
beforeAll(async () => { | ||
fixture = await createFixture({ | ||
files: { | ||
"app/components/my-lib/index.ts": js` | ||
export const pizza = "this is a pizza"; | ||
`, | ||
|
||
"app/routes/index.tsx": js` | ||
import { pizza } from "@mylib"; | ||
import { json, useLoaderData, Link } from "remix"; | ||
export function loader() { | ||
return json(pizza); | ||
} | ||
export default function Index() { | ||
let data = useLoaderData(); | ||
return ( | ||
<div> | ||
{data} | ||
</div> | ||
) | ||
} | ||
`, | ||
|
||
"app/routes/tilde-alias.tsx": js` | ||
import { pizza } from "~/components/my-lib"; | ||
import { json, useLoaderData, Link } from "remix"; | ||
export function loader() { | ||
return json(pizza); | ||
} | ||
export default function Index() { | ||
let data = useLoaderData(); | ||
return ( | ||
<div> | ||
{data} | ||
</div> | ||
) | ||
} | ||
`, | ||
|
||
"app/components/component.jsx": js` | ||
export function PizzaComponent() { | ||
return <span>this is a pizza</span> | ||
} | ||
`, | ||
|
||
"app/routes/mdx.mdx": mdx` | ||
--- | ||
meta: | ||
title: My First Post | ||
description: Isn't this awesome? | ||
headers: | ||
Cache-Control: no-cache | ||
--- | ||
import { PizzaComponent } from "@component"; | ||
# Hello MDX! | ||
This is my first post. | ||
<PizzaComponent /> | ||
`, | ||
|
||
"tsconfig.json": json` | ||
{ | ||
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"], | ||
"compilerOptions": { | ||
"lib": ["DOM", "DOM.Iterable", "ES2019"], | ||
"isolatedModules": true, | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"target": "ES2019", | ||
"strict": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"~/*": ["./app/*"], | ||
"@mylib": ["./app/components/my-lib/index"], | ||
"@component": ["./app/components/component.jsx"], | ||
}, | ||
// Remix takes care of building everything in \`remix build\`. | ||
"noEmit": true | ||
} | ||
} | ||
`, | ||
}, | ||
}); | ||
|
||
app = await createAppFixture(fixture); | ||
}); | ||
|
||
afterAll(async () => app.close()); | ||
|
||
it("import internal library via alias other than ~", async () => { | ||
// test for https://github.com/remix-run/remix/issues/2298 | ||
let response = await fixture.requestDocument("/"); | ||
expect(await response.text()).toMatch("this is a pizza"); | ||
}); | ||
|
||
it("import internal library via ~ alias", async () => { | ||
let response = await fixture.requestDocument("/tilde-alias"); | ||
expect(await response.text()).toMatch("this is a pizza"); | ||
}); | ||
|
||
it("works for mdx files", async () => { | ||
let response = await fixture.requestDocument("/mdx"); | ||
expect(await response.text()).toMatch("this is a pizza"); | ||
}); |
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
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
65 changes: 65 additions & 0 deletions
65
packages/remix-dev/compiler/utils/tsconfig/configLoader.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,65 @@ | ||
import * as path from "path"; | ||
|
||
import { tsConfigLoader } from "./tsConfigLoader"; | ||
|
||
export interface ConfigLoaderParams { | ||
cwd: string; | ||
} | ||
|
||
export interface ConfigLoaderSuccessResult { | ||
resultType: "success"; | ||
configFileAbsolutePath: string; | ||
baseUrl: string; | ||
absoluteBaseUrl: string; | ||
paths: { [key: string]: Array<string> }; | ||
mainFields?: Array<string>; | ||
addMatchAll?: boolean; | ||
} | ||
|
||
export interface ConfigLoaderFailResult { | ||
resultType: "failed"; | ||
message: string; | ||
} | ||
|
||
export type ConfigLoaderResult = | ||
| ConfigLoaderSuccessResult | ||
| ConfigLoaderFailResult; | ||
|
||
export function loadTsConfig(cwd: string = process.cwd()): ConfigLoaderResult { | ||
return configLoader({ cwd: cwd }); | ||
} | ||
|
||
export function configLoader({ | ||
cwd, | ||
}: ConfigLoaderParams): ConfigLoaderResult { | ||
// Load tsconfig and create path matching function | ||
let loadResult = tsConfigLoader({ | ||
cwd, | ||
getEnv: (key: string) => process.env[key], | ||
}); | ||
|
||
if (!loadResult.tsConfigPath) { | ||
return { | ||
resultType: "failed", | ||
message: "Couldn't find tsconfig.json", | ||
}; | ||
} | ||
|
||
if (!loadResult.baseUrl) { | ||
return { | ||
resultType: "failed", | ||
message: "Missing baseUrl in compilerOptions", | ||
}; | ||
} | ||
|
||
let tsConfigDir = path.dirname(loadResult.tsConfigPath); | ||
let absoluteBaseUrl = path.join(tsConfigDir, loadResult.baseUrl); | ||
|
||
return { | ||
resultType: "success", | ||
configFileAbsolutePath: loadResult.tsConfigPath, | ||
baseUrl: loadResult.baseUrl, | ||
absoluteBaseUrl, | ||
paths: loadResult.paths || {}, | ||
}; | ||
} |
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,20 @@ | ||
import tsConfigPaths from "tsconfig-paths"; | ||
|
||
import { loadTsConfig } from "./configLoader"; | ||
export { loadTsConfig } from "./configLoader"; | ||
|
||
export function createMatchPath() { | ||
let configLoaderResult = loadTsConfig(); | ||
if (configLoaderResult.resultType === "failed") { | ||
return undefined; | ||
} | ||
|
||
let matchPath = tsConfigPaths.createMatchPath( | ||
configLoaderResult.absoluteBaseUrl, | ||
configLoaderResult.paths, | ||
configLoaderResult.mainFields, | ||
configLoaderResult.addMatchAll | ||
); | ||
|
||
return matchPath; | ||
} |
Oops, something went wrong.