我的所有代码在 Webpack 编译时运行两次 [英] All my code runs twice when compiled by Webpack

查看:80
本文介绍了我的所有代码在 Webpack 编译时运行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 webpack-dev-server 用 webpack 构建我的 js 包时,我的代码每次运行两次.不知道如何解决.

When I build my js bundle with webpack using webpack-dev-server my code runs twice every time. Not sure how to fix it.

开发者工具控制台截图

我的 webpack 配置:

My webpack config:

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  devtool: 'cheap-eval-sourcemap',
  entry: [
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/dev-server',
    path.join(__dirname, '../src/main')
  ],
  output: {
    path: path.join(__dirname, '../dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, '../src/index.html')
    }),
    new CopyWebpackPlugin([
      {
        from: path.join(__dirname, '../assets'),
        to: path.join(__dirname, '../dist/assets')
      }
    ])
  ],
  devServer: {
    contentBase: path.join(__dirname, '../dist'),
    outputPath: '/lol',
    hot: true
  },
  module: {
    loaders: [
      {
        test: /.js$/,
        loaders: ['babel-loader'],
        include: path.join(__dirname, '../src')
      }
    ]
  }
};

推荐答案

在模板文件中,您可能已经手动添加了加载包.

in the template file you might have manually added a loading the bundle.

如果你没有

inject: false 

选项

new HtmlWebpackPlugin({
    template: path.join(__dirname, '../src/index.html')
}),

捆绑包将再次添加.

这篇关于我的所有代码在 Webpack 编译时运行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆