webpack入門(四)——webpack loader 和plugin

來源:互聯網
上載者:User

標籤:文檔   方便   顯示   情況   令行   rom   pretty   png   x-loader   

什麼是loader

loaders是你用在app源碼上的轉換元件。他們是用node.js啟動並執行,把源檔案作為參數,返回新的資源的函數。 
例如,你可以用loaders告訴webpack載入 coffeeScript或者JSX。 
loaders 特點: 
1. 可以鏈式拼接。他們用在通向檔案的管道,最後一個loader預期返回一個javascript,其它Loader可以返回任意格式給下一個loader。 
2. loaders可以是同步的,也可以是非同步。 
3. loaders是用node.js來跑,可以做一切可能的事情。 
4. loaders接收query參數。這些參數會傳入 loaders內部作為配置來用。 
5. 在webpack config 中 loaders可以綁定到 副檔名/正則 。 
6. loaders 可以用npm 發布或者安裝。 
7. 除了用package.json 的main匯出的 loader外, 一般的模組也可以匯出一個loader。 
8. 裝載機可以訪問配置。 
9. plugin 可以給loaders更多功能。 
10. loader可以發出更多的任意檔案。

resoloving loaders

loader 的resolve跟 模組很像。一個loader預期匯出一個函數,而且是用相容javascript的nodepgn 的。一般情況下,用npm管理loader,但是你也可以在自己app內有loader檔案。

引用loader

為了方便,雖然不是必須的, loaders一般命名為xxx-loader, xxx就是loader的實義名。例如 json-loader。 
或許你已經用全名引用了loader(例如 json-loader),如果沒有你可以用它的短名(例如 json)。 
裝載機命名慣例和優先順序搜尋順序由webpack 配置API中的resolveLoader.moduleTemplates 定義。 
裝載機的命名規範可能在用require 文法引用時會有用。看下面的用法。

安裝 loader

如果 npm 上有 這個Loader,你可以像這下面這樣安裝

$ nppm install xxx-loader --save
  • 1

或者

$ npm install xxx-loader --save-dev
  • 1
用法

有很多種辦法在你的app中用loader: 
1. 顯示地在 require 中添加。 
2. 在設定檔中 配置。 
3. 在命令列配置。

在require 中用

注意:如果你不知道你的代碼在哪個環境(node.js和browser)中用盡量, 避免使用這個。 使用下一節那樣的配置。

在require語句中(或者 define, require.ensure等)你可以指定裝載機。只需要用 “!”將資源和Loader分開。每一部分會相對於當前檔案夾來resolve。它可能覆蓋config 檔案中用 “!”規定的全部loader。

require("./loader!./dir/file.txt");// => uses the file "loader.js" in the current directory to transform// "file.txt" in the folder "dir".require("jade!./template.jade");// => uses the "jade-loader" (that is installed from npm to "node_modules")// to transform the file "template.jade"// If configuration has some transforms bound to the file, they will still be applied.require("!style!css!less!bootstrap/less/bootstrap.less");// => the file "bootstrap.less" in the folder "less" in the "bootstrap"// module (that is installed from github to "node_modules") is// transformed by the "less-loader". The result is transformed by the// "css-loader" and then by the "style-loader".// If configuration has some transforms bound to the file, they will not be applied.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
config配置

你可以在config檔案中把loader綁定到 一個正則。

{    module: {        loaders: [            { test: /\.jade$/, loader: "jade" }, // => "jade" loader is used for ".jade" files { test: /\.css$/, loader: "style!css" }, // => "style" and "css" loader is used for ".css" files // Alternative syntax: { test: /\.css$/, loaders: ["style", "css"] }, ] }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
命令列

命令列中,你也可以把Loader綁定到副檔名上。

$ webpack --module-bind jade --module-bind ‘css=style!css‘
  • 1

上面的意思是用jade檔案 用“jade-loader”載入,css檔案用 “style-loader”和“css-loader”載入。

參數

loader可以傳入query字元作為參數(像瀏覽器中那樣),query字串跟在 Loader後面。例如 url-loader?mimetype=image/png。 
注意:查詢字串的格式是由載入程式決定。請去具體的loader文檔中查看。許多載入機可以接收正常的query字串(例如 ?key=value&key2=value2) 
,也可以接收JSON對象(?{“key”:”value”,”key1”:”value1”})。

看以下四種寫法。

require("url-loader?mimetype=image/png!./file.png");
  • 1
{ test: /\.png$/, loader: "url-loader?mimetype=image/png" }
  • 1
{    test: /\.png$/,    loader: "url-loader", query: { mimetype: "image/png" }}
  • 1
  • 2
  • 3
  • 4
  • 5
webpack --module-bind "png=url-loader?mimetype=image/png"
  • 1
使用外掛程式

在webpack中通常使用外掛程式添加與 bundles相關的功能。例如 BellOnBundlerErrorPlugin 會在打包進程中給你通知錯誤訊息。

內建外掛程式

直接用配置項的plugins屬性。

// webpack should be in the node_modules directory, install if not.var webpack = require("webpack");module.exports = { plugins: [ new webpack.ResolverPlugin([ new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"]) ], ["normal", "loader"]) ]};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
其它外掛程式

如果不是內建外掛程式,在npm 上發布過的外掛程式一般可以通過npm 安裝安裝,如果沒有發布,你要用其它方法了

npm install component-webpack-plugin
  • 1

然後像像下面這樣使用

var ComponentPlugin = require("component-webpack-plugin");module.exports = { plugins: [ new ComponentPlugin() ]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果你用npm裝第三方外掛程式,建議用 webpack-load-plugins .它能檢測所有第你安裝過並且在儲存在package中的三方外掛程式,在你需要用的時候會載入進來。

更多相關文章webpack入門(一)webpack入門(二)webpack入門(三)webpack入門(四)webpack入門(五)webpack入門(六)

去8鬥5車,查看更多技術文章。

webpack入門(四)——webpack loader 和plugin

聯繫我們

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