简体   繁体   English

无法在 vue-cli 项目中使用自定义加载器加载模块

[英]Can't load a module with custom loader in vue-cli project

I have a project where Webpack 4.43.0 is set up with vue-cli.我有一个项目,其中使用 vue-cli 设置了 Webpack 4.43.0。 I'm trying to use image-size-loader to get image size at build time.我正在尝试使用image-size-loader在构建时获取图像大小。

For that, in one of my .vue files I'm trying to load the module using the custom loader I have installed in the project:为此,在我的一个 .vue 文件中,我尝试使用我在项目中安装的自定义加载器加载模块:

const background = require("image-size!../../../../assets/images/candy.jpg");

When my project builds, it outputs the following error:当我的项目构建时,它输出以下错误:

 ERROR  Failed to compile with 1 errors8:47:03 AM

This dependency was not found:

* image-size!../../../../assets/images/candy.jpg in ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options!./src/vue/guides/tags/hero/TagGroupInvite.vue?vue&type=script&lang=js&

To install it, you can run: npm install --save image-size!../../../../assets/images/candy.jpg

The file is present and js/ts/css files resolve fine.该文件存在并且 js/ts/css 文件解析正常。 What can be wrong with my setup?我的设置有什么问题?

I think you have to specify image-size as a loader too.我认为您也必须将image-size指定为加载程序。

Append this loader to webpack.base.conf.js将此加载器附加到 webpack.base.conf.js

...
loaders: [
    ...
    {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'image-size'
    }
    ...
]
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM