webpack2 热加载js 文件

Posted Hello World

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webpack2 热加载js 文件相关的知识,希望对你有一定的参考价值。

如果只要普通的热加载 只要如下配置就好了

package.json

{
  "devDependencies": {
    "webpack": "^2.6.1",
    "webpack-dev-server": "^2.4.5"
  },
  "scripts": {
    "start": "webpack-dev-server"
  }
}

 

webpack.config.js

module.exports = {
    entry: __dirname + ‘/js/test.js‘,
    output: {
        // 注意这里是 publicPath
        publicPath: "/dist/",
        filename: "bundle_test.js"
    }
}

 

若要使用 es6 的语法, 需加载babel 文件, 注意要先安装 babel-cli

package.json 如下:

{
  "devDependencies": {
      "babel-cli": "^6.24.1",
    "babel-core": "^6.25.0",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^2.6.1",
    "webpack-dev-server": "^2.4.5"
  },
  "scripts": {
    "start": "webpack-dev-server"
  }
}

 

webpack.config.js 如下:

module.exports = {
    entry: __dirname + ‘/js/test.js‘,
    output: {
        publicPath: "/dist/",
        filename: "bundle_test.js"
    },
    module: {
        loaders: [
            {
                test: /\.json$/,
                loader: "json-loader"
            },
            {
                test: /\.js$/,
                loader: "babel-loader"
            }
        ]
    }
}

并且还要加一个 .babelrc 文件, 如下:

{
    "presets": ["es2015", "react"]
}

 

以上是关于webpack2 热加载js 文件的主要内容,如果未能解决你的问题,请参考以下文章

06HMR热加载

nodejs实现热加载

webpack2的一些使用入门

升级webpack2

vue+webpack2实现路由的懒加载

webpack 2 css 通过条目加载导致问题