简体   繁体   English

webpackJsonp未定义:webpack-dev-server和CommonsChunkPlugin

[英]webpackJsonp is not defined: webpack-dev-server and CommonsChunkPlugin

This is my webpack.config.js file: 这是我的webpack.config.js文件:

const webpack = require('webpack');
const path = require('path');

module.exports = {
  cache: true,
  devtool: 'source-map',
  entry: {
    app : [
      './src/index.js'
    ],
    vendor: ['lodash']
  },
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
    publicPath: '/dist/',
    pathinfo: true
  },
  module: {
    loaders: [
      { test: /\.js$/, exclude: /node_modules/, loaders: ['babel'] },
      { test: /\.scss/, exclude: /node_modules/, loaders: ['style', 'css', 'sass'] }
    ]
  },
  plugins: [
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js', Infinity)
  ]
};

And this is my scripts that runs the webpack-dev-server: 这是我运行webpack-dev-server的脚本:

const webpack =require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const webpackConfig = require('../webpack.config');
const _  = require('lodash');

const webpackDevConfig = _.cloneDeep(webpackConfig);
const devPort = 3000;

webpackDevConfig.entry.app.unshift('webpack/hot/dev-server');
webpackDevConfig.entry.app.unshift('webpack-dev-server/client?http://localhost:' + devPort + '/');
webpackDevConfig.plugins.push(new webpack.HotModuleReplacementPlugin());

new WebpackDevServer(webpack(webpackDevConfig), {
  publicPath: webpackConfig.output.publicPath,
  hot: true,
  stats: {
    colors: true,
    chunks: false
  }
}).listen(devPort, 'localhost');

The webpack command output is good (bundle.js and vendor.bundle.js), however, the dev server fails with webpackJsonp is not defined (although its in-memory compilation succeeded). webpack命令输出正常(bundle.js和vendor.bundle.js),但是,dev服务器失败, webpackJsonp is not defined (尽管其内存中编译成功)。

When removing CommonsChunkPlugin from webpack.config.js - it all works fine: 从webpack.config.js中删除CommonsChunkPlugin时 - 一切正常:

...
entry: [
    './src/index.js'
  ],
...
plugins: [
    new webpack.NoErrorsPlugin()
  ]

Any ideas? 有任何想法吗?

In your index.html file just call vendor.bundle.js before bundle.js index.html文件中,只需在bundle.js之前调用vendor.bundle.js

<script src="assets/js/vendor.bundle.js"></script>
<script src="assets/js/bundle.js"></script>

That's all, now it should work. 这就是全部,现在它应该工作。 More information. 更多信息。

Rename vendor entry point to 将供应商入口点重命名为

'vendor.js': ['lodash'] 'vendor.js':['lodash']

Just to expand a little on the concept, the vendor has to come first since the runtime is contained in there (everything that defines all the variables and methods run during client load time because of all the webpacking). 只是为了扩展这个概念,供应商必须先来,因为运行时包含在那里(所有的定义所有变量和方法在客户端加载时运行,因为所有的webpacking)。

If you use a manifest file (because of chunking and so on), you'll have to put that first since it will then contain the runtime because of the way the module is built. 如果您使用清单文件(因为分块等),您必须首先放置它,因为它将包含运行时,因为模块的构建方式。

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

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