webpack设置chunks后提示 webpackJsonp is not defined,如何解决?

1.由于是两个页面就设置了两个入口,那么现在就有一个问题,就是打包的时候两个页面引入的所有js文件都会打包进去。
2.为了解决这个问题,看了下网上的说明可以使用chunks指定引入的打包文件,结果指定以后报错了。


webpack.config.js

entry:{
      index: './src/index.ts',
      home: './src/home.ts'
  },

webpack.prod.conf.js

 plugins: [
    new HtmlWebpackPlugin({         
      filename: config.build.index,
      template: 'index.html',
      inject: true,
      //chunks这个参数告诉插件要引用entry里面的哪几个入口
      chunks:['index'],      //这里设置以后报错,提示 webpackJsonp is not 
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true

    
      },
     
      chunksSortMode: 'dependency'
        }),
      new HtmlWebpackPlugin({          //另外一个入口文件
      filename: config.build.home,
      template: 'home.html',
      inject: true,
      //chunks这个参数告诉插件要引用entry里面的哪几个入口
      chunks:["home"],         //这里设置以后报错,提示 webpackJsonp is not defined                      
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
       
      },
    CommonsChunkPlugin
      chunksSortMode: 'dependency'
       
    }),

不知道是缺少其他配置还是什么原因,谢谢大家的解答,感激不尽

阅读 11.3k
5 个回答

你的commonChunk也要放进chunks里面的

问题解决了吗,最近在学这个东西,刚好碰到这个问题。原因是因为js文件加载顺序问题导致的,把你HtmlWebpackPlugin项里面的这个chunksSortMode改成chunksSortMode: 'auto' 试一下

chunksSortMode默认有四个选项,'none', 'auto', 'dependency', '{function}'。

根据具体情况选择使用

新手上路,请多包涵

因为生成时缺少一个manifest.js文件,webpackJsonp是在这个文件中声明的,需要把chunks:['index']改为chunks:['manifest', 'index']

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题