首页 > 其他分享 >vue2 webpack打包配置

vue2 webpack打包配置

时间:2024-09-13 14:50:06浏览次数:3  
标签:name config webpack build vue2 new true 打包

序言

最近在优化之前做的项目,看到打包后的文件夹,出现很多不需要的文件,想着应该是打包出现了问题,之前没时间优化,现在来看看优化项吧。

RemovedPluginError: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead
以上就是在使用命令行 npm run build:prod 之后出现的错误信息

打包配置

在项目中找到build文件夹下,找到webpack.prod.conf.js文件,可以注释掉或者删除,因为webpack版本是4.0以上的,CommonsChunkPlugin被移除掉了,所以导致打包出错。

在这里插入图片描述

解决方法

const webpackConfig = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({
      sourceMap: config.build.productionSourceMap,
      extract: true,
      usePostCSS: true
    })
  },
  devtool: config.build.productionSourceMap ? config.build.devtool : false,
  output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].[chunkhash].js'),
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js'),
    // publicPath:'./'
  },
  optimization: {
    runtimeChunk: {
      name: 'manifest'
    },
    splitChunks: {
      maxInitialRequests: 10,
      cacheGroups: {
        common: {name: 'common'}
      }
    }
  },
  plugins: [
    // http://vuejs.github.io/vue-loader/en/workflow/production.html
    new webpack.DefinePlugin({
      'process.env': env
    }),
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: {
          warnings: false
        }
      },
      sourceMap: config.build.productionSourceMap,
      parallel: true
    }),
    // extract css into its own file
    new ExtractTextPlugin({
      filename: utils.assetsPath('css/[name].[chunkhash].css'),
      // filename:`[name]_[md5:contenthash:hex:8].css`,
      // Setting the following option to `false` will not extract CSS from codesplit chunks.
      // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
      // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`, 
      // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
      allChunks: true,
    }),
    // Compress extracted CSS. We are using this plugin so that possible
    // duplicated CSS from different components can be deduped.
    new OptimizeCSSPlugin({
      cssProcessorOptions: config.build.productionSourceMap
        ? { safe: true, map: { inline: false },autoprefixer: false }
        : { safe: true }
    }),
    // generate dist index.html with correct asset hash for caching.
    // you can customize output by editing /index.html
    // see https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: process.env.NODE_ENV === 'testing'
        ? 'index.html'
        : config.build.index,
      template: 'index.html',
      favicon: 'static/favicon.ico',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
    // keep module.id stable when vendor modules does not change
    new webpack.HashedModuleIdsPlugin(),
    // enable scope hoisting
    new webpack.optimize.ModuleConcatenationPlugin(),
    // 将vendor js拆分为自己的文件。CommonsChunkPlugin用于提取多个入口文件共有的代码到单独的bundle中,以优化加载时间。
    // new webpack.optimize.CommonsChunkPlugin({
    //   name: 'vendor',
    //   minChunks (module) {
    //     // any required modules inside node_modules are extracted to vendor
    //     return ( module.resource && /\.js$/.test(module.resource) && module.resource.indexOf(path.join(__dirname, '../node_modules')) === 0 )
    //   }
    // }),
    // extract webpack runtime and module manifest to its own file in order to
    // prevent vendor hash from being updated whenever app bundle is updated
    // new webpack.optimize.CommonsChunkPlugin({
    //   name: 'manifest',
    //   minChunks: Infinity
    // }),
    // This instance extracts shared chunks from code splitted chunks and bundles them
    // in a separate chunk, similar to the vendor chunk
    // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
    // new webpack.optimize.CommonsChunkPlugin({
    //   name: 'app',
    //   async: 'vendor-async',
    //   children: true,
    //   minChunks: 3
    // }),

    // copy custom static assets
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static'),
        to: config.build.assetsSubDirectory,
        ignore: ['.*']
      }
    ])
  ]
})

哦可了,可以打包成功啦!

在这里插入图片描述

标签:name,config,webpack,build,vue2,new,true,打包
From: https://blog.csdn.net/m0_46159356/article/details/142210656

相关文章

  • 8、文件打包和压缩命令
    打包和压缩的区别打包定义:将多个文件合并成一个文件,以减少文件个数。作用:减少传输次数,也可以称为归档。术语:打包(pack)、解包(unpack)。压缩定义:缩小一个文件的体积,以减少占用空间。作用:减少文件体积。术语:压缩(compress)、解压缩(decompress)。压缩......
  • vue2动态生成(绘制)图形验证码(验证码图片)
     方案1:js绘制条形验证码 参考文档:https://blog.csdn.net/LOVE_mingjing/article/details/130622848 utils文件夹下新建GVerify.js文件:functionGVerify(options){//创建一个图形验证码对象,接收options对象为参数this.options={//默认options参数值......
  • Docker脚本一键打包java镜像运行备份多端口共存
    效果./docker_build.sh8081后会创建一个新的8081端口容器,并创建一个8081镜像,并备份之前的镜像可以启用多个端口 结构  DockerFile#FROM#基础镜像,当前新镜像是基于哪个镜像的#MAINTAINER#镜像维护者的姓名混合邮箱地址#RUN#容器构建时需......
  • Python打包完成后报错,如何解决?
    大家好,我是Python进阶者。一、前言前几天在Python最强王者交流群【钟爱一生】问了一个Python打包处理数据的问题,问题如下:打包完成后报错:发生错误:Missingoptionaldependency'openpyxl'.Usepiporcondatoinstallopenpyxl.哪位大佬帮我看一下错在哪了?二、实现过程后来......
  • # yyds干货盘点 # Python打包完成后报错,如何解决?
    大家好,我是Python进阶者。一、前言前几天在Python最强王者交流群【钟爱一生】问了一个Python打包处理数据的问题,问题如下:打包完成后报错:发生错误:Missingoptionaldependency'openpyxl'.Usepiporcondatoinstallopenpyxl.哪位大佬帮我看一下错在哪了?二、实现过程后来【隔壁......
  • 通过打包 Flash Attention 来提升 Hugging Face 训练效率
    简单概述现在,在HuggingFace中,使用打包的指令调整示例(无需填充)进行训练已与FlashAttention2兼容,这要归功于一个最近的PR以及新的DataCollatorWithFlattening。它可以在保持收敛质量的同时,将训练吞吐量提高多达2倍。继续阅读以了解详细信息!简介在训练期间,对小......
  • vite如何打包vue3插件为JSSDK
    安装vitenpmcreatevite@latest你还可以通过附加的命令行选项直接指定项目名称和你想要使用的模板。例如,要构建一个Vite+Vue+ts项目,运行:#npm7+,需要添加额外的--:npmcreatevite@latestmy-vue-app----templatevue-ts查看create-vite以获取每个模板的更多细......
  • Python中如何将图片资源打包进exe文件
    目录一、安装PyInstaller二、准备图片资源三、修改图片资源的引用方式1.使用Base64编码2.修改资源路径的引用1.打包命令2.打包后的文件3.运行exe文件五、案例与测试六、总结在Python开发中,经常需要将图片等资源文件与Python脚本一起打包成独立的可执行文件(ex......
  • 前端vue2 常用的函数
    1、在el-menu开启路由模式,default-active使用动态值等于当前路由,就需要用:default-active="$route.path" 2、阿里巴巴矢量图icfont的使用 ①将自己需要的图标下载到矢量库对应的项目文件中 ②更新对应的css代码,点击css代码链接,更新到本地去 ③使用<iclass="iconfont......
  • 在不同目录中的py文件,使用pyinstaller打包exe时,该如何设置才能打包正确
    在使用 pyinstaller 打包Python应用程序为单个可执行文件(.exe)时,如果你的项目包含位于不同目录中的Python文件(模块或包),你需要确保 pyinstaller 能够正确地找到并包含这些依赖文件。这通常通过以下几种方式实现:1.使用 -p 或 --paths 选项指定额外的搜索路径如果你的......