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

feat: project references #32

Open
wants to merge 6 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
73 changes: 59 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,47 @@ const isPathPattern = /^\.{0,2}\//;
const isTsFilePatten = /\.[cm]?tsx?$/;
const nodeModulesPath = `${path.sep}node_modules${path.sep}`;

const tsconfig = (
process.env.ESBK_TSCONFIG_PATH
? {
path: path.resolve(process.env.ESBK_TSCONFIG_PATH),
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
}
: getTsconfig()
);
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;
}

const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
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);

const applySourceMap = installSourceMapSupport();

Expand Down Expand Up @@ -67,11 +97,18 @@ function transformer(
code = applySourceMap(transformed, filePath);
}
} else {
let tsconfigRaw: TransformOptions['tsconfigRaw'];
for (const project of projectsMap.values()) {
tsconfigRaw = project.fileMatcher(filePath) as TransformOptions['tsconfigRaw'];
if (tsconfigRaw) {
break;
}
}
const transformed = transformSync(
code,
filePath,
{
tsconfigRaw: fileMatcher?.(filePath) as TransformOptions['tsconfigRaw'],
tsconfigRaw,
},
);

Expand Down Expand Up @@ -131,15 +168,23 @@ Module._resolveFilename = function (request, parent, isMain, options) {
}

if (
tsconfigPathsMatcher
projectsMap.size > 0

// bare specifier
&& !isPathPattern.test(request)

// Dependency paths should not be resolved using tsconfig.json
&& !parent?.filename?.includes(nodeModulesPath)
) {
const possiblePaths = tsconfigPathsMatcher(request);
const possiblePaths: string[] = [];
projectsMap.forEach((project) => {
if (project.tsconfigPathsMatcher) {
const possibleProjectPaths = project.tsconfigPathsMatcher(request);
if (possibleProjectPaths) {
possiblePaths.push(...possibleProjectPaths);
}
}
});

for (const possiblePath of possiblePaths) {
const tsFilename = resolveTsFilename.call(this, possiblePath, parent, isMain, options);
Expand All @@ -155,7 +200,7 @@ Module._resolveFilename = function (request, parent, isMain, options) {
isMain,
options,
);
} catch {}
} catch { }
}
}

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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"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";
11 changes: 11 additions & 0 deletions tests/fixtures/tsconfig/resolve-project-reference/tsconfig.json
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/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 @@ -181,6 +181,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