Skip to content
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"fastify": "^5.3.3",
"node-fetch": "^2.6.7",
"prettier": "^3.1.0",
"tsconfig-paths": "^4.2.0",
"ws": "^8.18.0"
},
"pnpm": {
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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

74 changes: 38 additions & 36 deletions src/webpack-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type { Compilation, Compiler, Module as WebpackModule } from "webpack";
import { sources } from "webpack";
import fs from "fs";
import path from "path";
import { loadConfig } from "tsconfig-paths";

interface Asset {
name: string;
Expand Down Expand Up @@ -108,51 +109,52 @@ export default class CodePressWebpackPlugin {
}
}

// Always try to read @ alias from tsconfig.json if not already present
// resolve.alias usually has Next.js internals but not the @ path alias
if (!aliases.has("@")) {
const tsconfigPath = path.join(compiler.context, "tsconfig.json");

// Load path aliases from tsconfig.json using tsconfig-paths library
// This properly handles:
// - JSON with comments (TypeScript's JSON5-like syntax)
// - The "extends" field to resolve inherited configurations
// - Complex path patterns
const tsconfigPath = path.join(compiler.context, "tsconfig.json");
if (fs.existsSync(tsconfigPath)) {
try {
if (fs.existsSync(tsconfigPath)) {
const tsconfigContent = fs.readFileSync(tsconfigPath, "utf8");

// Extract paths directly using regex (avoids JSON parsing issues with comments/globs)
// Match: "paths": { "@/*": ["./src/*"] } or similar
const pathsMatch = tsconfigContent.match(
/"paths"\s*:\s*\{([^}]+)\}/
);

if (pathsMatch) {
const pathsContent = pathsMatch[1];

// Extract individual path mappings: "@/*": ["./src/*"]
const pathPattern = /"([^"]+)"\s*:\s*\[\s*"([^"]+)"/g;
let match;
while ((match = pathPattern.exec(pathsContent)) !== null) {
const aliasPattern = match[1]; // "@/*"
const targetPattern = match[2]; // "./src/*"

// Convert "@/*" -> "@" and "./src/*" -> "src"
const alias = aliasPattern.replace(/\/\*$/, "");
const targetPath = targetPattern
.replace(/^\.\//, "")
.replace(/\/\*$/, "");
const config = loadConfig(compiler.context);

if (config.resultType === "success" && config.paths) {
for (const [pattern, targets] of Object.entries(config.paths)) {
// Convert "@/*" -> "@" and "./src/*" -> "src"
const alias = pattern.replace(/\/\*$/, "");

// Skip if we already have this alias from webpack config
if (aliases.has(alias)) continue;

// Get the first target path
const targetPattern = targets[0];
if (targetPattern) {
// Convert absolute or relative path to relative directory
let targetPath = targetPattern
.replace(/\/\*$/, "") // Remove trailing /*
.replace(/^\.\//, ""); // Remove leading ./

// If it's an absolute path, make it relative to project root
if (path.isAbsolute(targetPath)) {
targetPath = path.relative(compiler.context, targetPath);
}

aliases.set(alias, targetPath);
}
}
}
} catch (e) {
console.warn("[CodePress] Error reading tsconfig.json:", e);
console.warn("[CodePress] Error loading tsconfig.json paths:", e);
}
}

// Fallback: Next.js convention is @/* -> ./src/*
if (!aliases.has("@")) {
const srcDir = path.join(compiler.context, "src");
if (fs.existsSync(srcDir)) {
aliases.set("@", "src");
}
// Fallback: Next.js convention is @/* -> ./src/*
// Only applies if no @ alias was configured
if (!aliases.has("@")) {
const srcDir = path.join(compiler.context, "src");
if (fs.existsSync(srcDir)) {
aliases.set("@", "src");
}
}

Expand Down
Loading