一、初始webpack

來源:互聯網
上載者:User

標籤:css   type   pos   config   com   UI   顏色   test   還需要   

webpack支援的三種模組化方式: AMD,COMMON,ES6

webpack官網很多文檔都在

  • Configuration

在webpack中,任何資源都視為一個模組

loader的處理方式是從右至左

 

webpack-2:

https://qiutc.me/post/webpack-2-%E7%9A%84%E6%96%B0%E7%89%B9%E6%80%A7-What-s-new-in-webpack-2.html

        

一、初始webpack

webpack官網地址:http://webpack.github.io/docs/what-is-webpack.htm

  1.       安裝全域 => npm install webpack –g;

  2.       進入檔案根目錄,例如test;

  3.       初始化npm  => npm init  (斷行符號建立package.json);

        

  4.       在檔案夾安裝webpack => npm install webpack - -save-dev;安裝完之後有檔案夾node_modules;

  5.       在根目錄test下建立hello.js,

       

      -- 使用webpack打包檔案hello.js; 命令列 => webpack hello.js hello.bundle.js(打包以後檔案的名稱);

      -- 打包完成會給一些資訊:雜湊;版本;時間;Asset(打包產生的檔案);size(檔案大小);chunks(這次打包的分塊);chunk Names (這次打包的塊名稱)

      -- 打包完以後的檔案hello.bundle.js,上面有一大段代碼是在打包的過程中,產生的它自己所需要的一些內建函數;比如webpack_require; 在後面能看見我們的代碼,以及給這個代碼塊的編號;

  6.       建立一個檔案,熟悉下webpack的工作原理;

       

      -- 建立一個world.js,在hello.js檔案中 rquire () 引用進來;

      

      -- 再運行一下 webpack hello.js hello.bundle.js

      -- 打包完成,會有兩個模組(0,1);再看一下檔案名稱內容,world已經被打包進來,hello.js裡面的require() 已經變成了webpack 內建的 webpack_require;這是webpack的一種工作方式;尋找它的依賴然後把它們打包在一起;

  7.       試一下打包css檔案

      -- 建立一個style.css  

       

      -- 在hello.js檔案中 rquire 進來;

       

      打包;會報錯 You may need an appropriate loader to handle this file type.(你需要一個適當的loader來處理這種檔案!)

      -- 這說明webpack 天生是不支援這種類型.css的,要處理這種類型需要依賴loader;

      -- 在項目目錄下安裝這個css-loader;配合css-loaer 還需要 style-loader;

      npm install css-loader style-loader –-save-dev;

      再運行一下webpack hello.js hello.bundle.js 還是報錯;檔案中require()的css並沒有指定相應的loder;

      -- require(‘css-loader!./style.css‘);這種文法,在引用這個檔案之前,必須先經過css-loader的處理;

       

      在運行一下命令;css檔案已被打包進來;也多了一些其他的自訂的內建函數;

  8.       為了更清晰的展示效果,在建一個html檔案;引入打包後的檔案;

      ——在hello中,添加執行函數代碼 hello(‘hello world‘);

       

      打包;然後在瀏覽器開啟html檔案,hello函數已經執行;

      ——再修改一下css 比如給body加背景顏色;

       

      打包;重新整理頁面;並沒有效果;因為我們要是css生效,還需要藉助一個loader:style-loader; require(‘style-loader!css-loader!./style.css‘);

       

      打包;重新整理頁面;發現css生效;

      F12看下代碼,會發現我們寫的css,被建立了一個style標籤插在了head裡面;這是通過style-loader做到的;

      style-loader:把css-loader處理完的檔案,建立一個style標籤放在head裡面;

      css-loader: 是的webpack可以處理.css檔案;

   9.       如果不把loader放在require裡,可以通過命令列工具;

       

      Webpack hello.js hello.bundle.js –module-bind ‘css=style-loader!css-loader’;

      模組綁定,如果是css, 用style-loader,style-loader之前用css-loader處理;

  10.   –watch監聽;檔案改變就會打包;

      Webpack hello.js hello.bundle.js –module-bind ‘css=style-loader!css-loader’ --watch

  11.   --progress 可以看到打包過程;有個百分比讀條

  12.   –display-modules 可以看到打包的模組 會把引用的所有的模組會列出來

  13.   –display-reasons 會有打包模組的原因

一、初始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.