vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
12.88k stars 2.08k forks source link

Cant import createContentLoader on build #4267

Closed AzeTIIx closed 6 days ago

AzeTIIx commented 1 week ago

Describe the bug

Hi, I'm trying to use the createContentLoader function but I always get the same traceback:

PS G:\THR\The-Hacker-Recipes> npm run docs:build

> docs:build
> vitepress build docs

  vitepress v1.4.0

x Build failed in 13.54s
✖ building client + server bundles...
build error:
docs/.vitepress/theme/composables/authorsLoader.ts (2:9): "createContentLoader" is not exported by "node_modules/vitepress/dist/client/index.js", imported by "docs/.vitepress/theme/composables/authorsLoader.ts".  
file: G:/THR/The-Hacker-Recipes/docs/.vitepress/theme/composables/authorsLoader.ts:2:9

1: // authorsLoader.ts
2: import { createContentLoader } from 'vitepress'
            ^
3: 
4: export default createContentLoader({

docs/.vitepress/theme/composables/authorsLoader.ts (2:9): "createContentLoader" is not exported by "node_modules/vitepress/dist/client/index.js", imported by "docs/.vitepress/theme/composables/authorsLoader.ts".  
file: G:/THR/The-Hacker-Recipes/docs/.vitepress/theme/composables/authorsLoader.ts:2:9

1: // authorsLoader.ts
2: import { createContentLoader } from 'vitepress'
            ^
3:
4: export default createContentLoader({

    at getRollupError (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/parseAst.js:395:41)
    at error (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/parseAst.js:391:42)
    at Module.error (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/node-entry.js:15535:16)
    at Module.traceVariable (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/node-entry.js:15984:29)
    at ModuleScope.findVariable (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/node-entry.js:13770:39)
    at Identifier.bind (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/node-entry.js:5035:40)
    at CallExpression.bind (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/node-entry.js:2855:23)
    at CallExpression.bind (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/node-entry.js:11235:15)
    at ExportDefaultDeclaration.bind (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/node-entry.js:2855:23)
    at Program.bind (file:///G:/THR/The-Hacker-Recipes/node_modules/rollup/dist/es/shared/node-entry.js:2851:28)

Here is my file which uses the module, authorsLoader.ts :

// authorsLoader.ts
import { createContentLoader } from 'vitepress'

export default createContentLoader({
  pattern: '**/*.md',
  extract({ frontmatter }) {
    if (frontmatter.authors) {
      return {
        authors: frontmatter.authors.split(',').map((author: string) => author.trim()),
      }
    }
    return null
  }
})

And here is my index.js from which the modules I can import are taken:

// exports in this file are exposed to themes and md files via 'vitepress'
// so the user can do `import { useRoute, useData } from 'vitepress'`
// composables
export { useData, dataSymbol } from './app/data';
export { useRoute, useRouter } from './app/router';
// utilities
export { inBrowser, onContentUpdated, defineClientComponent, withBase, getScrollOffset, _escapeHtml } from './app/utils';
// components
export { Content } from './app/components/Content';

Reproduction

Here you can see the github action build failing :

https://github.com/The-Hacker-Recipes/The-Hacker-Recipes/actions/runs/11284736710/job/31386402614

Expected behavior

I expect my version of vitepress being able to use all the framework modules.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 AMD Ryzen 7 3800X 8-Core Processor
    Memory: 10.25 GB / 31.95 GB
  Binaries:
    Node: 20.11.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.5.0 - ~\AppData\Local\pnpm\pnpm.EXE
    bun: 1.1.20 - ~\AppData\Roaming\npm\bun.CMD
  Browsers:
    Edge: Chromium (128.0.2739.54)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    vitepress: ^1.4.0 => 1.4.0

Additional context

No response

Validations

brc-dd commented 6 days ago

createContentLoader is node-only API. You seem to be using it in client-side code. You should name your file authors.data.ts -- docs https://vitepress.dev/guide/data-loading