向Webpack添加插件会破坏它

时间:2019-06-11 14:18:27

标签: webpack webpack-2 webpack-4 webpack-style-loader

我使用的是webpack 3.5.6,并且正在尝试使用以下插件:

https://www.npmjs.com/package/copy-webpack-plugin

这是我的webpack代码:

var path = require("path");
var DIST_DIR = path.resolve(__dirname, "public");
var SRC_DIR = path.resolve(__dirname, "src");

const CopyPlugin = require('copy-webpack-plugin');
var config = {
//entry: [ SRC_DIR + "/app/index.js"],
plugins:[new CopyPlugin([
    { from: '/deployment-config/robots-stg.txt', to: '/' }
])],
entry: [ SRC_DIR + "/app/client.js"],
output: {
    path: DIST_DIR ,
    filename: "bundle.js"
},
module: {
    loaders: [
        {
            test: /\.js?/,
            include: SRC_DIR,
            loader: "babel-loader",
            query: {
                "plugins": ["transform-decorators-legacy", "transform-class-properties","transform-decorators"],
                presets: [
                    "react",
                    "es2015",
                    "stage-2",
                    ['env', {targets: {browsers: ['last 3 versions']}}]]
            }
        }, {
            test: /\.s?css/,
            include: SRC_DIR,
            loaders: ["style-loader","css-loader","sass-loader"]
        },
        { test: /\.(svg|woff|ttf|wav|mp3)$/, loader: "file-loader" },
        {
            test: /\.(png|jpe?g|gif)$/,
            loader: 'url-loader?limit=25000&name=img/[name].[ext]'
        }
      ]
   }
};

  module.exports = config;

运行此命令时,出现以下错误:

\node_modules\webpack\bin\webpack.js:348
                    throw err;
                    ^

  TypeError: Cannot read property 'emit' of undefined

我删除插件部分后,一切正常。我是否不正确地添加了插件?

2 个答案:

答案 0 :(得分:0)

复制到目录时,需要在toType中指定CopyWebpackPlugin选项。默认情况下,插件假定to是文件路径。试试这个:

plugins:[new CopyPlugin([
  { from: '/deployment-config/robots-stg.txt', to: '/', toType: "dir" }
])],

详细了解该选项here

答案 1 :(得分:0)

就我而言,它有助于将Webpack降级到4.0.0版。

Car.paint()