英文:
Vite not detecting changes (vue/pug)
问题
Vite在被其他PUG文件引用时无法检测到更改。例如:
div
include ./pug/index-2.pug
index-2.pug
位于index.pug
旁边,但Vite只会检测父文件的更改。这可能是因为.vue
文件中没有直接引用index-2.pug
。但这是一个重大障碍!有没有办法解决这个问题?
更新:
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [
vue(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
port: 8080
}
})
和package.json
:
{
"name": "booqall_ui",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "run-p type-check build-only",
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --noEmit -p tsconfig.app.json --composite false",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
"format": "prettier --write src/"
},
"dependencies": {
"axios": "^1.4.0",
"buffer": "^6.0.3",
"pinia": "^2.1.3",
"pug": "^3.0.2",
"socket.io-client": "^4.7.0",
"underscore": "^1.13.6",
"vite-plugin-pug": "^0.3.2",
"vue": "^3.3.4",
"vue-router": "^4.2.2",
"vue-toast-notification": "^3.1.1"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.2.0",
"@tsconfig/node18": "^2.0.1",
"@types/node": "^18.16.18",
"@types/underscore": "^1.11.5",
"@vitejs/plugin-vue": "^4.2.3",
"@vue/eslint-config-prettier": "^7.1.0",
"@vue/eslint-config-typescript": "^11.0.3",
"@vue/tsconfig": "^0.4.0",
"eslint": "^8.39.0",
"eslint-plugin-vue": "^9.11.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"sass": "^1.63.6",
"typescript": "~5.0.4",
"vite": "^4.3.9",
"vue-tsc": "^1.6.5"
}
}
英文:
Vite fails to detected changes in PUG files that are referenced by other PUG files. For example:
div
include ./pug/index-2.pug
The index-2.pug
is placed next to index.pug
, but Vite only detects changes for the parent file. Maybe that is because index-2.pug
is not referenced in the .vue
file directly. But that's a huge deal breaker! Is there any way to fix this?
Update:
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
port: 8080
}
})
and package.json
{
"name": "booqall_ui",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "run-p type-check build-only",
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --noEmit -p tsconfig.app.json --composite false",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
"format": "prettier --write src/"
},
"dependencies": {
"axios": "^1.4.0",
"buffer": "^6.0.3",
"pinia": "^2.1.3",
"pug": "^3.0.2",
"socket.io-client": "^4.7.0",
"underscore": "^1.13.6",
"vite-plugin-pug": "^0.3.2",
"vue": "^3.3.4",
"vue-router": "^4.2.2",
"vue-toast-notification": "^3.1.1"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.2.0",
"@tsconfig/node18": "^2.0.1",
"@types/node": "^18.16.18",
"@types/underscore": "^1.11.5",
"@vitejs/plugin-vue": "^4.2.3",
"@vue/eslint-config-prettier": "^7.1.0",
"@vue/eslint-config-typescript": "^11.0.3",
"@vue/tsconfig": "^0.4.0",
"eslint": "^8.39.0",
"eslint-plugin-vue": "^9.11.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"sass": "^1.63.6",
"typescript": "~5.0.4",
"vite": "^4.3.9",
"vue-tsc": "^1.6.5"
}
}
答案1
得分: -1
Vite+Vue不支持使用PUG的包含功能进行开发模式的重新加载。但这并不是问题。需要有一些心态转变,将Vue组件视为应用程序的构建块,而不是PUG文件。因此,您只需将您的PUG代码包装在Vue SFC中,并在其他PUG代码中使用这些组件。这样做的巨大好处是您可以进一步扩展这些基于PUG的组件,添加带有作用域的CSS模块样式和JS代码。
<script setup>
import MyPug from './MyPug.vue'; // 通常带有PUG模板的SFC
</script>
<template lang="pug">
div
my-pug
</template>
有人抱怨Webpack可以平滑地处理pug包含。但这里有一个巨大的区别。Webpack是一个打包工具,而Vite不是。Vite使用Rollup.js作为打包工具。因此,您可以轻松地重新创建您的Webpack工作流程:
- 使用
npx vite build --watch --minify=true
(您可能希望在rollup选项中关闭treeshaking)。 - 使用Vite的选项
build.watch.include
来包含您的PUG文件以进行监视和在更改时重新构建。 - 使用
npx vite preview
启动预览服务器。 - (不在Vite的范围内)使用一些工具在构建文件重新构建时进行实时重新加载。我不介意手动重新加载。
英文:
The Vite+Vue doesn't support development mode with reloading PUG's includes. But that's not a problem. There should be some mindset shift to see Vue components as building blocks of an application, not PUG files. So you just wrap your PUG code into Vue SFCs and use these components in other PUG code. The huge benefit of that is that you can futher extend those PUG based components with scoped/css module styles and JS code.
<script setup>
import MyPug from './MyPug.vue'; // usuall SFC with pug template
</script>
<template lang="pug">
div
my-pug
</template>
The OP complained that Webpack offers pug includes smoothly. But there's a huge difference. Webpack is a bundler, Vite isn't. Vite uses Rollup.js as a bundler. So you can easily recreate your Webpack workflow:
- use
npx vite build --watch --minify=true
(you should probably want to turn off treeshaking in the rollup options). - use
build.watch.include
Vite's options to include your PUG files for watching and rebuild on changes - use
npx vite preview
to start a preview server - (out of scope of Vite): use some tool to live reload when the build files are re-build. I don't mind to reload manually
答案2
得分: -2
经过调查,发现这个问题实际上是Vite中的设计缺陷。该框架在设计时严重依赖于一个假设,即所有标记语言都与Html类似,没有任何高级功能。因此,它无法理解PUG文件之间的任何依赖关系。实际上,当对pug依赖进行更改时,它会被检测到!但是这个框架太愚蠢,无法理解它是另一个pug的依赖,并且应重新加载另一个pug。在遇到这个问题后,我绝对不推荐使用Vite,因为我们不得不迁回Webpack。
英文:
After investigating this issue it turned out to be a Design Flaw in Vite. The framework has heavily designed based on the assumption that all markup languages work similar to Html without any advanced functionalities. For that reasons, it cannot understand any dependency between PUG files. In fact when a change is made to a pug dependency, it is detected! but the framework is too dump to understand that it is a dependency of another pug and that other pug should be reloaded. I absolutely discourage Vite after running to this issue as we had to migrate back to Webpack.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论