美文网首页Web开发
Webpack操作指南

Webpack操作指南

作者: nimw | 来源:发表于2018-09-11 08:37 被阅读4次

初始化

  1. 创建项目文件夹
    design
  2. 创建package.json文件
    ➜ design npm init
  3. 安装webpack以及webpack-cli
    ➜ design npm i webpack webpack-cli --save-dev

模块打包

  1. 创建打包配置文件
    webpack.dev.config.js
  2. 创建入口文件
    ./src/index.js
  3. 编辑打包配置文件
module.exports = {
  entry: './src/index.js',
  output: {
    path: __dirname,
    filename: './release/bundle.js' 
  }
}
  1. 创建打包命令
    package.json文件
"scripts": {
    "dev": "webpack --config ./webpack.dev.config.js --mode development"
  }
  1. 执行打包命令
    npm run dev
    此时,生成release文件夹,内部有打包后文件bundle.js

热更新

  1. 安装依赖
    ➜ design npm i webpack-dev-server html-webpack-plugin --save-dev
  2. 创建html模板文件
    ./index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>前端设计模式</title>
</head>
<body>
  <h1>前端设计模式</h1>
</body>
</html>
  1. 编辑打包配置文件
    webpack.dev.config.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: './src/index.js',
  output: {
    path: __dirname,
    filename: './release/bundle.js' 
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './index.html'
    })
  ],
  devServer: {
    open: true, //浏览器自动打开
    port: 9000
  }
}
  1. 编辑打包命令
    package.json文件
"scripts": {
    "dev": "webpack-dev-server --config ./webpack.dev.config.js --mode development"
  },
  1. 执行打包命令
    npm run dev
    此时,浏览器自动打开http://localhost:9000,以./index.html为模板,以./release/bundle.jsJavaScript脚本。且实现热更新(修改 ./src/index.js文件时,浏览器自动刷新)。

开发调试

  1. 编辑打包配置文件
    webpack.dev.config.js
module.exports = {
  //...
  devtool: 'eval-source-map',
  //...
}

代理

  1. 编辑打包配置文件
    webpack.dev.config.js
module.exports = {
  //...
  devServer: {
    open: true, //浏览器自动打开
    port: 9000,
    proxy: {
      '/api/*': {
        target: 'http://localhost:8880'
        //访问hocalhost:9000/api:*时,会被代理到hocalhost:8880/api:*
      }
    }
  }
}

此时,访问hocalhost:9000/api:*时,会被代理到hocalhost:8880/api:*

ES6语法

  1. 安装依赖
    npm install babel-loader@7 babel-core babel-preset-env --save-dev
  2. 编辑打包配置文件
    webpack.dev.config.js
module.exports = {
 // ...
 module: {
    rules: [{
    test: /\.js$/,
      exclude: /(node_modules|bower_components)/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['env'],
          plugins: []
        }
      }
    }]
  },
 //...
}

ES7语法

  1. 安装依赖
    npm install babel-preset-stage-0 --save-dev
  2. 编辑打包配置文件
module.exports = {
 // ...
 module: {
    rules: [{
    test: /\.js$/,
      exclude: /(node_modules|bower_components)/,
      use: {
        loader: 'babel-loader',
        options: {
          presets:  ["env","stage-0"],
          plugins: []
        }
      }
    }]
  },
 //...
}

ES7提案-装饰器

  1. 安装依赖
    npm install babel-plugin-transform-decorators-legacy --save-dev
  2. 编辑打包配置文件
    webpack.dev.config.js
module.exports = {
 // ...
 module: {
    rules: [{
    test: /\.js$/,
      exclude: /(node_modules|bower_components)/,
      use: {
        loader: 'babel-loader',
        options: {
          presets:  ["env","stage-0"],
          plugins: ["transform-decorators-legacy"]
        }
      }
    }]
  },
 //...
}

参考资料

Javascript 设计模式系统讲解与应用

相关文章

  • Webpack操作指南

    初始化 创建项目文件夹design 创建package.json文件➜ design npm init 安装web...

  • Webpack打包结果分析

    一. 最简单的Webpack打包项目 单个文件打包(1) 按照Webpack操作指南中初始化、模块打包、模块热更新...

  • webpack 简介 以及和gulp区别

    参考Webpack傻瓜式指南 第一章:Webpack 缘起和入门Webpack傻瓜式指南 第二章 Webpack进...

  • 计算机电子书 2018 BiliDrive 备份

    下载方式 根据你的操作系统下载不同的 BiliDrive 二进制。 执行: 链接 文档链接Webpack 中文指南...

  • 入门 Webpack@3 的配置过程

    Webpack webpack 指南 React 的 Webpack 配置 PS: 文章结尾有完整实例启动方法:G...

  • Webpack5更新指南

    Webpack5更新指南 2020-10-10 Webpack5 正式 realease 相较于 Webpack4...

  • webpack入门

    webpack入坑之旅(一)不是开始的开始 Webpack傻瓜式指南(一)

  • webpack打包

    Webpack入门指南 一、什么是 webpack? webpack是一款模块加载器兼打包工具,它能把各种资源,例...

  • vue项目中配置全局常量

    使用webpack插件webpack.DefinePlugin设置全局常量,可减少某些操作。 在webpack插件...

  • Webpack入门

    随手笔记,参考资料: Webpack入门 Webpack 中文指南 官方文档 阮一峰教程 调试代码 一、What ...

网友评论

    本文标题:Webpack操作指南

    本文链接:https://www.haomeiwen.com/subject/xsylqxtx.html