-
Notifications
You must be signed in to change notification settings - Fork 1
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
@/CivicSignalBlog - Use aliases in Payload CMS #902
Conversation
…ng escaping issue
Just saw this custom condition approach that doesn't require extra dependencies. Did you review it / won't it work for us? We should be on TS 5.4 already. |
Hmm interesting... I hadn't looked at this one...let me quickly test it and get back to you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LG
apps/civicsignalblog/src/payload/access/applications/research.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@m453h Looks good to me, I may have missed something but why are we using #civicsignalblog/*
as opposed to @/civicsignalblog/*
that we've been using all along
@koechkevin , we have to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏽
--
Can you update the PR description to match this new direction we've taken?
Sure, I was planning to do that...I just need to resolve one minor issue with the imports, currently we cant implicitly import from folders (i.e. the index.js file doesn't get resolved automatically thus we are are forced to import such files by explicitly appending e.g. in
|
@CodeForAfrica/tech is this PR a go or a no-go? 👀 🤞 |
kaende |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
This pull request includes several changes to the
civicsignalblog
application, primarily focusing on using Path aliases for cleaner imports.Additionally, it fixes failing
build-next
due to ESLint that were noted after merging #901.While trying to use path aliases, it was noted that Payload does not resolve the paths as expected, this is dicussed here.
Thus, to resolve this we have used Subpath Import Aliases with custom conditions to ensure that paths are correctly resolved when we run the application in both prod and dev mode.
This has been achieved by:
Adding the
imports
field to thepackage.json
and setting the alias for thesrc
folder to#civicsignalblog
. It should be noted that as per the documentation of sub path imports (Entries in the "imports" field must always start with # to ensure they are disambiguated from external package specifiers.)Making the following changes in
tsconfig.json
andtsconfig.server.json
:"module": "esnext",
to"module": "NodeNext"
"moduleResolution": "node"
to"moduleResolution": "NodeNext"
These changes have been made because subpath imports need a moduleResolution of
Node16
orNodeNext
.Adding the same alias
#civicsignalblog
i.e. in webpackresolve > alias
configpayload.config.ts
Using custom conditions in
package.json
which allows already built code to be imported fromdist/
instead ofsrc/
. We have used two conditions wheredev
resolves tosrc/
and default that resolves todist/
thus updated thedev
script with--conditions=dev
Adding
eslint-import-resolver-typescript
plugin for enabling ESLint to resolve import statements.This has been done as an alternative solution that used two packages
tsconfig-paths
and tsc alias which involved the following steps:dev
script with-r tsconfig-paths/register
enabling ts-node to correctly resolve path aliases defined intsconfig.json
tsconfigPathToWebpackAlias.ts
that create path aliases that are used inpayload.config.ts
build-next
script to includetsc-alias -p tsconfig.server.json
that replaces alias paths with relative paths after typescript compilation.P.S
The PR might have many modified files but they are mainly adding aliases, one caveat with the current implementation is explained here where we are forced to explicitly include the filename in our imports even for cases where we have an
index.js
thus in some cases you will see that we are usingimport slug from "#civicsignalblog/payload/fields/slug/index";
Instead of
import slug from "#civicsignalblog/payload/fields/slug";
An issue that closely relates to this limitation (though it mainly discusses about exports) can be seen here.
Type of change
Checklist: