有人建议调查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
文件夹中