Skip to content

Refactor/module path aliases#18

Merged
pipewrk merged 3 commits intomainfrom
refactor/module-path-aliases
Sep 30, 2025
Merged

Refactor/module path aliases#18
pipewrk merged 3 commits intomainfrom
refactor/module-path-aliases

Conversation

@pipewrk
Copy link
Contributor

@pipewrk pipewrk commented Sep 30, 2025

What

Brief description of what this PR does.

Why

Problem or use case this solves. Link to related issues.

Fixes #

How

High-level overview of the implementation approach.

Testing

How to test this change:

  1. Step one
  2. Step two
  3. Expected result

Screenshots (if applicable)

Breaking Changes

List any breaking changes and migration steps.

Checklist

  • Tests pass (pnpm test and pnpm e2e)
  • Code is linted (pnpm lint)
  • Changeset added (pnpm changeset)
  • Documentation updated
  • Examples updated (if API changed)

- Add @kernel/* path alias to tsconfig.base.json for internal imports
- Configure Jest moduleNameMapper to resolve @kernel/* in tests
- Configure ESLint to recognize @kernel/* and @geekist/wp-kernel imports
- Add TypeScript resolver settings for import validation
- Update all relative imports to use @kernel/* aliases:
  - '../errors' → '@kernel/errors'
  - '../../types' → '@kernel/resource'
  - '../store/types' → '@kernel/resource/store/types'
- Configure test tsconfig with composite:true and project reference
- Include all source files in test tsconfig for proper resolution
- Add migration script (scripts/fix-imports.sh) for bulk replacements

All imports now use path aliases from root tsconfig.base.json.
TypeScript compilation, ESLint, and all 195 tests pass.

Fixes import clarity and maintainability as codebase grows.
Copilot AI review requested due to automatic review settings September 30, 2025 23:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces internal module path aliases to improve code organization and reduce complex relative imports within the WP Kernel package. The changes focus on establishing a @kernel/* alias system for cleaner, more maintainable imports throughout the codebase.

  • Adds @kernel/* path mapping to replace deep relative imports like ../../../errors with clean aliases
  • Creates automation script to perform bulk import replacements across the kernel package
  • Updates build and testing configurations to support the new alias system

Reviewed Changes

Copilot reviewed 13 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tsconfig.base.json Adds @kernel/* path mapping configuration for TypeScript module resolution
scripts/fix-imports.sh Automation script to replace relative imports with @kernel/ aliases throughout the codebase
packages/kernel/tsconfig.tests.json Updates test configuration to enable composite mode and broader file inclusion
packages/kernel/src/resource/store/types.ts Replaces relative import with @kernel/resource alias
packages/kernel/src/resource/store/createStore.ts Updates imports to use @kernel/ aliases for errors and resource types
packages/kernel/src/resource/store/tests/createStore.test.ts Converts test imports to use @kernel/ aliases
packages/kernel/src/resource/interpolate.ts Replaces relative error import with @kernel/errors alias
packages/kernel/src/resource/defineResource.ts Updates error import to use @kernel/errors alias
packages/kernel/src/resource/tests/interpolate.test.ts Converts test imports to @kernel/ aliases with multi-line formatting
packages/kernel/src/resource/tests/defineResource.test.ts Updates all test imports to use @kernel/ aliases
package.json Adds eslint-import-resolver-typescript dependency for proper import resolution
jest.config.cjs Configures Jest module name mapping for @kernel/ aliases
eslint.config.js Adds TypeScript import resolver configuration and import ordering rules
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment on lines +5 to +24
# Go to repo root
cd "$(dirname "$0")/.."

# Replace ../errors with @kernel/errors
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../../errors'|from '@kernel/errors'|g" {} \;

# Replace ../types with @kernel/resource for resource imports
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../types'|from '@kernel/resource'|g" {} \;
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../types'|from '@kernel/resource'|g" {} \;

# Replace ../../errors in tests
find packages/kernel/src -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../errors'|from '@kernel/errors'|g" {} \;

# Replace ../types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../types'|from '@kernel/resource'|g" {} \;

# Replace ../store/types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../store/types'|from '@kernel/resource/store/types'|g" {} \;
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script uses macOS-specific sed -i '' syntax which will fail on Linux systems. Consider using a cross-platform approach or documenting the macOS requirement.

Suggested change
# Go to repo root
cd "$(dirname "$0")/.."
# Replace ../errors with @kernel/errors
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../../errors'|from '@kernel/errors'|g" {} \;
# Replace ../types with @kernel/resource for resource imports
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../types'|from '@kernel/resource'|g" {} \;
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../types'|from '@kernel/resource'|g" {} \;
# Replace ../../errors in tests
find packages/kernel/src -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../errors'|from '@kernel/errors'|g" {} \;
# Replace ../types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../types'|from '@kernel/resource'|g" {} \;
# Replace ../store/types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../store/types'|from '@kernel/resource/store/types'|g" {} \;
# Detect platform and set SED_INPLACE variable
if [[ "$OSTYPE" == "darwin"* ]]; then
SED_INPLACE=("sed" "-i" "")
else
SED_INPLACE=("sed" "-i")
fi
# Go to repo root
cd "$(dirname "$0")/.."
# Replace ../errors with @kernel/errors
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec "${SED_INPLACE[@]}" "s|from '../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec "${SED_INPLACE[@]}" "s|from '../../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec "${SED_INPLACE[@]}" "s|from '../../../errors'|from '@kernel/errors'|g" {} \;
# Replace ../types with @kernel/resource for resource imports
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec "${SED_INPLACE[@]}" "s|from '../types'|from '@kernel/resource'|g" {} \;
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec "${SED_INPLACE[@]}" "s|from '../../types'|from '@kernel/resource'|g" {} \;
# Replace ../../errors in tests
find packages/kernel/src -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec "${SED_INPLACE[@]}" "s|from '../../errors'|from '@kernel/errors'|g" {} \;
# Replace ../types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec "${SED_INPLACE[@]}" "s|from '../types'|from '@kernel/resource'|g" {} \;
# Replace ../store/types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec "${SED_INPLACE[@]}" "s|from '../store/types'|from '@kernel/resource/store/types'|g" {} \;

Copilot uses AI. Check for mistakes.
Comment on lines +5 to +24
# Go to repo root
cd "$(dirname "$0")/.."

# Replace ../errors with @kernel/errors
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../../errors'|from '@kernel/errors'|g" {} \;

# Replace ../types with @kernel/resource for resource imports
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../types'|from '@kernel/resource'|g" {} \;
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../types'|from '@kernel/resource'|g" {} \;

# Replace ../../errors in tests
find packages/kernel/src -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../errors'|from '@kernel/errors'|g" {} \;

# Replace ../types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../types'|from '@kernel/resource'|g" {} \;

# Replace ../store/types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../store/types'|from '@kernel/resource/store/types'|g" {} \;
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script contains duplicated find command patterns. Consider extracting the common find pattern into a function to reduce repetition and improve maintainability.

Suggested change
# Go to repo root
cd "$(dirname "$0")/.."
# Replace ../errors with @kernel/errors
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../errors'|from '@kernel/errors'|g" {} \;
find packages/kernel/src -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../../errors'|from '@kernel/errors'|g" {} \;
# Replace ../types with @kernel/resource for resource imports
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../types'|from '@kernel/resource'|g" {} \;
find packages/kernel/src/resource/store -type f -name "*.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../types'|from '@kernel/resource'|g" {} \;
# Replace ../../errors in tests
find packages/kernel/src -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../../errors'|from '@kernel/errors'|g" {} \;
# Replace ../types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../types'|from '@kernel/resource'|g" {} \;
# Replace ../store/types in resource tests
find packages/kernel/src/resource/__tests__ -type f -name "*.test.ts" -not -path "*/node_modules/*" -exec sed -i '' "s|from '../store/types'|from '@kernel/resource/store/types'|g" {} \;
# Function to run find+sed for import replacement
run_find_sed() {
local search_dir="$1"
local file_pattern="$2"
local sed_expr="$3"
find "$search_dir" -type f -name "$file_pattern" -not -path "*/node_modules/*" -exec sed -i '' "$sed_expr" {} \;
}
# Go to repo root
cd "$(dirname "$0")/.."
# Replace ../errors with @kernel/errors
run_find_sed "packages/kernel/src" "*.ts" "s|from '../errors'|from '@kernel/errors'|g"
run_find_sed "packages/kernel/src" "*.ts" "s|from '../../errors'|from '@kernel/errors'|g"
run_find_sed "packages/kernel/src" "*.ts" "s|from '../../../errors'|from '@kernel/errors'|g"
# Replace ../types with @kernel/resource for resource imports
run_find_sed "packages/kernel/src/resource/store" "*.ts" "s|from '../types'|from '@kernel/resource'|g"
run_find_sed "packages/kernel/src/resource/store" "*.ts" "s|from '../../types'|from '@kernel/resource'|g"
# Replace ../../errors in tests
run_find_sed "packages/kernel/src" "*.test.ts" "s|from '../../errors'|from '@kernel/errors'|g"
# Replace ../types in resource tests
run_find_sed "packages/kernel/src/resource/__tests__" "*.test.ts" "s|from '../types'|from '@kernel/resource'|g"
# Replace ../store/types in resource tests
run_find_sed "packages/kernel/src/resource/__tests__" "*.test.ts" "s|from '../store/types'|from '@kernel/resource/store/types'|g"

Copilot uses AI. Check for mistakes.
The fix-imports.sh script was used for one-time migration to @kernel/* aliases.
No longer needed now that migration is complete.
@pipewrk pipewrk merged commit 1908c0a into main Sep 30, 2025
7 checks passed
@pipewrk pipewrk deleted the refactor/module-path-aliases branch October 5, 2025 01:48
pipewrk added a commit that referenced this pull request Nov 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants