Vuejs 3 webpack:vue-template-compiler 的问题

新手上路,请多包涵

我正在尝试将 vuejs 3 集成到使用 webpack 的现有项目中。我阅读了有关 vue-loader 的信息,因此我正在尝试使用它。

在官方文档中我有这个:

每次发布新版本的vue,都会同时发布对应版本的vue-template-compiler。编译器的版本必须与基本的 vue 包同步,以便 vue-loader 生成与运行时兼容的代码。这意味着每次在项目中升级 vue 时,都应该升级 vue-template-compiler 以匹配它。

所以,当我尝试编译时,我得到了这个错误:

 Vue packages version mismatch:

- vue@3.0.2 (/home/alejo/playground/parquesFrontend/node_modules/vue/index.js)
- vue-template-compiler@2.6.12 (/home/alejo/playground/parquesFrontend/node_modules/vue-template-compiler/package.json)

This may cause things to work incorrectly. Make sure to use the same version for both.
If you are using vue-loader@>=10.0, simply update vue-template-compiler.
If you are using vue-loader@<10.0 or vueify, re-installing vue-loader/vueify should bump vue-template-compiler to the latest.

但是当我尝试安装 vue-template-compiler@3.0.2 我得到这个错误:

 ❯ npm install vue-template-compiler@3.0.2
npm ERR! code ETARGET
npm ERR! notarget No matching version found for vue-template-compiler@3.0.2.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/alejo/.npm/_logs/2020-11-17T02_52_46_458Z-debug.log

我怎么解决这个问题?

原文由 Alejo Dev 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 4.2k
2 个回答

要使 vue 3 在不使用 vite 或 vue cli 的情况下与 webpack 一起正常工作,请执行以下步骤:

  1. 初始化 package.json 像:
 {
    "private": true,
    "name": "vue-3",
    "description": null,

}

  1. 安装最新版本的 vue :

npm i --save vue@next vue-loader@next

  1. 还安装包含 @vue/compiler-sfc 的开发依赖项,它取代了 vue-template-compiler
 npm i -D @vue/compiler-sfc css-loader file-loader mini-css-extract-plugin
 url-loader webpack webpack-cli webpack-dev-server

  • @vue/编译器-sfc
  • CSS加载器
  • 文件加载器
  • 迷你 CSS 提取插件
  • 网址加载器
  • 加载器
  • 网页包
  • webpack-cli
  • webpack-开发服务器
  1. 使用以下内容创建或编辑您的 webpack.config.js:
 const path = require("path");
const { VueLoaderPlugin } = require("vue-loader");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = (env = {}) => ({
  mode: env.prod ? "production" : "development",
  devtool: env.prod ? "source-map" : "cheap-module-eval-source-map",
  entry: [
    require.resolve(`webpack-dev-server/client`),
    path.resolve(__dirname, "./src/main.js")
  ].filter(Boolean),
  output: {
    path: path.resolve(__dirname, "./dist"),
    publicPath: "/dist/"
  },
  resolve: {
    alias: {
      // this isn't technically needed, since the default `vue` entry for bundlers
      // is a simple `export * from '@vue/runtime-dom`. However having this
      // extra re-export somehow causes webpack to always invalidate the module
      // on the first HMR update and causes the page to reload.
      vue: "@vue/runtime-dom"
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: "vue-loader"
      },
      {
        test: /\.png$/,
        use: {
          loader: "url-loader",
          options: { limit: 8192 }
        }
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: { hmr: !env.prod }
          },
          "css-loader"
        ]
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new MiniCssExtractPlugin({
      filename: "[name].css"
    })
  ],
  devServer: {
    inline: true,
    hot: true,
    stats: "minimal",
    contentBase: __dirname,
    overlay: true,
    injectClient: false,
    disableHostCheck: true
  }
});

  1. 添加 dev 脚本来运行您的应用程序:
 {
    "private": true,
    "scripts": {
        "dev": "webpack-dev-server"
    },
    "dependencies": {
        "vue": "^3.0.2"
    },
    "name": "vue-3",
    "description": null,
    "devDependencies": {
      ...
    }
}

  1. 用以下内容填写 index.html
 <link rel="stylesheet" href="/dist/main.css" />
<div id="app"></div>
<script src="/dist/main.js"></script>

最后 运行 npm run dev 访问http://localhost:8080/

对于准备使用的项目,请尝试克隆按照上述 步骤 构建的存储库。

编辑 webpack-vue3

原文由 Boussadjra Brahim 发布,翻译遵循 CC BY-SA 4.0 许可协议

我相信您需要在 Vue 3 中使用 vue-loader@next

在 Vue 3 中,SFC 编译器包不再是 vue-template-compiler 而是 compiler-sfc在这里查看

我完全同意使用 Vue CLI 来管理项目的建议——它可以让你在管理所有依赖项时省去很多麻烦(尤其是现在 Vue 3 生态系统正试图赶上 Vue 3 的发布,甚至还有很多工具都不要)没有任何迁移文档 ….像 vue-loader)

如果您因为现有项目已经有 Webpack 配置而无法使用 CLI,您仍然可以使用 CLI 作为指南。只需在旁边生成新项目,使用 vue inspect 命令检查它正在使用的 Webpack 配置,并使用 package.json 获取所需的依赖项…

原文由 Michal Levý 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏