Skip to content
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
15 changes: 6 additions & 9 deletions packages/code-inspector-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { TurbopackCodeInspectorPlugin } from '@code-inspector/turbopack';
import { MakoCodeInspectorPlugin } from '@code-inspector/mako';
import {
CodeOptions,
fileURLToPath,
getEnvVariable,
resetFileRecord,
} from '@code-inspector/core';
import chalk from 'chalk';
import path, { dirname } from 'path';
import path from 'path';

export interface CodeInspectorPluginOptions extends CodeOptions {
/**
Expand Down Expand Up @@ -39,16 +38,14 @@ export function CodeInspectorPlugin(options: CodeInspectorPluginOptions): any {
}
}

let compatibleDirname = '';
if (typeof __dirname !== 'undefined') {
compatibleDirname = __dirname;
} else {
compatibleDirname = dirname(fileURLToPath(import.meta.url));
}
// Write generated files to user project's node_modules/.cache directory
// This ensures relative imports work correctly with pnpm link
const outputDir = path.resolve(process.cwd(), 'node_modules/.cache/code-inspector');

const params = {
...options,
close,
output: path.resolve(compatibleDirname, './'),
output: outputDir,
};
resetFileRecord(params.output);
if (options.bundler === 'webpack' || options.bundler === 'rspack') {
Expand Down
26 changes: 18 additions & 8 deletions packages/core/src/server/use-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,18 @@ export async function getCodeWithWebComponent({
);
if (isNextjs || options.importClient === 'file') {
writeEslintRcFile(record.output);
const webComponentNpmPath = writeWebComponentFile(
const webComponentPath = writeWebComponentFile(
record.output,
injectCode,
getProjectRecord(record)?.port || 0
getProjectRecord(record)?.port || 0,
file
);
if (!file.match(webComponentNpmPath)) {
if (!file.match(webComponentPath)) {
if (isNextjs) {
code = addImportToEntry(code, webComponentNpmPath);
code = addImportToEntry(code, webComponentPath);
code = addNextEmptyElementToEntry(code);
} else {
code = `import '${webComponentNpmPath}';${code}`;
code = `import '${webComponentPath}';${code}`;
}
}
} else {
Expand Down Expand Up @@ -370,13 +371,22 @@ module.exports = {
function writeWebComponentFile(
targetPath: string,
content: string,
port: number
port: number,
fromFile: string
) {
const webComponentFileName = `append-code-${port}.js`;
const webComponentNpmPath = `code-inspector-plugin/dist/${webComponentFileName}`;
const webComponentFilePath = path.resolve(targetPath, webComponentFileName);
fs.writeFileSync(webComponentFilePath, content, 'utf-8');
return webComponentNpmPath;

// Calculate relative path from the importing file to the generated file
// This works with pnpm link and is required by Next.js
const relativePath = path.relative(path.dirname(fromFile), webComponentFilePath);
// Ensure path starts with ./ or ../
const normalizedRelative = relativePath.startsWith('.')
? relativePath
: `./${relativePath}`;

return normalizePath(normalizedRelative);
}

export function isNextjsProject() {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/shared/record-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import fs from 'fs';
import { RecordInfo } from './type';

export const resetFileRecord = (output: string) => {
// Ensure output directory exists
if (!fs.existsSync(output)) {
fs.mkdirSync(output, { recursive: true });
}

const recordFilePath = path.resolve(output, './record.json');
const projectDir = process.cwd();
let content: {
Expand Down