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

fix: windows-compatibility-for-tsconfig-generation-script #97

Closed
Closed
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
44 changes: 30 additions & 14 deletions packages/web-components/scripts/addPreactAliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,44 @@ import { createRequire } from "node:module";

const require = createRequire(import.meta.url);

// Helper to convert a path to POSIX style (using forward slashes)
const toPosixPath = p => p.split(path.sep).join("/");

// Helper to ensure a path ends with a trailing slash
const ensureTrailingSlash = p => p.endsWith("/") || p.endsWith("*") ? p : `${p}/`;

const addPreactAliases = async () => {
// Determine the tsconfig.json location (assumed to be in the project root)
// Resolve the project root tsconfig.json
const tsconfigPath = path.resolve("tsconfig.json");

const content = await readFile(tsconfigPath, "utf8");
const tsconfig = JSON.parse(content);

tsconfig.compilerOptions = tsconfig.compilerOptions || {};
tsconfig.compilerOptions.paths = tsconfig.compilerOptions.paths || {};

const preactCompatFile = require.resolve("preact/compat");
const preactJsxRuntimeFile = require.resolve("preact/jsx-runtime");


const preactCompatDir = path.relative(process.cwd(), path.join(path.dirname(preactCompatFile), ".."));
const preactJsxRuntimeDir = path.relative(process.cwd(), path.join(path.dirname(preactJsxRuntimeFile), ".."));

// Update the tsconfig paths with trailing separator so TypeScript treats them as directories
tsconfig.compilerOptions.paths.react = [ preactCompatDir + path.sep ];
tsconfig.compilerOptions.paths["react/jsx-runtime"] = [ preactJsxRuntimeDir + path.sep ];
tsconfig.compilerOptions.paths["react-dom"] = [ preactCompatDir + path.sep ];
tsconfig.compilerOptions.paths["react-dom/*"] = [ `${preactCompatDir + path.sep }*` ];
// Get preact's package root by resolving its package.json
const preactPkgJson = require.resolve("preact/package.json");
const preactRoot = path.dirname(preactPkgJson);

// Instead of pointing to a dist folder, point to the package directories:
// preact/compat for "react" and "react-dom", and preact/jsx-runtime for "react/jsx-runtime"
const preactCompatDir = path.join(preactRoot, "compat");
const preactJsxRuntimeDir = path.join(preactRoot, "jsx-runtime");

// Compute relative, POSIX-style paths from the project root
const relativePreactCompat = toPosixPath(path.relative(process.cwd(), preactCompatDir));
const relativePreactJsxRuntime = toPosixPath(path.relative(process.cwd(), preactJsxRuntimeDir));

// Ensure each path has a trailing slash as required by tsconfig "paths"
const posixPreactCompat = ensureTrailingSlash(relativePreactCompat);
const posixPreactJsxRuntime = ensureTrailingSlash(relativePreactJsxRuntime);
const posixReactDomWildcard = ensureTrailingSlash(toPosixPath(path.join(posixPreactCompat, "*")));

// Update tsconfig paths:
tsconfig.compilerOptions.paths.react = [posixPreactCompat];
tsconfig.compilerOptions.paths["react/jsx-runtime"] = [posixPreactJsxRuntime];
tsconfig.compilerOptions.paths["react-dom"] = [posixPreactCompat];
tsconfig.compilerOptions.paths["react-dom/*"] = [posixReactDomWildcard];

await writeFile(tsconfigPath, JSON.stringify(tsconfig, null, 2), "utf8");
};
Expand Down