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

feat(remix-dev): add tsconfigPath config option to RemixConfig #3936

Merged
merged 5 commits into from Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
Expand Up @@ -209,6 +209,7 @@
- juwiragiye
- jveldridge
- jvnm-dev
- jwnx
- kalch
- kanermichael
- karimsan
Expand Down
2 changes: 2 additions & 0 deletions packages/remix-dev/__tests__/readConfig-test.ts
Expand Up @@ -21,6 +21,7 @@ describe("readConfig", () => {
serverBuildPath: expect.any(String),
assetsBuildDirectory: expect.any(String),
relativeAssetsBuildDirectory: expect.any(String),
tsconfigPath: expect.any(String)
},
`
Object {
Expand Down Expand Up @@ -50,6 +51,7 @@ describe("readConfig", () => {
"serverMode": "production",
"serverModuleFormat": "cjs",
"serverPlatform": "node",
"tsconfigPath": Any<String>,
"watchPaths": Array [],
}
`
Expand Down
8 changes: 8 additions & 0 deletions packages/remix-dev/compiler.ts
Expand Up @@ -374,6 +374,10 @@ async function createBrowserBuild(
sourcemap: options.sourcemap,
metafile: true,
incremental: options.incremental,
// As pointed out by https://github.com/evanw/esbuild/issues/2440, when tsconfig is set to
// `undefined`, esbuild will keep looking for a tsconfig.json recursively up. This unwanted
// behavior can only be avoided by creating an empty tsconfig file in the root directory.
tsconfig: config.tsconfigPath,
mainFields: ["browser", "module", "main"],
treeShaking: true,
minify: options.mode === BuildMode.Production,
Expand Down Expand Up @@ -463,6 +467,10 @@ function createServerBuild(
loader: loaders,
bundle: true,
logLevel: "silent",
// As pointed out by https://github.com/evanw/esbuild/issues/2440, when tsconfig is set to
// `undefined`, esbuild will keep looking for a tsconfig.json recursively up. This unwanted
// behavior can only be avoided by creating an empty tsconfig file in the root directory.
tsconfig: config.tsconfigPath,
incremental: options.incremental,
sourcemap: options.sourcemap, // use linked (true) to fix up .map file
// The server build needs to know how to generate asset URLs for imports
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-dev/compiler/plugins/mdx.ts
Expand Up @@ -17,7 +17,7 @@ export function mdxPlugin(config: RemixConfig): esbuild.Plugin {
]);

build.onResolve({ filter: /\.mdx?$/ }, (args) => {
let matchPath = createMatchPath();
let matchPath = createMatchPath(config.tsconfigPath);
// Resolve paths according to tsconfig paths property
function resolvePath(id: string) {
if (!matchPath) {
Expand Down
Expand Up @@ -23,7 +23,7 @@ export function serverBareModulesPlugin(
let isDenoRuntime = remixConfig.serverBuildTarget === "deno";

// Resolve paths according to tsconfig paths property
let matchPath = isDenoRuntime ? undefined : createMatchPath();
let matchPath = isDenoRuntime ? undefined : createMatchPath(remixConfig.tsconfigPath);
function resolvePath(id: string) {
if (!matchPath) {
return id;
Expand Down
13 changes: 11 additions & 2 deletions packages/remix-dev/compiler/utils/tsconfig/index.ts
Expand Up @@ -2,8 +2,17 @@ import tsConfigPaths from "tsconfig-paths";

import { writeConfigDefaults } from "./write-config-defaults";

export function createMatchPath() {
let configLoaderResult = tsConfigPaths.loadConfig();
export function createMatchPath(tsconfigPath: string | undefined) {
// There is no tsconfig to match paths against.
if (!tsconfigPath) {
return undefined;
}

// When passing a absolute path, loadConfig assumes that the path contains
// a tsconfig file.
// Ref.: https://github.com/dividab/tsconfig-paths/blob/v4.0.0/src/__tests__/config-loader.test.ts#L74
let configLoaderResult = tsConfigPaths.loadConfig(tsconfigPath);

if (configLoaderResult.resultType === "failed") {
if (configLoaderResult.message === "Missing baseUrl in compilerOptions") {
throw new Error(
Expand Down
15 changes: 15 additions & 0 deletions packages/remix-dev/config.ts
Expand Up @@ -269,6 +269,11 @@ export interface RemixConfig {
* A list of directories to watch.
*/
watchPaths: string[];

/**
* The path for the tsconfig file, if present on the root directory.
*/
tsconfigPath: string | undefined;
}

/**
Expand Down Expand Up @@ -450,6 +455,15 @@ export async function readConfig(

let serverDependenciesToBundle = appConfig.serverDependenciesToBundle || [];

// When tsconfigPath is undefined, the default "tsconfig.json" is not
// found in the root directory.
let tsconfigDefaultFilename = "tsconfig.json"
Copy link
Collaborator

Choose a reason for hiding this comment

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

do you happen to know if this all works as expected if you use a jsconfig.json file and pass that to esbuild?

Copy link
Collaborator

Choose a reason for hiding this comment

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

it appears that it does, can you update this to also check for jsconfig.json? i think something like this should be sufficient https://github.com/remix-run/remix/pull/3012/files#diff-852e3f3cc491a6fcc66706c94417be59f4c12569e0f367354613bfc103df7b38R299-R307

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. There you go!

Copy link
Collaborator

Choose a reason for hiding this comment

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

:chefkiss:

let tsconfigPath: string | undefined = undefined

if (fse.existsSync(path.resolve(rootDirectory, tsconfigDefaultFilename))) {
tsconfigPath = path.resolve(rootDirectory, tsconfigDefaultFilename)
}

return {
appDirectory,
cacheDirectory,
Expand All @@ -472,6 +486,7 @@ export async function readConfig(
serverDependenciesToBundle,
mdx,
watchPaths,
tsconfigPath,
};
}

Expand Down