首页 > 其他分享 >vue报错 Multiple assets emit different content to the same filename index.html

vue报错 Multiple assets emit different content to the same filename index.html

时间:2023-08-07 12:44:33浏览次数:36  
标签:index different resolve 插件 html 报错 HtmlWebpackPlugin config

vue-cli版本:@vue/[email protected]

报错现象:想把css和script全部内嵌到html文件中,就用了"HtmlInlineScriptPlugin"插件,打包后js代码被嵌到了head里,导致代码提前执行找不到#app,再配置HtmlWebpackPlugin插件通过inject: "body"指定代码内嵌到body,打包报错"Multiple assets emit different content to the same filename index.html"

报错原因:通过查询源码发现"node_modules\@vue\cli-service\lib\config\app.js"文件中引用了一个HtmlWebpackPlugin插件

// resolve HTML file(s)
    const HTMLPlugin = require('html-webpack-plugin')
    // const PreloadPlugin = require('@vue/preload-webpack-plugin')
    const multiPageConfig = options.pages
    const htmlPath = api.resolve('public/index.html')
    const defaultHtmlPath = path.resolve(__dirname, 'index-default.html')
    const publicCopyIgnore = ['**/.DS_Store']

    if (!multiPageConfig) {
      // default, single page setup.
      htmlOptions.template = fs.existsSync(htmlPath)
        ? htmlPath
        : defaultHtmlPath

      publicCopyIgnore.push(api.resolve(htmlOptions.template).replace(/\\/g, '/'))

      webpackConfig
        .plugin('html')
          .use(HTMLPlugin, [htmlOptions])

 在configureWebpack配置HtmlWebpackPlugin插件导致冲突,自定义的HtmlWebpackPlugin插件和脚手架的插件同时向默认的index.html中写入内容导致报错

config.plugins.push(
    new HtmlWebpackPlugin({
        inject: "body",
    })) 

解决方案:

      方案1: HtmlWebpackPlugin配置输出文件名为app.html(打包后dist文件夹中会同时有app.html和index.html,app.html为可用文件)

 new HtmlWebpackPlugin({
	filename: "app.html",
	templateParameters: {
		BASE_URL: `/`
	},
	template: resolve("./public/index.html"),
	inlineSource: ".(js|css)$",
	inject: "body",
}),   

      方案2:在chainWebpack中配置HtmlWebpackPlugin覆盖脚手架配置

chainWebpack: (config) => {
	config
		.plugin('html')
		.use(new HtmlWebpackPlugin({
			filename: "index.html",
			templateParameters: {
				BASE_URL: `/`
			},
			template: resolve("./public/index.html"),
			inlineSource: ".(js|css)$",
			inject: "body",
		}))
},   

 

完整配置:

      修改前:

//部分
module.exports = {
    configureWebpack: config => {
        if (process.env.NODE_ENV === 'production') {
            config.plugins.push(
                new HtmlWebpackPlugin({
                filename: "index.html",
                templateParameters: {
                    BASE_URL: `/`
                },
                template: resolve("./public/index.html"),
                inlineSource: ".(js|css)$",
                inject: "body",
            })
            )
        }
    },
    chainWebpack: (config) => {
       
    },
}

  修改后:

module.exports = {
    configureWebpack: config => {
        if (process.env.NODE_ENV === 'production') {
        
        }
    },
    chainWebpack: (config) => {
        config
            .plugin('html')
            .use(new HtmlWebpackPlugin({
                filename: "index.html",
                templateParameters: {
                    BASE_URL: `/`
                },
                template: resolve("./public/index.html"),
                inlineSource: ".(js|css)$",
                inject: "body",
            }))
    },
}

后记:

修改后使用的HtmlWebpackPlugin插件时new出来的对象,脚手架自用的是静态类,暂时不知道为什么这样用,改后有没有什么影响,HtmlWebpackPlugin有几个静态方法特别是getHooks估计会有影响,遇到了再研究吧

标签:index,different,resolve,插件,html,报错,HtmlWebpackPlugin,config
From: https://www.cnblogs.com/jusha/p/17611149.html

相关文章

  • 解决报错:Redis ERR unknown command ‘FLUSHDB‘
    RedisERRunknowncommand‘FLUSHDB’报错信息:ERRunknowncommand`flushdb`ERRunknowncommand`flushall`解决方案:我的redis版本是5.0.7修改配置文件打开/etc/redis/redis.conf文件,将下面两行代码注释掉rename-commandFLUSHALL37_dba_FLUSHALLrename-commandFLUSHDB......
  • 【错误记录】PySpark 运行报错 ( Did not find winutils.exe | HADOOP_HOME and hadoo
    文章目录一、报错信息二、解决方案(安装Hadoop运行环境)一、报错信息核心报错信息:WARNShell:Didnotfindwinutils.exe:java.io.FileNotFoundException:java.io.FileNotFoundException:HADOOP_HOMEandhadoop.home.dirareunset.在PyCharm中,调用PySpark执......
  • 【HMS Core】推送报错907135701、分析数据查看
    ​【关键字】HMS、推送服务、分析服务 【问题描述1】集成推送服务,获取Token时报错907135701:scopelistempty 【解决方案】907135701OpenGW没有配置Scope1、您可以检查下网络是否有问题,手机是否可以正常连接互联网2、查看推送服务开关是否正常打开​​ 【问题......
  • bazel test 编译失败:googletest、gtest 报错
    问题描述bazeltest遇到很多奇怪的编译错误,报错位置位于“googletest”目录,而且没有修改过googletest源码:ERROR:/bazel_cache/output_user_root/.../external/google/BUILD.bazel:80:11:Compilinggoogletest/src/gtest-matchers.ccfailed:(Exit1):gccfailed:errore......
  • bazel 使用 gtest/gmock 报错 Constraints from @bazel_tools//platforms have been r
    问题描述运行bazeltest命令,遇到错误:“Constraintsfrom@bazel_tools//platformshavebeenremoved.Pleaseuseconstraintsfrom@platformsrepositoryembeddedinBazel,orpreferablydeclaredependencyonhttps://github.com/bazelbuild/platforms.Seehttps://gi......
  • tensorflow 版本不同 报错合集
    1、"AttributeError:module'tensorflow'hasnoattribute'random_normal'"问题解决办法使用importtensorflow.compat.v1astftf.disable_v2_behavior()替换importtensorflowastf 2、tensorflow.python.framework.errors_impl.InvalidArgume......
  • vue 开源项目 安装脚手架报错问题 ERESOLVE unable to resolve dependency tree
       在安装项目依赖时,很大可能会遇到安装不成功的问题,其中有一个很大的原因,可能就是因为你的npm版本导致的 使用--force或--legacy-peer-deps可解决这种情况。--force会无视冲突,并强制获取远端npm库资源,当有资源冲突时覆盖掉原先的版本。--legacy-peer-deps标志是在v7......
  • 解决 Dapper.Contrib 报错“值对于 Int32 太大或太小”问题
    需求:在使用Dapper.Contrib执行Insert方法的时候,插入单条数据对于自增主键字段的返回值为插入后的值,当返回值大于2147483647的时候,Dapper.Contrib会抛出异常“值对于Int32太大或太小”问题。使用Dapper.Contrib插入数据的时候,报错“值对于Int32太大或太小”,此时数据库......
  • CentOS8安装Docker报错问题解决
    问题描述CentOS版本:8.5.2111。#cat/etc/redhat-releaseCentOSLinuxrelease8.5.2111安装准备:#安装所需软件包sudoyuminstall-yyum-utilsdevice-mapper-persistent-datalvm2#设置docker仓库:推荐阿里云sudoyum-config-manager--add-repohttp://mirrors.al......
  • nvidia-docker启动容器报错 Unknown runtime specified nvidia
    使用nvidia-docker创建容器时报错:Errorresponsefromdaemon:Unknownruntimespecifiednvidia.See'dockerrun--help'.主要原因在于配置docker镜像时,daemon.json文件被修改了。只要添加对应内容即可。vim /etc/docker/daemon.json原文件:{"registry-mirr......