webpack 的簡單使用

來源:互聯網
上載者:User

標籤:方便   檔案   添加   public   ttext   ini   test   更新   type   

  • 參考:
    http://www.jianshu.com/p/c28e5df8647d http://www.jianshu.com/p/397df5dfd09c

1.該案例的目錄結果如下

 

zpvue

    app

        main.js

        module1.js

        module2.js

    public

        bundle.js

        index.html

    package.json

    webpack.config.js

 

2.安裝 webpack

 

  npm install webpack --save-dev --registry=https://registry.npm.taobao.org

 

使用以下命令進行webpack 初始化

 

  npm init

 

配置webpack.json檔案

 

{

  "name": "zpvue",

  "version": "1.0.0",

  "description": "",

  "main": "index.js",

  "scripts": {

    "test": "echo \"Error: no test specified\" && exit 1",

    "start":"webpack -p"    //對應5.webpack 的命令

  },

  "author": "zeopean",

  "license": "ISC",

  "devDependencies": {

    "css-loader": "^0.26.0",

    "extract-text-webpack-plugin": "^1.0.1",

    "sass-loader": "^4.0.2",

    "style-loader": "^0.13.1",

    "webpack": "^1.13.3"

  }

}

  • scripts:start 明亮對應 =>5.webpack 的命令
  • scripts.devDependencies 在3.安裝載入器時會主動添加

3.安裝loader 載入器

 

  npm install sass-loader style-loader css-loader extract-text-webpack-plugin --save-dev --registry=https://registry.npm.taobao.org

 

4.配置webpack.config.js 檔案

 

/**

 * Created by zeopean on 16/11/19.

 */

var webpack = require(‘webpack‘)

var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {

    //頁面入口

    entry:{

        index:  ‘./app/main.js‘

    },

    //入口檔案輸出配置

    output:{

        path:‘./public‘,

        filename:‘bundle.js‘

    },

    //載入模組

    module:{

        loaders:[{

            test:‘/\.sass$/‘,

            loader:ExtractTextPlugin.extract(‘style-loader‘, ‘css-loader!sass-loader‘)

        }]

    },

    plugins:[

        new ExtractTextPlugin(‘[name].css‘)

    ]

};

  • entry 表示入口
  • output 表示輸出打包後的bundle.js
  • module表示載入器

5.webpack 的命令

 

關於webpack的啟動方式:

 

webpack         // 最基本的啟動webpack的方法

 

webpack -w      // 提供watch方法;即時進行打封裝更新

 

webpack -p      // 對打包後的檔案進行壓縮

 

webpack -d      // 提供source map,方便調式代碼

 

6.引入輸出的bundle.js

 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>webpack demo</title>

</head>

<body>

 

</body>

<script type="text/javascript" src="../public/bundle.js"></script>

</html>

webpack 的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.