((pug template)TypeError:require不是一个函数

问题描述 投票:0回答:1

这是我的代码:

img(src=require('/images/menu/img.svg'))

我需要使用webpack将图像保存在dist文件夹中。

{
    test: /\.pug$/,
    loaders: ['html-loader?attrs=img:src',
      'pug-html-loader?{"pretty":true,"exports":false}'
    ]
  },
  {
    test: /\.(png|jpe?g|svg)$/i,
    use: [{
      loader: "file-loader",
      options:{
        name:'[path][hash].[name].[ext]'
      }
    }]        
  }

但是我总是会收到此错误。请帮忙。我需要安装装载程序或任何附属物吗?

TypeError: require is not a function
webpack pug
1个回答
0
投票

我建议您使用插件copy-webpack-plugin

安装:

npm install copy-webpack-plugin --save-dev

并将其添加到您的webpack配置中:

const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  // ...
  plugins: [
    new CopyPlugin([
      {
        from: path.resolve(__dirname, '/PATH-TO/images/menu/img.svg'),
        to: path.resolve(__dirname, '/dist/images/menu/img.svg')
      },
    ]),
  ],
  // ...
};
© www.soinside.com 2019 - 2024. All rights reserved.