DcBD / vite-hugo-plugin

Plugin that makes integrating Vite into Hugo application painless.
MIT License
2 stars 2 forks source link

"no such file or directory" - when appDir is a subdirectory #12

Open ionous opened 6 months ago

ionous commented 6 months ago

when appDir points to a subdirectory:

const appDir = path.resolve(__dirname, 'site');
const hugoOutDir = path.resolve(__dirname, 'public');
const hugoConfigFileName = 'config.toml';
export default defineConfig({
  plugins: [
    hugoPlugin({ appDir, hugoOutDir, hugoConfigFileName })
  ]
});

hugoPlugin will fail to load the config ( resulting in the stack below ) because the request to read the config doesn't include the path to the app dir.

https://github.com/DcBD/vite-hugo-plugin/blob/58ff891eafedb086ad4869745121a0efe0606e2a/src/utils/get-hugo-config.ts#L33

modifying the line as follows, seems to fix the issue.

// import path from 'path'; 
const configString = readFileSync(path.join(appDir, configFile), "utf-8");

an alternative fix might be to remove the configFile filtering at the top of getHugoConfig(), and then the configFile could be an absolute path. ( readFileSync will fail if the file doesnt exist anyway )

failed to load config from ... vite.config.js
error during build:
Error: ENOENT: no such file or directory, open 'config.toml'
    at Object.readFileUtf8 (node:internal/fs/sync:25:18)
    at readFileSync (node:fs:441:19)
    at getHugoConfig (/Users/ionous/Dev/shift/shift-docs/node_modules/vite-hugo-plugin/dist/index.js:71:51)
    at hugoPlugin (/Users/ionous/Dev/shift/shift-docs/node_modules/vite-hugo-plugin/dist/index.js:91:22)
    at file:///Users/ionous/Dev/shift/shift-docs/vite.config.js.timestamp-1710057699029-eb224d5f316f.mjs:11:5
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadConfigFromBundledFile (file:///Users/ionous/Dev/shift/shift-docs/node_modules/vite/dist/node/chunks/dep-G-px366b.js:68182:21)
    at async loadConfigFromFile (file:///Users/ionous/Dev/shift/shift-docs/node_modules/vite/dist/node/chunks/dep-G-px366b.js:68037:28)
    at async resolveConfig (file:///Users/ionous/Dev/shift/shift-docs/node_modules/vite/dist/node/chunks/dep-G-px366b.js:67649:28)
tkgregory commented 2 months ago

I have a similar problem when config is in a subdirectory.

e.g.

my-project/
└── config/
    ├── _default/
        └── config.toml
    └── production/
        └── config.toml

This is the recommended approach to split config by environment as per Hugo docs.