Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

feat: project references #57

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions src/loaders-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import type { TransformOptions } from 'esbuild';
import {
applySourceMap,
fileMatcher,
projectsMap,
tsExtensionsPattern,
getFormatFromFileUrl,
fileProtocol,
Expand Down Expand Up @@ -82,11 +82,18 @@ const _transformSource: transformSource = async function (
url.endsWith('.json')
|| tsExtensionsPattern.test(url)
) {
let tsconfigRaw: TransformOptions['tsconfigRaw'];
for (const project of projectsMap.values()) {
tsconfigRaw = project.fileMatcher(filePath) as TransformOptions['tsconfigRaw'];
if (tsconfigRaw) {
break;
}
}
const transformed = await transform(
source.toString(),
filePath,
{
tsconfigRaw: fileMatcher?.(filePath) as TransformOptions['tsconfigRaw'],
tsconfigRaw,
},
);

Expand Down
31 changes: 22 additions & 9 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
import type { TransformOptions } from 'esbuild';
import {
applySourceMap,
tsconfigPathsMatcher,
fileMatcher,
projectsMap,
tsExtensionsPattern,
getFormatFromFileUrl,
fileProtocol,
Expand Down Expand Up @@ -78,7 +77,7 @@ async function tryDirectory(
if (!isExplicitDirectory) {
try {
return await tryExtensions(specifier, context, defaultResolve);
} catch {}
} catch { }
}

const { message } = error;
Expand Down Expand Up @@ -118,11 +117,18 @@ export const resolve: resolve = async function (
);

if (
tsconfigPathsMatcher
projectsMap.size > 0
&& !isPath // bare specifier
&& !context.parentURL?.includes('/node_modules/')
) {
const possiblePaths = tsconfigPathsMatcher(specifier);
const possiblePaths: string[] = [];
projectsMap.forEach((project) => {
if (project.tsconfigPathsMatcher) {
const possibleProjectPaths = project.tsconfigPathsMatcher(specifier);
possiblePaths.push(...possibleProjectPaths);
}
});

for (const possiblePath of possiblePaths) {
try {
return await resolve(
Expand All @@ -135,11 +141,10 @@ export const resolve: resolve = async function (
}

/**
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
*/
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
*/
if (tsExtensionsPattern.test(context.parentURL!)) {
const tsPath = resolveTsPath(specifier);

if (tsPath) {
try {
return await resolve(tsPath, context, defaultResolve, true);
Expand Down Expand Up @@ -238,11 +243,19 @@ export const load: load = async function (
loaded.format === 'json'
|| tsExtensionsPattern.test(url)
) {
let tsconfigRaw: TransformOptions['tsconfigRaw'];
for (const project of projectsMap.values()) {
tsconfigRaw = project.fileMatcher(filePath) as TransformOptions['tsconfigRaw'];
if (tsconfigRaw) {
break;
}
}

const transformed = await transform(
code,
filePath,
{
tsconfigRaw: fileMatcher?.(filePath) as TransformOptions['tsconfigRaw'],
tsconfigRaw,
},
);

Expand Down
52 changes: 41 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,47 @@ import { getPackageType } from './package-json.js';

export const applySourceMap = installSourceMapSupport();

const tsconfig = (
process.env.ESBK_TSCONFIG_PATH
? {
path: path.resolve(process.env.ESBK_TSCONFIG_PATH),
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
}
: getTsconfig()
);

export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
function getProjectsMap(tsconfigPath?: string, projectsMap?: Map<string, {
tsconfig: ReturnType<typeof getTsconfig>;
tsconfigPathsMatcher: ReturnType<typeof createPathsMatcher>;
fileMatcher: ReturnType<typeof createFilesMatcher>;
}>) {
if (!projectsMap) {
projectsMap = new Map();
}

const tsconfig = (
tsconfigPath
? {
path: path.resolve(tsconfigPath),
config: parseTsconfig(tsconfigPath),
}
: getTsconfig()
);

if (!tsconfig) {
return projectsMap;
}

if (projectsMap.has(tsconfig.path)) {
return projectsMap;
}

projectsMap.set(tsconfig.path, {
tsconfig,
tsconfigPathsMatcher: tsconfig && createPathsMatcher(tsconfig),
fileMatcher: tsconfig && createFilesMatcher(tsconfig),
});

tsconfig?.config?.references?.forEach((reference) => {
const referencedTsconfigPath = reference.path.endsWith('.json') ? reference.path : path.join(reference.path, 'tsconfig.json');
projectsMap = getProjectsMap(referencedTsconfigPath, projectsMap);
});

return projectsMap;
}

export const projectsMap = getProjectsMap(process.env.ESBK_TSCONFIG_PATH);

export const fileProtocol = 'file://';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { resolve } from "resolve-project-reference";

console.log(resolve);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "module",
"main": "src/index.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "~/resolve";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const resolve = "resolved";
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"baseUrl": ".",
"paths": {
"~/*": [
"src/*",
],
},
},
}
22 changes: 18 additions & 4 deletions tests/fixtures/package-module/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@
"jsxFragmentFactory": "null",
"baseUrl": "./src",
"paths": {
"paths-exact-match": ["resolve-target"],
"p/*": ["utils/*"],
"*/s": ["utils/*"]
"paths-exact-match": [
"resolve-target"
],
"p/*": [
"utils/*"
],
"*/s": [
"utils/*"
],
"resolve-project-reference": [
"../resolve-project-reference/src/index.ts"
]
},
},
}
"references": [
{
"path": "./resolve-project-reference"
}
]
}
7 changes: 7 additions & 0 deletions tests/specs/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
});
expect(nodeProcess.stdout).toBe('resolved');
});

test('should resolve project reference', async () => {
const nodeProcess = await node.load('./dependency-resolve-project-reference', {
cwd: './tsconfig',
});
expect(nodeProcess.stdout).toBe('resolved');
});
});
});
});
Expand Down