webpack的使用1

來源:互聯網
上載者:User

標籤:建立   mod   text   img   div   output   變數   pre   項目   

1、安裝(windows)

安了npm後,npm install webpack -g,全域位置在 C:\Users\lihaihong\AppData\Roaming\npm\node_modules 

然後專案檔夾 =>

npm init  #會自動產生一個package.json檔案     //自己寫package.json就npm installnpm install webpack --save-dev #將webpack增加到package.json檔案中

安裝開發工具

npm install webpack-dev-server --save-dev

(spm install module-name -save 自動把模組和版本號碼添加到dependencies部分——產品發布依賴
spm install module-name -save-dve 自動把模組和版本號碼添加到devdependencies部分——開發時依賴

)

2、使用

添加index.html,entry.js

<html>    <head>        <meta charset="utf-8">    </head>    <body>        <script type="text/javascript" src="bundle.js" charset="utf-8"></script>    </body></html>
document.write("看看如何讓它工作!");

運行下

添加模組了

建立module1.js

module.exports = ‘模組1‘

entry.js修改:

document.write("看看如何讓它工作!");document.write(require(‘./module1.js‘)) // 添加模組

運行下,看變化

------先運行entry.js,再執行require

3、載入器loader,處理js之外的其他檔案

style.css

background:yellow;

安裝對應的npm install css-loader style-loader,我一起安裝報錯,分開安裝好了

entry.js加

require("!style-loader!css-loader!./style.css") // 載入 style.css

運行:webpack entry.js bundle.js => 背景變色

外部引入的樣式怎麼弄呢?

4、配置,可直接webpack

webpack.config.js

var webpack = require(‘webpack‘);module.exports = {  entry: ‘./entry.js‘, //__dirname+‘/x‘ 也可以  output: {    path: __dirname,  // __dirname+‘/a‘ ----就在根目錄a檔案下    filename: ‘bundle.js‘  //‘[name]-[hash].js‘ ----main-63b4a6ae43aac97bb23d.js  },  module: {    loaders: [      {test: /\.css$/, loader: ‘style-loader!css-loader‘}    ]  }}
註:“__dirname”是node.js中的一個全域變數,它指向當前執行指令碼所在的目錄。

可改entry.js 

require(‘./style.css‘)

運行webpack,效果同前

這樣就算打包了,將模組打包到指定js裡

5、執行webpack => npm start

在package.json裡改

scripts:{

  "start":"webpack"

}

 

 

參考文獻:

http://webpackdoc.com/install.html

 

webpack的使用1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.