Open daalgi opened 1 year ago
Same hereee
Yes, there seems to be an error on the onSuccess
callback hook in Contentlayer when running on windows: https://github.com/contentlayerdev/contentlayer/issues/477
As a workaround, I suggest commenting out the onSuccess
hook and doing the transformation during the transformation manually:
Add the following script to ./scripts/postContentlayer.mjs
:
import { writeFileSync } from 'fs'
import GithubSlugger from 'github-slugger'
import { allCoreContent, sortPosts } from 'pliny/utils/contentlayer.js'
import { allBlogs } from '../.contentlayer/generated/index.mjs'
import siteMetadata from '../data/siteMetadata.js'
const isProduction = process.env.NODE_ENV === 'production'
/**
* Count the occurrences of all tags across blog posts and write to json file
*/
export async function createTagCount() {
const tagCount = {}
allBlogs.forEach((file) => {
if (file.tags && (!isProduction || file.draft !== true)) {
file.tags.forEach((tag) => {
const formattedTag = GithubSlugger.slug(tag)
if (formattedTag in tagCount) {
tagCount[formattedTag] += 1
} else {
tagCount[formattedTag] = 1
}
})
}
})
writeFileSync('./app/tag-data.json', JSON.stringify(tagCount))
}
export async function createSearchIndex() {
if (
siteMetadata?.search?.provider === 'kbar' &&
siteMetadata.search.kbarConfig.searchDocumentsPath
) {
writeFileSync(
`public/${siteMetadata.search.kbarConfig.searchDocumentsPath}`,
JSON.stringify(allCoreContent(sortPosts(allBlogs)))
)
console.log('Local search index generated...')
}
}
async function postContentlayer() {
await createTagCount()
await createSearchIndex()
}
postContentlayer()
You can then run node ./scripts/postContentlayer.mjs
before running yarn dev
again. It's not the perfect solution - ideally the issue is patched on contentlayer side or there' a next config plugin to manipulate the assets before next server is run, but this would at least enable you to run the template on windows.
I made a PR https://github.com/contentlayerdev/contentlayer/pull/571 to fix this.
before new version of contentlayer
or @contentlayer/core
released, you can manually compile or create a patch update:
make a chagne in @contentlayer/core/dist/generation/generate-dotpkg.js
, do a temp fix.
+import * as URL from "url"
const successCallback = (onSuccess) => {
if (!onSuccess)
return T.unit;
- return pipe(getCwd, T.map((cwd) => ArtifactsDir.getDirPath({ cwd })), T.tapSync((path) => console.log('successCallback', path)), T.chain((generatedPkgPath) => T.tryCatchPromise(() => onSuccess(() => import(filePathJoin(generatedPkgPath, 'generated', 'index.mjs'))), (error) => new SuccessCallbackError({ error }))), OT.withSpan('@contentlayer/core/generation:successCallback'));
+ return pipe(getCwd, T.map((cwd) => ArtifactsDir.getDirPath({ cwd })), T.tapSync((path) => console.log('successCallback', path)),
+ T.chain((generatedPkgPath) => T.tryCatchPromise(() => onSuccess(() => import(
+ URL.pathToFileURL(
+ filePathJoin(generatedPkgPath, 'generated', 'index.mjs')))
+ )
+ ,
+ (error) => new SuccessCallbackError({ error }))), OT.withSpan('@contentlayer/core/generation:successCallback'));
};
Yes, there seems to be an error on the
onSuccess
callback hook in Contentlayer when running on windows: contentlayerdev/contentlayer#477As a workaround, I suggest commenting out the
onSuccess
hook and doing the transformation during the transformation manually:Add the following script to
./scripts/postContentlayer.mjs
:import { writeFileSync } from 'fs' import GithubSlugger from 'github-slugger' import { allCoreContent, sortPosts } from 'pliny/utils/contentlayer.js' import { allBlogs } from '../.contentlayer/generated/index.mjs' import siteMetadata from '../data/siteMetadata.js' const isProduction = process.env.NODE_ENV === 'production' /** * Count the occurrences of all tags across blog posts and write to json file */ export async function createTagCount() { const tagCount = {} allBlogs.forEach((file) => { if (file.tags && (!isProduction || file.draft !== true)) { file.tags.forEach((tag) => { const formattedTag = GithubSlugger.slug(tag) if (formattedTag in tagCount) { tagCount[formattedTag] += 1 } else { tagCount[formattedTag] = 1 } }) } }) writeFileSync('./app/tag-data.json', JSON.stringify(tagCount)) } export async function createSearchIndex() { if ( siteMetadata?.search?.provider === 'kbar' && siteMetadata.search.kbarConfig.searchDocumentsPath ) { writeFileSync( `public/${siteMetadata.search.kbarConfig.searchDocumentsPath}`, JSON.stringify(allCoreContent(sortPosts(allBlogs))) ) console.log('Local search index generated...') } } async function postContentlayer() { await createTagCount() await createSearchIndex() } postContentlayer()
You can then run
node ./scripts/postContentlayer.mjs
before runningyarn dev
again. It's not the perfect solution - ideally the issue is patched on contentlayer side or there' a next config plugin to manipulate the assets before next server is run, but this would at least enable you to run the template on windows.
Nice workaround, thanks
I made a PR contentlayerdev/contentlayer#571 to fix this.
before new version of
contentlayer
或者@contentlayer/core
released, you can manually compile or create a patch update:make a chagne in
@contentlayer/core/dist/generation/generate-dotpkg.js
, do a temp fix.+import * as URL from "url" const successCallback = (onSuccess) => { if (!onSuccess) return T.unit; - return pipe(getCwd, T.map((cwd) => ArtifactsDir.getDirPath({ cwd })), T.tapSync((path) => console.log('successCallback', path)), T.chain((generatedPkgPath) => T.tryCatchPromise(() => onSuccess(() => import(filePathJoin(generatedPkgPath, 'generated', 'index.mjs'))), (error) => new SuccessCallbackError({ error }))), OT.withSpan('@contentlayer/core/generation:successCallback')); + return pipe(getCwd, T.map((cwd) => ArtifactsDir.getDirPath({ cwd })), T.tapSync((path) => console.log('successCallback', path)), + T.chain((generatedPkgPath) => T.tryCatchPromise(() => onSuccess(() => import( + URL.pathToFileURL( + filePathJoin(generatedPkgPath, 'generated', 'index.mjs'))) + ) + , + (error) => new SuccessCallbackError({ error }))), OT.withSpan('@contentlayer/core/generation:successCallback')); };
This solved my problem,Thanks
Describe the bug The site runs ok locally
npm run dev
, but raise errors whennpm run build
.To Reproduce Steps to reproduce the behavior:
Expected behavior Build without errors.
System Info (if dev / build issue):