为了账号安全,请及时绑定邮箱和手机立即绑定

webpack深入与实战

难度中级
时长 3小时21分
学习人数
综合评分9.60
259人评价 查看评价
9.8 内容实用
9.5 简洁易懂
9.5 逻辑清晰
  • output中的path 在4版本中必须是绝对路径

    http://img1.sycdn.imooc.com//5e4fa57c0001289210270117.jpg

    var path=require('path');

    output:{

    path:path.resolve(__dirname,'dist/js'),

    filename:'b.js'

    }


    查看全部
  • 这一节中用到的命令行在webpack4下稍有不同

    webpack hello.js -o helo.d.js --mode=development --module-bind css=style-loader!css-loader

    第一 生成目标文件前要加 -o

    第二 需要指定mode

    第三 module-bind 后面不需要加引号

    查看全部
  • Webpack,必须学习的
    查看全部
  • 安装image-webpack-loader:

    https://www.npmjs.com/package/image-webpack-loader

    npm install image-webpack-loader --save-dev


    webpack.config.js

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              },
              {
                  test: /\.html$/,
                  loader: 'html-loader'
              },
              {
                  test: /\.tpl$/,
                  loader: 'ejs-loader'
              },
              {
                  test: /\.css$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      { loader: 'css-loader', options: { importLoaders: 1 } },
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      }
                  ]
              },
              {
                  test: /\.less$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      'css-loader',
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      },
                      { loader: 'less-loader'}
                  ]
              },
              {
                    test: /\.scss$/,
                    use: [
                        'style-loader',
                        // 用来处理css中引入的css
                        'css-loader',
                        {
                            loader: 'postcss-loader',
                            options: {
                                ident: 'postcss',
                                plugins: [
                                    require('autoprefixer')({
                                        broswers: ['last 5 versions']
                                    }),
                                ]
                            }
                        },
                        { loader: 'sass-loader'}
                    ]
              },
              {
                  test: /\.(png|jpg|gif|svg)$/i,
                  use: [
                      {
                          loader: 'url-loader',
                          options: {
                              limit: 200000,
                              name: 'assets/[name]-[hash:5].[ext]'
                          }
                      },
                      {
                          loader: 'image-webpack-loader',
                          options: {
                              disable: true
                          }
                      }
                  ],
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    查看全部
  • 安装url-loader:

    npm install url-loader --save-dev


    webpack.config.js

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              },
              {
                  test: /\.html$/,
                  loader: 'html-loader'
              },
              {
                  test: /\.tpl$/,
                  loader: 'ejs-loader'
              },
              {
                  test: /\.css$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      { loader: 'css-loader', options: { importLoaders: 1 } },
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      }
                  ]
              },
              {
                  test: /\.less$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      'css-loader',
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      },
                      { loader: 'less-loader'}
                  ]
              },
              {
                    test: /\.scss$/,
                    use: [
                        'style-loader',
                        // 用来处理css中引入的css
                        'css-loader',
                        {
                            loader: 'postcss-loader',
                            options: {
                                ident: 'postcss',
                                plugins: [
                                    require('autoprefixer')({
                                        broswers: ['last 5 versions']
                                    }),
                                ]
                            }
                        },
                        { loader: 'sass-loader'}
                    ]
              },
              {
                  test: /\.(png|jpg|gif|svg)$/i,
                  loader: 'url-loader',
                  query: {
                      limit: 300000,
                      name: 'assets/[name]-[hash:5].[ext]'
                  }
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    查看全部
  • 处理图片文件:

    npm install file-loader --save-dev

    layer.less:

    @import "modal.less";
    .layer {
      width: 600px;
      height: 200px;
      background-color: green;
    
      div {
        width: 400px;
        height: 100px;
        background: url('../../assets/bg.png');
      }
    }


    webpack.config.js:

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              },
              {
                  test: /\.html$/,
                  loader: 'html-loader'
              },
              {
                  test: /\.tpl$/,
                  loader: 'ejs-loader'
              },
              {
                  test: /\.css$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      { loader: 'css-loader', options: { importLoaders: 1 } },
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      }
                  ]
              },
              {
                  test: /\.less$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      'css-loader',
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      },
                      { loader: 'less-loader'}
                  ]
              },
              {
                    test: /\.scss$/,
                    use: [
                        'style-loader',
                        // 用来处理css中引入的css
                        'css-loader',
                        {
                            loader: 'postcss-loader',
                            options: {
                                ident: 'postcss',
                                plugins: [
                                    require('autoprefixer')({
                                        broswers: ['last 5 versions']
                                    }),
                                ]
                            }
                        },
                        { loader: 'sass-loader'}
                    ]
              },
              {
                  test: /\.(png|jpg|gif|svg)$/i,
                  loader: 'file-loader',
                  query: {
                                    name: 'assets/[name]-[hash:5].[ext]'
                                }
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    查看全部
  • 安装ejs-loader:

    npm install ejs-loader --save-dev


    app.js

    import './css/common.css';
    import Layer from './components/layer/layer.js';
    
    const App = function() {
        var dom = document.getElementById('app');
        var layer = new Layer();
        dom.innerHTML = layer.tpl({
            name: 'john',
            arr: ['apple', 'xiaomi', 'oppo']
        });
    };
    
    new App();


    webpack.config.js

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              },
              {
                  test: /\.html$/,
                  loader: 'html-loader'
              },
              {
                  test: /\.tpl$/,
                  loader: 'ejs-loader'
              },
              {
                  test: /\.css$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      { loader: 'css-loader', options: { importLoaders: 1 } },
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      }
                  ]
              },
              {
                  test: /\.less$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      'css-loader',
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      },
                      { loader: 'less-loader'}
                  ]
              },
              {
                    test: /\.scss$/,
                    use: [
                        'style-loader',
                        // 用来处理css中引入的css
                        'css-loader',
                        {
                            loader: 'postcss-loader',
                            options: {
                                ident: 'postcss',
                                plugins: [
                                    require('autoprefixer')({
                                        broswers: ['last 5 versions']
                                    }),
                                ]
                            }
                        },
                        { loader: 'sass-loader'}
                    ]
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    layer.tpl

    <div class="layer">
        <div> this is a <%= name %> layer </div>
        <% for (var i = 0;i < arr.length; i++ ) { %>
        <%= arr[i] %>
        <% } %>
    </div>


    layer.js

    import  './layer.less';
    import tpl from './layer.tpl';
    
    function layer() {
        return {
            name: 'layer',
            tpl: tpl
        };
    }
    
    export default layer;


    查看全部
    0 采集 收起 来源:处理模板文件

    2020-01-31

  • 安装html-loader:

    https://www.npmjs.com/package/html-loader

    npm install html-loader --save-dev


    layer.html

    <div class="layer">
        <div>
            this is a layer
        </div>
    </div>


    layer.js

    import  './layer.less';
    import tpl from './layer.html';
    
    function layer() {
        return {
            name: 'layer',
            tpl: tpl
        };
    }
    
    export default layer;


    webpack.config.js

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              },
              {
                  test: /\.html$/,
                  loader: 'html-loader'
              },
              {
                  test: /\.css$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      { loader: 'css-loader', options: { importLoaders: 1 } },
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      }
                  ]
              },
              {
                  test: /\.less$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      'css-loader',
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      },
                      { loader: 'less-loader'}
                  ]
              },
              {
                    test: /\.scss$/,
                    use: [
                        'style-loader',
                        // 用来处理css中引入的css
                        'css-loader',
                        {
                            loader: 'postcss-loader',
                            options: {
                                ident: 'postcss',
                                plugins: [
                                    require('autoprefixer')({
                                        broswers: ['last 5 versions']
                                    }),
                                ]
                            }
                        },
                        { loader: 'sass-loader'}
                    ]
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    app.js

    import './css/common.css';
    import Layer from './components/layer/layer.js';
    
    const App = function() {
        var dom = document.getElementById('app');
        var layer = new Layer();
        dom.innerHTML = layer.tpl;
    };
    
    new App();


    index.html

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>webpack app</title>
      </head>
      <body>
        <div id="app"></div>
      </body>
    </html>


    查看全部
    0 采集 收起 来源:处理模板文件

    2020-01-31

  • layer.less

    .layer {
      width: 600px;
      height: 200px;
      background-color: green;
    
      div {
        width: 400px;
        height: 100px;
        background-color: red;
      }
    
      .flex {
        display: flex;
      }
    }


    layer.html

    <div class="layer">
        <div>
    
        </div>
    </div>


    查看全部
  • const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              },
              {
                  test: /\.css$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      { loader: 'css-loader', options: { importLoaders: 1 } },
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      }
                  ]
              },
              {
                  test: /\.less$/,
                  use: [
                      'style-loader',
                      // 用来处理css中引入的css
                      'css-loader',
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      },
                      { loader: 'less-loader'}
                  ]
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    查看全部
  • 添加postcss-loader:

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              },
              {
                  test: /\.css$/,
                  use: [
                      'style-loader',
                      { loader: 'css-loader', options: { importLoaders: 1 } },
                      {
                          loader: 'postcss-loader',
                          options: {
                              ident: 'postcss',
                              plugins: [
                                  require('autoprefixer')({
                                      broswers: ['last 5 versions']
                                  }),
                              ]
                          }
                      }
                  ]
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };

    效果如下:

    http://img1.sycdn.imooc.com//5e329ebd0001638d13930473.jpg

    查看全部
  • postcss-loader:

    https://www.npmjs.com/package/postcss-loader


    npm install postcss-loader --save-dev 
    npm install autoprefixer --save-dev
    
    // 官网示例只需要安装一个
    npm i -D postcss-loader


    查看全部
  • const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              },
              {
                  test: /\.css$/,
                  loader: 'style-loader!css-loader'
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    查看全部
  • const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader',
                  // 排除
                  exclude: /node_modules/,
                  query: {
                      presets: ["@babel/preset-env"]
                  }
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    查看全部
  • 安装babel:

    npm install --save-dev babel-loader @babel/core 
    npm install @babel/preset-env --save-dev


    webpack.config.js

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    
    module.exports = {
        entry: './src/app.js',
        output: {
            filename: 'js/[name].bundle.js',
            path: path.resolve(__dirname, 'dist')
        },
        module: {
            rules: [
              {
                  test: /\.js$/,
                  loader: 'babel-loader'
              }
          ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                filename: 'index.html',
                // html 标题
                title: 'this is a.html',
                // 使用模板
                template: 'index.html',
                // script标签插入位置
                inject: 'body'
            }),
        ],
    };


    package.json

    {
      "name": "webpack-demo",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      // 引入babel
      "babel": {
        "presets": ["@babel/preset-env"]
      },
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "build": "webpack --config webpack.dev.config.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "@babel/core": "^7.8.3",
        "@babel/preset-env": "^7.8.3",
        "babel-loader": "^8.0.6",
        "clean-webpack-plugin": "^3.0.0",
        "css-loader": "^3.4.2",
        "file-loader": "^5.0.2",
        "html-webpack-plugin": "^3.2.0",
        "style-loader": "^1.1.3",
        "webpack": "^4.41.5",
        "webpack-cli": "^3.3.10"
      },
      "dependencies": {
        "lodash": "^4.17.15"
      }
    }


    查看全部

举报

0/150
提交
取消
课程须知
1、对模块化开发有一些了解 2、使用过 node 对 npm 有基本的了解 3、对打包有一个基本的概念
老师告诉你能学到什么?
1、模块化的开发 2、webpack 的 CLI (命令行) 3、webpack 如何在项目中打包 4、webpack 如何处理项目中的资源文件

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!