10

这是我的 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'
};

在 package.json 中:

   "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --inline",
    "build": "webpack --config webpack.config.prod.js"
  },

当我运行 npm start 时,在 localhost 中,js 文件未注入index.html. 如果我运行 webpack 或 npm run build,js 文件注入成功。当我在本地主机时,html-webpack-plugin也可以注入 js 文件吗?index.html

4

3 回答 3

6

此问题与 webpack-development-server 无法将文件写入本地文件系统而是将其文件写入仅 MEMORY的事实有关。

这就是为什么当您运行默认的 webpack 构建命令(不是WDS / Webpack-Development-Server)时,您能够使用 Html-Webpack-Plugin 正确生成文件的原因:

webpack 

或者,由于您使用的是 vue.js Webpack-simple 项目(https://github.com/vuejs-templates/webpack-simple/tree/master/template),您还可以使用 Vue 附带的 npm 脚本。 js 示例(位于 package.json 中)通过:

npm run build

在任何一种情况下,文件都被写入目录,因为您认为它们应该是因为使用 webpack 构建可以写入文件系统,而使用 Webpack-Development-Server 时没有写入文件(同样这不起作用,因为 WDS 写入到内存而不是本地文件系统)。

由于您的评论,我在处理同一问题时偶然发现了这个答案:

“如果我运行webpack或npm run build,js文件注入成功。html-webpack-plugin是否也可以在我在localhost时将js文件注入index.html?”

总结一下:当 Html-Webpack-Plugin用作Webpack -Development-Server (WDS) 的一部分时,即使 Html-Webpack-Plugin写入文件(使用相同的 webpack配置)当使用正常的 webpack 构建过程(无 WDS)时。

于 2017-01-03T20:33:50.973 回答
0

我有同样的问题,下面的配置对我有用。

我曾经有绝对路径,当将其更改为相对路径时,它可以工作。

new HtmlWebpackPlugin({
        inject: true,
        template:'public/index.html', // relative to project root 
        filename:'index.html'         // relative to build folder
    })
于 2019-04-20T05:38:12.117 回答
-1

你可以试试这样的配置..

new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      inject: true
    })

和 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>

您将需要安装npm i -D html-webpack-plugin vue-loader vue-html-loader

我建议你从模板创建一个项目,vue init webpack

此模板使用 html-webpack-plugin

于 2016-09-20T21:14:22.870 回答