我用vue cli创建了一个vue项目.运行yarn build时的正常输出是一个dist文件夹,其中包含一个index.html,以及一个js和css子目录,其中包含相应的.js和.css文件.

I want the build output to be a single html file that contains the js and css.

我在项目的根目录中添加了一个vue.config.js文件,并将其设置为输出一个js文件,这样就可以了.但我只想有一个带有js的html文件,以及html文件中已有的任何css.

module.exports = {
    css: {
        extract: false,
    },
    configureWebpack: {
      optimization: {
        splitChunks: false
      }
    }
  }

基本上,我希望我的html文件是这样的:

<html lang=en>
    <head>
        ... meta tags
        <title>my title</title>
    </head>
    <body>
        <div id=app></div>
        <script>
           // contents of the output js file here
        </script>
    </body>
</html>

这可能吗?

使用Vue 3.9.3

推荐答案

有人建议调查html-webpack-inline-source-plugin人,但删除了他们的答案.但这正是我完成这件事所需要的.

该插件不是特定于Vue或Vue CLI的,但如果您执行以下操作,它就会工作:

1) 在应用程序的根目录中添加一个vue.config.js文件.

2) 上面链接的插件实际上是另一个包的扩展.两者都需要.

npm install --save-dev html-webpack-plugin 
npm install --save-dev html-webpack-inline-source-plugin 

3)

// vue.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
module.exports = {
    css: {
        extract: false,
    },
    configureWebpack: {
      optimization: {
        splitChunks: false // makes there only be 1 js file - leftover from earlier attempts but doesn't hurt
      },
      plugins: [
        new HtmlWebpackPlugin({
          filename: 'output.html', // the output file name that will be created
          template: 'src/output-template.html', // this is important - a template file to use for insertion
          inlineSource: '.(js|css)$' // embed all javascript and css inline
        }),
        new HtmlWebpackInlineSourcePlugin()
      ]
    }
  }

4) 添加一个模板.这对于在Vue上下文中工作是必要的,因为如果没有它,默认情况下输出的html文件将不会有必要的<div id="app"></div>,Vue也不会装载到任何内容.我基本上采用了正常的输出html文件,并对其进行了一些修改.

<!-- output-template.html -->
<!DOCTYPE html>
<html lang=en>
    <head>
        <meta charset=utf-8>
        <meta http-equiv=X-UA-Compatible content="IE=edge">
        <meta name=viewport content="width=device-width,initial-scale=1">        
        <title>example title</title>
    </head>
    <body><noscript><strong>We're sorry but my example doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript>
        <div id=app>

        </div>
        <!-- plugin will insert js here by default -->
    </body>
</html>

然后像正常一样构建,output.html文件将位于/dist文件夹中

Vue.js相关问答推荐

如何只提取一次用于无线电组过滤的数据,而不是针对数据库中的每个条目提取数据?

元素不会对react 性props 的更改做出react

在 Vue 中更改 json 数据后,如何在屏幕上重新呈现表格组件?

v-on 的 .native 修饰符仅在组件上有效,但在 标签上使用

如何在 vuejs 中导入和使用本地 .csv 文件

避免在运行时向 Vue 实例或其根 $data 添加响应式属性

在 Vue 项目之间共享assets和组件

未捕获的类型错误:无法读取未定义的属性initializeApp

Nuxt & Vuetify:如何控制 CSS 文件的加载顺序?

使用 Vue.js (cdn) 在特定元素上切换显示/隐藏

Angular 5 中的 功能

执行调度程序刷新期间未处理的错误,这可能是一个 Vue 内部错误

将 ref 的内容从子组件传递到另一个 VueJS 子组件

在vue中单击时如何模糊元素

Vue Router beforeRouteLeave 不会停止子组件

仅在提交时验证 vuetify 文本字段

如何在我的 vue js 中实现无限滚动

使用 CloudFront 部署在 S3 上的 VueJS 应用程序的指定的密钥不存在

如何在 vue3 中的setup方法中发出事件?

Vue-meta:metaInfo 无权访问计算(computed)属性