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

ContentLayer does not follow the path aliases defined in tsconfig.json #38

Closed
zhutmost opened this issue Aug 31, 2024 · 4 comments · Fixed by #39
Closed

ContentLayer does not follow the path aliases defined in tsconfig.json #38

zhutmost opened this issue Aug 31, 2024 · 4 comments · Fixed by #39

Comments

@zhutmost
Copy link

Thanks for the developers' great work.

ContentLayer does NOT follow the path aliases defined in tsconfig.json. So I met such an error:

ConfigReadError ([repo_path]/contentlayer.config.ts): Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@/util' imported from [repo_path]/.contentlayer/.cache/v0.5.0/compiled-contentlayer-config-J3VF3BP3.mjs

when I am trying to import some packages in contentlayer.config.ts, like this:

import { createTagCount, createSearchIndex } from '@/util/PostContentLayer'

My path aliases are defined in tsconfig.json:

    ...
    "paths": {
      "@/*": ["./src/*"],
      ...
    },

Here are some related issues:

@zhutmost
Copy link
Author

zhutmost commented Aug 31, 2024

The workaround is to use the original package path instead of the corresponding alias. like this:

import { createTagCount, createSearchIndex } from './src/util/PostContentLayer'
                                                   ^^^^^

But this will make a warning in WebStorm.
image

@DopamineDriven
Copy link

DopamineDriven commented Sep 10, 2024

It works perfectly fine in vscode 🤔

here's my tsconfig

{
  "extends": "@takeda-digital/tsconfig/next.json",
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@/*": ["./*"],
      "~/*": ["./.contentlayer/*"]
    },
    "plugins": [
      {
        "name": "next"
      }
    ],
    "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
  },
  "include": [
    ".",
    "types/index.d.ts",
    "index.d.ts",
    "next-env.d.ts",
    "eslint.config.mjs",
    "global.d.ts",
    "next.config.mjs",
    "postcss.config.cjs",
    "tailwind.config.ts",
    "contentlayer.config.ts",
    "**/*.tsx",
    ".contentlayer/generated",
    "**/*.ts",
    ".next/types/**/*.ts",
    "../../reset.d.ts"
  ],
  "exclude": ["node_modules", "public/**/*.js"]
}

The next.json file that the tsconfig of the app using contentlayer is extending:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "extends": "./base.json",
  "compilerOptions": {
    "lib": ["ESNext", "DOM", "DOM.Iterable", "ScriptHost"],
    "jsx": "preserve",
    "module": "ESNext",
    "isolatedModules": true,
    "alwaysStrict": true
  }
}

and the base.json file that the next.json tsconfig is extending

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "ES2022",
    "lib": ["ES2022", "ScriptHost"],
    "allowJs": true,
    "declaration": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,
    "incremental": true,
    "disableSourceOfProjectReferenceRedirect": true,
    "strict": true,
    "strictNullChecks": true,
    "noUncheckedIndexedAccess": true,
    "checkJs": true,
    "module": "Preserve",
    "moduleResolution": "Bundler",
    "noEmit": true
  },
  "exclude": ["node_modules", "build", "dist", ".next/cache"]
}

assigning the .contentlayer dir its own path alias might resolve it

{
  "paths": {
    "@/*": ["./*"],
    "~/*": ["./.contentlayer/*"]
   }
}

also ensure that the contentlayer.config.ts file is being explicitly whitelisted in the include array of your tsconfig

contentlayer.config.ts

@timlrx
Copy link
Owner

timlrx commented Sep 10, 2024

@zhutmost yes, this issues still occurs as indicated by your example. Since contentlayer manages the build step with its own plugin now, it should be a bug that path alias are not treated as external. Instead they should be added to the filter like local files, treated as external and bundled. Let me test if that fixes the issue.

@timlrx
Copy link
Owner

timlrx commented Sep 12, 2024

@zhutmost released as v0.5.1, your example should work now.

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 a pull request may close this issue.

3 participants