1 回答

TA貢獻1829條經驗 獲得超4個贊
有人回答建議查看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>
<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文件夾中
添加回答
舉報