javascript - 使用 webpack-dev-server 时,html-webpack-plugin 不会将 js 文件注入(inject) index.html

标签 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'
};

在 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 时,在本地主机中,js 文件未注入(inject)到 index.html 中。 如果我运行 webpack 或 npm run build,js 文件注入(inject)成功。 当我在本地主机时,html-webpack-plugin 是否也可以将 js 文件注入(inject)到 index.html 中?

最佳答案

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

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

webpack 

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

npm run build

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

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

"If I run webpack or npm run build, the js file is injected successfully. Can html-webpack-plugin also inject js file into index.html when I'm in localhost?"

总结一下: 当 Html-Webpack-Plugin 作为 Webpack-Development-Server (WDS) 的一部分使用时,它不会将文件写入本地文件系统,即使 Html-Webpack-Plugin 在使用正常的 webpack 构建过程(无 WDS)时写入文件(具有相同的 webpack 配置)。

关于javascript - 使用 webpack-dev-server 时,html-webpack-plugin 不会将 js 文件注入(inject) index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39585680/

相关文章:

node.js - Webpack 突然无法编译,因为 "Module not found: Error: Can' tsolve"错误

javascript - 翻译 Bootstrap-Vue.js 中的表头

javascript - 输入字段仅接受数值使用 Vue.js 中的指令

javascript - Service Worker 和 AJAX

javascript - Hammertime 垂直滚动不起作用

javascript - 如何在 apache 服务器上部署没有 index.html 的 React 应用程序以进行生产

Azure DevOps - 如何在部署期间使用 webpack 构建 bundle

node.js - 为什么 package.json 文件中有 'type: module'?

javascript - 单击时强制悬停状态辅助元素

javascript - 如何检查对象中多个数组的数组长度?