Xia*_*ang 8 javascript webpack vue.js webpack-dev-server html-webpack-plugin
这是我的webpack配置:
var path = require('path');
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var fs = require('fs'),buildPath='./dist/';
var folder_exists = fs.existsSync(buildPath);
if(folder_exists == true)
{
require('shelljs/global')
rm('-rf', 'dist')
};
module.exports = {
entry: './src/main',
output: {
path: path.join(__dirname, './dist'),
filename: '[name].js',
publicPath: '/dist/'
},
devServer: {
historyApiFallback: true,
hot: false,
inline: true,
grogress: true,
},
// "vue-hot-reload-api": "^1.2.2",
module: {
loaders: [
{ test: /\.vue$/, loader: 'vue' },
{ test: /\.js$/, loader: 'babel', exclude: /node_modules/ },
{ test: /\.css$/, loader: 'style-loader!css-loader'},
//install css-loader style-loader sass-loader node-sass --save-dev
{ test: /\.scss$/, loader: 'style!css!sass?sourceMap'},
{ test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192&name=images/[name].[ext]'},
{ test: /\.(html|tpl)$/, loader: 'html-loader' },
]
},
vue: {
loaders: {
js:'babel',
css: 'style-loader!css-loader',
sass:'style!css!sass?sourceMap'
}
},
babel: {
presets: ['es2015'],
plugins: ['transform-runtime']
},
plugins:[
new HtmlWebpackPlugin({
template: 'index.html',
filename: './index.html',
inject:true
}),
],
resolve: {
extensions: ['', '.js', '.vue'],
alias: {
filter: path.join(__dirname, './src/filters'),
components: path.join(__dirname, './src/components')
}
},
devtool: 'eval-source-map'
};
Run Code Online (Sandbox Code Playgroud)
在package.json中:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --inline",
"build": "webpack --config webpack.config.prod.js"
},
Run Code Online (Sandbox Code Playgroud)
当我运行npm start时,在localhost中没有注入js文件index.html.如果我运行webpack或npm run build,则会成功注入js文件.可html-webpack-plugin也注入js文件到index.html,当我在本地主机是?
此问题特别与webpack-development-server无法将文件写入本地文件系统并将其文件写入MEMORY的事实有关.
这就是为什么当您运行默认的webpack构建命令(不是 WDS/Webpack-Development-Server)时,您能够使用Html-Webpack-Plugin正确生成文件的原因:
webpack
Run Code Online (Sandbox Code Playgroud)
或者,因为您使用的是vue.js Webpack-simple项目(https://github.com/vuejs-templates/webpack-simple/tree/master/template),您还可以使用Vue附带的npm脚本. js sample(位于package.json内)通过:
npm run build
Run Code Online (Sandbox Code Playgroud)
在任何一种情况下,文件都会像你认为的那样被写入目录,因为使用webpack构建CAN可以编写文件系统,而在使用Webpack-Development-Server时没有写入任何文件(同样这也不起作用,因为WDS写道到内存而不是本地文件系统).
由于你的评论,我在处理同样的问题时偶然发现了这个答案:
"如果我运行webpack或npm运行构建,则会成功注入js文件.当我在localhost时,html-webpack-plugin是否也会将js文件注入index.html?"
综上所述:HTML-的WebPack,插件将不会当它被用来作为的WebPack -发展-服务器(WDS)的一部分写入文件到本地文件系统,即使HTML的的WebPack-插件WILL写文件(具有相同的WebPack配置)使用正常的webpack构建过程(没有WDS).
rid*_*nsb -1
你可以尝试这样的配置..
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
})
Run Code Online (Sandbox Code Playgroud)
和 Index.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, shrink-to-fit=no">
<title>My App</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
您将需要安装npm i -D html-webpack-plugin vue-loader vue-html-loader
但我建议您从模板创建一个项目,vue init webpack
该模板使用 html-webpack-plugin
| 归档时间: |
|
| 查看次数: |
9870 次 |
| 最近记录: |