Skip to content

Commit 24625fa

Browse files
committed
fix: improve pnpm link compatibility and file path resolution
Resolve MODULE_NOT_FOUND errors when using pnpm link for local development. Problems solved: 1. Use relative paths instead of NPM specifiers for generated files 2. Write generated files to project cache dir (node_modules/.code-inspector) 3. Ensure cache directory exists before write operations 4. Better Next.js compatibility with relative path imports This enables smooth local development workflow: - pnpm/npm/yarn link support - Testing unpublished changes in real projects - Better contributor development workflow Changes: - packages/core/src/server/use-client.ts: Switch to relative path imports - packages/code-inspector-plugin/src/index.ts: Use project cache directory - packages/core/src/shared/record-cache.ts: Add directory creation
1 parent e54252d commit 24625fa

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

packages/code-inspector-plugin/src/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import { TurbopackCodeInspectorPlugin } from '@code-inspector/turbopack';
55
import { MakoCodeInspectorPlugin } from '@code-inspector/mako';
66
import {
77
CodeOptions,
8-
fileURLToPath,
98
getEnvVariable,
109
resetFileRecord,
1110
} from '@code-inspector/core';
1211
import chalk from 'chalk';
13-
import path, { dirname } from 'path';
12+
import path from 'path';
1413

1514
export interface CodeInspectorPluginOptions extends CodeOptions {
1615
/**
@@ -39,16 +38,14 @@ export function CodeInspectorPlugin(options: CodeInspectorPluginOptions): any {
3938
}
4039
}
4140

42-
let compatibleDirname = '';
43-
if (typeof __dirname !== 'undefined') {
44-
compatibleDirname = __dirname;
45-
} else {
46-
compatibleDirname = dirname(fileURLToPath(import.meta.url));
47-
}
41+
// Write generated files to user project's node_modules/.cache directory
42+
// This ensures relative imports work correctly with pnpm link
43+
const outputDir = path.resolve(process.cwd(), 'node_modules/.cache/code-inspector');
44+
4845
const params = {
4946
...options,
5047
close,
51-
output: path.resolve(compatibleDirname, './'),
48+
output: outputDir,
5249
};
5350
resetFileRecord(params.output);
5451
if (options.bundler === 'webpack' || options.bundler === 'rspack') {

packages/core/src/server/use-client.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,18 @@ export async function getCodeWithWebComponent({
338338
);
339339
if (isNextjs || options.importClient === 'file') {
340340
writeEslintRcFile(record.output);
341-
const webComponentNpmPath = writeWebComponentFile(
341+
const webComponentPath = writeWebComponentFile(
342342
record.output,
343343
injectCode,
344-
getProjectRecord(record)?.port || 0
344+
getProjectRecord(record)?.port || 0,
345+
file
345346
);
346-
if (!file.match(webComponentNpmPath)) {
347+
if (!file.match(webComponentPath)) {
347348
if (isNextjs) {
348-
code = addImportToEntry(code, webComponentNpmPath);
349+
code = addImportToEntry(code, webComponentPath);
349350
code = addNextEmptyElementToEntry(code);
350351
} else {
351-
code = `import '${webComponentNpmPath}';${code}`;
352+
code = `import '${webComponentPath}';${code}`;
352353
}
353354
}
354355
} else {
@@ -376,13 +377,22 @@ module.exports = {
376377
function writeWebComponentFile(
377378
targetPath: string,
378379
content: string,
379-
port: number
380+
port: number,
381+
fromFile: string
380382
) {
381383
const webComponentFileName = `append-code-${port}.js`;
382-
const webComponentNpmPath = `code-inspector-plugin/dist/${webComponentFileName}`;
383384
const webComponentFilePath = path.resolve(targetPath, webComponentFileName);
384385
fs.writeFileSync(webComponentFilePath, content, 'utf-8');
385-
return webComponentNpmPath;
386+
387+
// Calculate relative path from the importing file to the generated file
388+
// This works with pnpm link and is required by Next.js
389+
const relativePath = path.relative(path.dirname(fromFile), webComponentFilePath);
390+
// Ensure path starts with ./ or ../
391+
const normalizedRelative = relativePath.startsWith('.')
392+
? relativePath
393+
: `./${relativePath}`;
394+
395+
return normalizePath(normalizedRelative);
386396
}
387397

388398
export function isNextjsProject() {

packages/core/src/shared/record-cache.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import fs from 'fs';
33
import { RecordInfo } from './type';
44

55
export const resetFileRecord = (output: string) => {
6+
// Ensure output directory exists
7+
if (!fs.existsSync(output)) {
8+
fs.mkdirSync(output, { recursive: true });
9+
}
10+
611
const recordFilePath = path.resolve(output, './record.json');
712
const projectDir = process.cwd();
813
let content: {

0 commit comments

Comments
 (0)