本文介绍了Webpack-dev-server编译文件,但不刷新或使编译的javascript可供浏览器使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用webpack-dev-server编译文件并启动一个开发Web服务器。

I'm trying to use webpack-dev-server to compile files and start up a dev web server.

在我的 package.json 中,我将脚本属性设置为:

In my package.json I have the script property set to:

"scripts": {
  "dev": "webpack-dev-server --hot --inline",
 }

所以 - 热 - inline 应该启用网络服务器和热重新加载(据我所知)。

So the --hot and --inline should enable the webserver and the hot reloading (as I understand it).

在我的 webpack.config.js 文件中,我设置了入口,输出和devServer设置以及添加一个加载器,用于查找 .vue 文件中的更改:

In my webpack.config.js file I set the entry, output, and devServer settings as well as add a loader to look for changes in .vue files:

module.exports = {
    entry: './src/index.js',
    output: {
        path: __dirname + '/public',
        publicPath: '/public',
        filename: 'bundle.js'
    },
    devtool: 'source-map',
    devServer:{
        contentBase: __dirname + '/public'
    },
    module:{
        loaders:[
            { test: /\.vue$/, loader: 'vue'}
        ]
    }
};

因此,通过此设置,我运行 npm run dev 。 webpack-dev-server启动,模块加载器测试工作(即当我保存任何.vue文件时导致webpack重新编译),但是:

So with this setup, I run npm run dev. The webpack-dev-server starts up, the module loader test works (i.e. when I save any .vue file it causes webpack to recompile), but:


  • 浏览器永不刷新

  • 存储在内存中的已编译的javascript永远不会提供给浏览器

在第二个项目符号中,我可以看到这一点,因为在浏览器窗口中,vue占位符永远不会被替换,如果我打开javascript控制台,Vue实例永远不会被创建或全局可用。

On that second bullet, I can see this because in the browser window the vue placeholders are never replaced and if I open up the javascript console the Vue instance is never created or made available globally.

我缺少什么?

推荐答案

这里有两件事导致我的问题:

Two things were causing my problems here:

module.exports = {
    entry: './src/index.js',
    output: {

        // For some reason, the `__dirname` was not evaluating and `/public` was
        // trying to write files to a `public` folder at the root of my HD.
        path: __dirname + '/public', 

        // Public path refers to the location from the _browser's_ perspective, so 
        // `/public' would be referring to `mydomain.com/public/` instead of just
        // `mydomain.com`.
        publicPath: '/public',
        filename: 'bundle.js'
    },
    devtool: 'source-map',
    devServer:{

        // `contentBase` specifies what folder to server relative to the 
        // current directory. This technically isn't false since it's an absolute
        // path, but the use of `__dirname` isn't necessary. 
        contentBase: __dirname + '/public'
    },
    module:{
        loaders:[
            { test: /\.vue$/, loader: 'vue'}
        ]
    }
};

这是固定的 webpack.config.js

var path = require('path');

module.exports = {
    entry: [
        './src/PlaceMapper/index.js'
    ],
    output:{
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'public/')
    },
    devtool: 'source-map',
    devServer:{
        contentBase: 'public'
    },
    module:{
        loaders:[
            { test: /\.vue$/, loader: 'vue'}
        ]
    }
};

这篇关于Webpack-dev-server编译文件,但不刷新或使编译的javascript可供浏览器使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:50