[webpack3.8.1]Guides-4-Output Management(輸出管理)

Output Management

This guide extends on code examples found in the Asset Management guide.

???????這個指南將在上一個指南的基礎上對示例代碼進行擴展。

So far we've manually included all our assets in our index.html file, but as your application grows and once you start using hashes in filenames and outputting multiple bundles, it will be difficult to keep managing your index.html file manually. However, a few plugins exist that will make this process much easier to manage.

???????到現在為止,我們已經給index.html手動包含了所有的資源,但是,一旦開始使用哈希值為文件名并輸出多個包,index.html將難以手動管理文件。然而,有一些插件可以使這個過程更容易管理。

Preparation (預備)

First, let's adjust our project a little bit:

???????首先,讓我們調整一下我們的項目:

project

  webpack-demo|- package.json|- webpack.config.js|- /dist|- /src|- index.js
+   |- print.js|- /node_modules

Let's add some logic to our src/print.js file:

???????讓我們添加一些邏輯到src/print.js

src/print.js

export default function printMe() {console.log('I get called from print.js!');
}

And use that function in our src/index.js file:

??????? 在我們的src/index.js使用那個函數吧。

src/index.js

  import _ from 'lodash';
+ import printMe from './print.js';function component() {var element = document.createElement('div');
+   var btn = document.createElement('button');element.innerHTML = _.join(['Hello', 'webpack'], ' ');+   btn.innerHTML = 'Click me and check the console!';
+   btn.onclick = printMe;
+
+   element.appendChild(btn);return element;}document.body.appendChild(component());

Let's also update our dist/index.html file, in preparation for webpack to split out entries:

??????? 我們也給我們的dist/index.html升升級,準備webpack拆分條目。

dist/index.html

  <html><head>
-     <title>Asset Management</title>
+     <title>Output Management</title>
+     <script src="./print.bundle.js"></script></head><body>
-     <script src="./bundle.js"></script>
+     <script src="./app.bundle.js"></script></body></html>

Now adjust the config. We'll be adding our src/print.js as a new entry point (print) and we'll change the output as well, so that it will dynamically generate bundle names, based on the entry point names:

??????? 現在,調整配置。我們將要添加我們的src/print.js作為一個新的條目,而且,我們還要改變輸出,以便于它能夠動態地生成基于入口點名稱的捆綁后的名稱。

webpack.config.js

  const path = require('path');module.exports = {entry: {
-     index: './src/index.js',
+     app: './src/index.js',
+     print: './src/print.js'},output: {
-     filename: 'bundle.js',
+     filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist')}};

Let's run npm run build and see what this generates:

???????讓我們運行npm run build,看看生成了什么:

Hash: aa305b0f3373c63c9051
Version: webpack 3.0.0
Time: 536msAsset     Size  Chunks                    Chunk Namesapp.bundle.js   545 kB    0, 1  [emitted]  [big]  app
print.bundle.js  2.74 kB       1  [emitted]         print[0] ./src/print.js 84 bytes {0} {1} [built][1] ./src/index.js 403 bytes {0} [built][3] (webpack)/buildin/global.js 509 bytes {0} [built][4] (webpack)/buildin/module.js 517 bytes {0} [built]+ 1 hidden module

We can see that webpack generates our print.bundle.js and app.bundle.js files, which we also specified in our index.html file. if you open index.html in your browser, you can see what happens when you click the button.

???????我們可以看到,webpack生成了我們的print.bundle.jsapp.bundle.js還有很多我們在index.html中定義的文件。如果你在瀏覽器中打開index.html,當你點擊按鈕時你就可以看到發生了什么。

But what would happen if we changed the name of one of our entry points, or even added a new one? The generated bundles would be renamed on a build, but our index.html file would still reference the old names. Let's fix that with the HtmlWebpackPlugin.

???????但是,如果我們把其中一個入口點改名,會發生什么呢?更甚者,我們添加了一個新的會發生什么呢?我們生成的打包的文件將會在編譯中重命名,但是我們的index.html文件仍然關聯的是舊文件名。讓我們使用HtmlWebpackPlugin來修復這個bug。

Setting up HtmlWebpackPlugin (啟動HtmlWebpackPlugin)

First install the plugin and adjust the webpack.config.js file:

???????首先,安裝插件,調整webpack.config.js文件:

npm install --save-dev html-webpack-plugin

webpack.config.js

  const path = require('path');
+ const HtmlWebpackPlugin = require('html-webpack-plugin');module.exports = {entry: {app: './src/index.js',print: './src/print.js'},
+   plugins: [
+     new HtmlWebpackPlugin({
+       title: 'Output Management'
+     })
+   ],output: {filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist')}};

Before we do a build, you should know that the HtmlWebpackPlugin by default will generate its own index.html file, even though we already have one in the dist/ folder. This means that it will replace our index.html file with a newly generated one. Let's see what happens when we do an npm run build:

???????在我們開始一個編譯之前,你應當知道HtmlWebpackPlugin是會默認創建一個index.html的,即便我們已經有一個放在dist/文件夾里了(`?ω?′)。這就意味著呀,我們的index.html將會被覆蓋 Σ(っ°Д°;)っ讓我們來看一下,當我們運行npm run build會發生什么吧:

Hash: 81f82697c19b5f49aebd
Version: webpack 2.6.1
Time: 854msAsset       Size  Chunks                    Chunk Namesprint.bundle.js     544 kB       0  [emitted]  [big]  printapp.bundle.js    2.81 kB       1  [emitted]         appindex.html  249 bytes          [emitted][0] ./~/lodash/lodash.js 540 kB {0} [built][1] (webpack)/buildin/global.js 509 bytes {0} [built][2] (webpack)/buildin/module.js 517 bytes {0} [built][3] ./src/index.js 172 bytes {1} [built][4] multi lodash 28 bytes {0} [built]
Child html-webpack-plugin for "index.html":[0] ./~/lodash/lodash.js 540 kB {0} [built][1] ./~/html-webpack-plugin/lib/loader.js!./~/html-webpack-plugin/default_index.ejs 538 bytes {0} [built][2] (webpack)/buildin/global.js 509 bytes {0} [built][3] (webpack)/buildin/module.js 517 bytes {0} [built]

If you open index.html in your code editor, you'll see that the HtmlWebpackPlugin has created an entirely new file for you and that all the bundles are automatically added.

???????如果你在你的代碼編輯器中打開了index.html,你將會看到HtmlWebpackPlugin已經為你創建了一個全新的文件,而且所有需要捆綁的文件都已經自動地添加上了。

If you want to learn more about all the features and options that the HtmlWebpackPlugin provides, then you should read up on it on the HtmlWebpackPlugin repo.

???????如果你想學習更多HtmlWebpackPlugin提供的關于特性和選項的內容,然后你應當專門攻讀HtmlWebpackPlugin的報告。

You can also take a look at html-webpack-template which provides a couple of extra features in addition to the default template.

???????你也可以稍微看看html-webpack-template,它提供了一組默認模板額外的特性。

Cleaning up the /dist folder (/dist文件夾大掃除)

As you might have noticed over the past guides and code example, our /dist folder has become quite cluttered. Webpack will generate the files and put them in the /dist folder for you, but it doesn't keep track of which files are actually in use by your project.

???????你應該也注意到了,經過一系列的指南和咱們的代碼例子,咱這個/dist文件夾啊,已經非常凌亂了(╬ ̄皿 ̄)

In general it's good practice to clean the /dist folder before each build, so that only used files will be generated. Let's take care of that.

???????通常來說,在每一次編譯之前清理/dist文件夾是一種優良品格(~ ̄▽ ̄)~ 。這樣呀,只有真正被使用的文件才會生成。讓我們來處理一下吧。

  1. popular plugin to manage this is the clean-webpack-plugin so let's install and configure it.

???????clean-webpack-plugin是所有做這類事情的流行插件中的一個,那么好,我們來安裝和配置一下它吧!

npm install clean-webpack-plugin --save-dev

webpack.config.js

  const path = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');
+ const CleanWebpackPlugin = require('clean-webpack-plugin');module.exports = {entry: {app: './src/index.js',print: './src/print.js'},plugins: [
+     new CleanWebpackPlugin(['dist']),new HtmlWebpackPlugin({title: 'Output Management'})],output: {filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist')}};

Now run an npm run build and inspect the /dist folder. If everything went well you should now only see the files generated from the build and no more old files!

???????現在(?ˉ?ˉ?),我們運行npm run build并且監視/dist文件夾,如果一切正常,你將只會看到編譯后的文件,之前的舊文件沒啦!

The Manifest (清單)

You might be wondering how webpack and its plugins seem to "know" what files are being generated. The answer is in the manifest that webpack keeps to track how all the modules map to the output bundles. If you're interested in managing webpack's output in other ways, the manifest would be a good place to start.

???????你也許會好奇,webpack和它的插件是怎么知道哪些文件是需要生成的,哪些沒用呢?答案是這樣的,webpack會一直跟蹤更新一個清單,這個清單上會記錄所有的模塊到輸出的映射關系。如果你對于使用其它方式來管理webpack的輸出,manifest是一個不錯的選擇。

The manifest data can be extracted into a json file for easy consumption using the WebpackManifestPlugin.

???????清單數據可以提取到一個JSON文件,以便于使用WebpackManifestPlugin。

We won't go through a full example of how to use this plugin within your projects, but you can read up on the concept page and the caching guide to find out how this ties into long term caching.

???????我們將不會詳細介紹如何在您的項目中使用這個插件,但是您可以閱讀概念頁面和緩存指南,了解這些內容如何與長期緩存聯系起來。

Conclusion (總結一下)

Now that you've learned about dynamically adding bundles to your HTML, let's dive into the development guide. Or, if you want to dig into more advanced topics, we would recommend heading over to the code splitting guide.

???????現在,你已經學習了有關如何動態添加綁定到你的HTML,讓我們一頭扎進開發指南的海洋里吧。或者,如果你想深挖更多的更高級的主題,我們強烈推薦您趕緊踏上代碼分割指南的征途。

P.S.:本來想很嚴肅地翻譯官網的指南來著,突然間發現沒幾個小盆友看, ̄へ ̄,干脆加點自己喜歡的宅文化表情。希望愛逛B站的小伙伴們喜歡。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/285226.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/285226.shtml
英文地址,請注明出處:http://en.pswp.cn/news/285226.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

有條件地 [JsonIgnore]

前言通常&#xff0c;在進行 JSON 序列化或反序列化時&#xff0c;如果要忽略某個屬性&#xff0c;我們會使用 [JsonIgnore] 特性&#xff1a;public class User {public int Id { get; set; }[JsonIgnore]public string Name { get; set; } }var user new User { Id 1, Name…

C語言試題145之創建一個鏈表

??個人主頁:個人主頁 ??系列專欄:C語言試題200例 ??推薦一款模擬面試、刷題神器?? 點擊跳轉進入網站 ?作者簡介:大家好,我是碼莎拉蒂,CSDN博客專家(全站排名Top 50),阿里云博客專家、51CTO博客專家、華為云享專家 1、題目 題目:創建一個鏈表 2 、溫馨提示 …

[轉]IntelliJ IDEA 2019.3正式發布,給我們帶來哪些新特性?

每篇一句 工欲善其事必先利其器 ——《論語衛靈公》 前言 千呼萬喚始出來。自從JetBrains在今年7月24日發布了IDEA 2019.2版本后&#xff0c;從9月份開始我便一直在關注此版本正式版的發布。JetBrains公司在9月中旬就對外公布了下一個主要版本 2019.3的Roadmap&#xff0c;而且…

Git中的日常使用 碼云

http://git.mydoc.io/?t83143轉載于:https://www.cnblogs.com/yangyuqiu/p/6164822.html

【譚浩強版】C語言程序設計(第三版)課后習題完整答案附源碼--高等教育出版社

文章目錄 第一章 語言程序設計的概念第二章 基本數據類型第三章 C語言程序的流程控制第四章 模塊化程序設計第五章 數組第六章 指針第一章 語言程序設計的概念 1.1 請編寫一個程序, 顯示以下兩行文字。 #include <stdio.h> main() {printf("I am a student.\n&qu…

php判斷是否為json格式的方法

http://www.poluoluo.com/jzxy/201403/265005.html 首先要記住json_encode返回的是字符串, 而json_decode返回的是對象 判斷數據不是JSON格式: .代碼如下:function is_not_json($str){ return is_null(json_decode($str));}判斷數據是合法的json數據: (PHP版本大于5.3) .代碼如…

FineReport中以jws方式調用WebService數據源方案

在使用WebService作為項目的數據源時&#xff0c;希望報表中也是直接調用這個WebService數據源&#xff0c;而不是定義數據連接調用對應的數據庫表&#xff0c;這樣要怎么實現呢&#xff1f; 在程序中訪問WebService應用服務&#xff0c;將WebService返回的數據轉為程序數據集&…

C語言試題148之海灘上有一堆桃子,五只猴子來分。第一只猴子把這堆桃子憑據分為五份,多了一個,這只 猴子把多的一個扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了 一個,它同樣把多的

??個人主頁:個人主頁 ??系列專欄:C語言試題200例 ??推薦一款模擬面試、刷題神器?? 點擊跳轉進入網站 ?作者簡介:大家好,我是碼莎拉蒂,CSDN博客專家(全站排名Top 50),阿里云博客專家、51CTO博客專家、華為云享專家 1、題目 題目:海灘上有一堆桃子,五只猴子…

一文讀懂什么是CTO、技術VP、技術總監、首席架構師

究竟什么是CTO&#xff0c;一個公司真的需要CTO么&#xff1f;哪些公司的職位對于技術管理者來講真的是CTO的職位&#xff1f;同樣是技術最高負責人&#xff0c;為什么有人叫CTO、有人叫技術總監、技術VP&#xff0c;有人叫首席架構師&#xff1f;他們之間的差別是什么&#xf…

與MySQL傳統復制相比,GTID有哪些獨特的復制姿勢?

與MySQL傳統復制相比&#xff0c;GTID有哪些獨特的復制姿勢? http://mp.weixin.qq.com/s/IF1Pld-wGW0q2NiBjMXwfg 陳華軍&#xff0c;蘇寧云商IT總部資深技術經理&#xff0c;從事數據庫服務相關的開發和維護工作&#xff0c;之前曾長期從事富士通關系數據庫的開發&#xff0c…

【ArcGIS Pro微課1000例】0007:ArcGIS Pro 2.5質量檢查:拓撲創建與編輯案例教程

文章目錄 1. 加載矢量數據2. 創建數據庫、要素數據集3. 拓撲創建4. 拓撲錯誤編輯與修改1. 加載矢量數據 矢量數據可以是單獨shp格式的文件數據,也可是存在于數據庫中的要素類。 2. 創建數據庫、要素數據集 創建數據庫 無論是在ArcMap,還是ArcGIS Pro中,創建拓撲都需要在…

C語言試題149之809乘以??=800乘以??+9乘以??+1 其中??代表的兩位數,8乘以??的結果為兩位數,9乘以??的結果為 3 位數。求??代表 的兩位數,及 809乘以??后的結果

??個人主頁:個人主頁 ??系列專欄:C語言試題200例 ??推薦一款模擬面試、刷題神器?? 點擊跳轉進入網站 ?作者簡介:大家好,我是碼莎拉蒂,CSDN博客專家(全站排名Top 50),阿里云博客專家、51CTO博客專家、華為云享專家 1、題目 題目:809*??=800*??+9*??+1…

[轉]想要成為一名優秀的Java程序員,這份文檔必讀

A、規則&#xff08;1–2 級&#xff09; 1、在switch 中每個 case 語句都應該包含 break 或者 return 。 2、不要使用空的for 、if 、while 語句。 3、在運算中不要減小數據的精度。 4、switch 語句中的 case 關鍵字要和后面的常量保持一個空格&#xff0c;switch 語句中不要定…

QC 環境安裝條件

1、HP-QC設置 HP Quality Center環境設置是一個復雜的過程&#xff0c;它不是一般的安裝&#xff0c;我們一個windows操作系統上開展。 Enterprise安裝將會分發&#xff0c;但是&#xff0c;對于學習/評估目的之一&#xff0c;可以在獨立的服務器上安裝HP-ALM11.5的試用版。還應…

.Net之時間輪算法(終極版)定時任務

TimeWheelDemo一個基于時間輪原理的定時任務對時間輪的理解其實我是有一篇文章(.Net 之時間輪算法(終極版)[1])針對時間輪的理論理解的&#xff0c;但是&#xff0c;我想&#xff0c;為啥我看完時間輪原理后&#xff0c;會采用這樣的方式去實現。可能只是一些小技巧不上大雅之堂…

phpstorm config include paths for swoole

配置phpstorm 當你寫swoole 類或者函數時會自動補全 https://github.com/swoole/ide-helper.git 克隆下這個工具包 點加&#xff0c;然后指定你下載好的工具包路徑&#xff0c;點ok 本文轉自 skinglzw 51CTO博客&#xff0c;原文鏈接&#xff1a;http://blog.51cto.com/sking…

C語言試題150之八進制轉換為十進制

??個人主頁:個人主頁 ??系列專欄:C語言試題200例 ??推薦一款模擬面試、刷題神器?? 點擊跳轉進入網站 ?作者簡介:大家好,我是碼莎拉蒂,CSDN博客專家(全站排名Top 50),阿里云博客專家、51CTO博客專家、華為云享專家 1、題目 題目:八進制轉換為十進制 2 、溫馨…

JavaScript全面學習(中階)

1.typeof操作符總是返回一個字符串&#xff1a; typeof 123; // number typeof NaN; // number typeof str; // string typeof true; // boolean typeof undefined; // undefined typeof Math.abs; // function typeof null; // object typeof []; // object typeof {}; // obj…

【ArcGIS風暴】ArcGIS創建柵格數據集色彩映射表案例--以GlobeLand30土地覆蓋數據為例

矢量數據快速符號化&#xff0c;可以將常用的樣式保存到樣式符號庫&#xff0c;柵格數據快速符號化&#xff0c;需要創建色彩映射表。本文以GlobeLand30土地覆蓋數據為例&#xff0c;詳解ArcGIS中創建與使用色彩映射表。 文章目錄一、 ArcGIS色彩映射表介紹二、土地覆蓋數據色彩…

Visual Studio 2019 16.3.10 初體驗

Visual Studio 2019 版本 16.3.10 發布時間&#xff1a;2019 年 11 月 20 日 官網地址&#xff1a;https://visualstudio.microsoft.com/zh-hans/vs/ 介紹&#xff1a; https://devblogs.microsoft.com/visualstudio/dot-net-core-support-in-visual-studio-2019-version-16…