image.png
    image.png
    image.png
    安装(有全局和局部):
    npm install webpack-dev-server —save

    webpack.config.js:

    1. const webpack = require('webpack')
    2. const extractTextCss = require('extract-text-webpack-plugin');
    3. const HtmlWebpackPlugin = require('html-webpack-plugin');
    4. module.exports={
    5. mode:'production',
    6. entry:{
    7. app:'./src/app.js',
    8. },
    9. output:{
    10. path:__dirname+"/dist",
    11. filename:'[name].bundle.js'
    12. },
    13. module:{
    14. rules:[
    15. {
    16. test:/\.less$/,
    17. use:extractTextCss.extract({
    18. fallback:{
    19. loader:'style-loader',
    20. options:{
    21. //insertInto:"#mydiv",
    22. singleton:true,
    23. //transform:"./transform.js"
    24. }
    25. },
    26. use:[
    27. {
    28. loader:'css-loader',
    29. options:{
    30. modules:{
    31. localIdentName:'[path][name]_[local]_[hash:4]'
    32. }
    33. }
    34. },
    35. {
    36. loader:'less-loader'
    37. }
    38. ]
    39. })
    40. }
    41. ]
    42. },
    43. devtool:'eval-source-map',
    44. devServer:{
    45. port: 9001,
    46. inline:true,
    47. overlay:true,
    48. hot:true,
    49. hotOnly:true,
    50. historyApiFallback:{
    51. rewrites:[
    52. {
    53. from:/^\/([ -~]+)/,
    54. to:function(context){
    55. return './'+context.match[1]+'.html'
    56. }
    57. }
    58. ]
    59. },
    60. proxy:{
    61. "/smartSpec":{
    62. target:"https://mooc.study.163.com/",
    63. changeOrigin:true,
    64. pathRewrite:{
    65. "^/smartSpec/qd":"/smartSpec/detail/1202816603.htm"
    66. },
    67. },
    68. }
    69. },
    70. plugins: [
    71. new HtmlWebpackPlugin({
    72. filename: 'index.html',
    73. template: './src/index.html',
    74. minify: {
    75. collapseWhitespace: true
    76. },
    77. inject:true,
    78. }),
    79. new extractTextCss({
    80. filename:"app.bundle.css",
    81. disable:true
    82. }),
    83. ]
    84. }

    package.json:

    配置了一个命令dev,运行的是局部webpack-dev-server。

    1. "scripts": {
    2. "test": "echo \"Error: no test specified\" && exit 1",
    3. "dev": "webpack-dev-server --open"
    4. }

    image.png

    inline: 会在页面最上面显示一个状态信息,默认值是true不显示,如果置为false则出现。
    image.png

    overlay: 会将错误显示在页面(true),而不是控制台上。

    historyApiFallback: true,发现找不到路径,则停留在原页面。
    from,to 重定向。

    1. historyApiFallback:{
    2. rewrites:[
    3. {
    4. from:/^\/([ -~]+)/,
    5. to:function(context){
    6. return './'+context.match[1]+'.html'
    7. }
    8. }
    9. ]
    10. }

    image.png

    app.js:

    1. import test from "./test.less";
    2. import test2 from "./test2.less";
    3. import $ from 'jquery';
    4. $.ajax({
    5. url:"/smartSpec/qd",
    6. type:'get',
    7. success:function(res){
    8. console.log(res);
    9. }
    10. });
    11. var i=0;
    12. document.getElementById('mydiv').setAttribute('class',test2.div1);
    13. console.log(j.a);

    如果不使用代理的话,会出现跨域错误问题。
    changeOrigin默认是false:请求头中host仍然是浏览器发送过来的host。如果设置成true:发送请求头中host会设置成target。

    1. proxy:{
    2. "/smartSpec":{
    3. target:"https://mooc.study.163.com/",
    4. changeOrigin:true,
    5. pathRewrite:{
    6. "^/smartSpec/qd":"/smartSpec/detail/1202816603.htm"
    7. },
    8. },
    9. }

    热更新:
    image.png
    如果不配置热更新,更新代码的时候,会刷新页面,也就是live loading。

    extract-text-webpack-plugin 和热更新并兼容,所以要设置disable:

    1. new extractTextCss({
    2. filename:"app.bundle.css",
    3. disable:true
    4. })

    在app.js中设置一个定时器:

    1. var i = 0;
    2. setInterval(function(){
    3. i++;
    4. document.getElementById("mydiv").innerHTML = i;
    5. });

    在webpack.config.js文件中也设置了

    1. {
    2. hot: true,
    3. hotOnly: true
    4. }

    当我们更改app.js文件的时候,发现页面刷新了。这是因为我们还需要在app文件中写入,判断module是否有hot模块,如果有则accept。

    1. if(module.hot){
    2. module.hot.accept();
    3. }

    经过上面的更改,在修改定时器,会发现页面并无在刷新,而页面上会出现两个计时器,一个是改之前,一个是改之后的,两个计时器间隔显示。