javascript Vite插件手柄不工作与多页设置

lmyy7pcs  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(131)

Vite-plugin-handlebars对嵌套在/public中的任何页面都不起作用。例如,about.html嵌套在/public中是因为Vite在运行构建时将其捆绑到根目录中。然而,这给我的handlebars带来了一个问题。它们在index.html中起作用,但对/public中的任何页面都不起作用。

我的root设置

├── package.json
├── vite.config.js
├── index.html
└── public
    ├── about.html
    └── partials
        ├── _head.hbs
        └── _nav.hbs

vite.config.js

import { resolve } from 'path';
import handlebars from 'vite-plugin-handlebars';
import { defineConfig } from 'vite';

const pageData = {
    '/index.html': {
      title: 'Home',
    },
    '/about.html': {
      title: 'About',
    },
  };

export default defineConfig({
    base: '/',
    assetsInclude: ['partials/_nav.hbs', 'partials/_head.hbs', 'partials/_footer.hbs'],
    server: {
        host: true
    },
    plugins: [
        handlebars({
            partialDirectory: resolve(__dirname, './public/partials'),
            context(pagePath) {
                return pageData[pagePath];
              },
        }),
    ],
});

package.json

{
  "name": "v",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "devDependencies": {
    "postcss-scss": "^4.0.6",
    "sass": "^1.57.0",
    "vite": "^4.0.0",
    "vite-plugin-handlebars": "^1.6.0"
  },
  "dependencies": {
    "@rollup/plugin-inject": "^5.0.2",
    "feather-icons": "^4.29.0",
    "smooth-scroll": "^16.1.3"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">

{{> _head }}

<body>

  <div id="app" class="desktop">

    {{> _nav }}

    <div id="content" class="content">
ufj5ltwl

ufj5ltwl1#

这对我很有效
partialDirectory:resolve(__dirname,'src','hbs','partials'),

相关问题