简体   繁体   English

将插件添加到webpack会破坏它

[英]Adding plugins to webpack breaks it

I use webpack 3.5.6 and I am trying to use the following plugin: 我使用webpack 3.5.6,我试图使用以下插件:

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

So here is my webpack code: 所以这是我的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;

When I run this I get the following error: 当我运行这个时,我收到以下错误:

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

  TypeError: Cannot read property 'emit' of undefined

as soon as I remove the plugins section everything works fine. 一旦我删除插件部分一切正常。 Am I adding the plugin improperly ? 我不正确地添加插件吗?

As you are copying to a directory, you need to specify the toType option in CopyWebpackPlugin . 在复制到目录时,需要在CopyWebpackPlugin指定toType选项。 By default the plugin assumes to is a file path. 默认情况下,插件假定to文件路径。 Try this: 尝试这个:

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

Read more about that option here . 在此处阅读有关该选项的更多信

In my case it helped to downgrade the webpack to version 4.0.0. 在我的例子中,它有助于将webpack降级到4.0.0版。

npm uninstall webpack --save
npm install webpack@4.0.0 --save
npm install webpack-cli -D

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

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