首頁  >  文章  >  web前端  >  使用Parcel如何打包

使用Parcel如何打包

亚连
亚连原創
2018-06-11 17:26:441873瀏覽

本篇文章主要介紹了Parcel 打包範例(React HelloWorld),詳細的介紹了Parcel打包的特點和使用範例,有興趣的可以了解一下

Parcel 打包特點

極速打包時間

Parcel 使用worker 進程去啟用多核心編譯。同時有檔案系統緩存,即使在重新啟動建置後也能快速再編譯。

 將你所有的資源打包

Parcel 具備開箱即用的對 JS, CSS, HTML, 文件 及更多的支持,而且不需要插件。

自動轉換

如若有需要,Babel, PostCSS, 和PostHTML甚至node_modules 套件會被用於自動轉換程式碼.

設定程式碼分拆

使用動態import() 語法, Parcel 將你的輸出檔案束(bundles)分拆,因此你只需要在初次載入時載入你所需要的程式碼。

 熱模組替換

Parcel 不需要配置,在開發環境的時候會自動在瀏覽器內隨著你的程式碼更改而去更新模組。

友善的錯誤日誌

當遇到錯誤時,Parcel 會輸出 語法高亮的程式碼片段,幫助你定位問題。

使用 Parcel 打包的 React HelloWorld 應用程式。 GitHub 網址: https://github.com/justjavac/parcel-example/tree/master/react-helloworld

0. 新目錄

mkdir react-helloworld
cd react-helloworld

# 1. 初始化npm

yarn init -y

npm init -y

此時會建立要給package.json 文件,文件內容:

{
 "name": "parcel-example-react-helloworld",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": [],
 "author": "",
 "license": "ISC"
}

2. 新增React

yarn:

yarn add react react-dom

npm:

npm install react react-dom --save

package.json 檔案內容:

 {
  "name": "parcel-example-react-helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
- "license": "ISC"
+ "license": "ISC",
+ "dependencies": {
+  "react": "^16.2.0",
+  "react-dom": "^16.2.0"
+ }
 }

3. 新增Babel

新建.babelrc 檔案

touch .babelrc

輸入內容:

{
 "presets": ["react"]
}

新增babel-preset-react:

yarn:

yarn add babel-preset-react -D

npm :

npm install babel-preset-react --D

此時package.json 檔案內容:

 {
  "name": "parcel-example-react-helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
   "react": "^16.2.0",
   "react-dom": "^16.2.0"
-  }
+  },
+  "devDependencies": {
+   "babel-preset-react": "^6.24.1"
+  }
 }

#5. 新增Parcel

yarn:

yarn add parcel-bundler -D

npm:

npm install parcel-bundler --D

此時package.json 檔案內容:

 {
  "name": "parcel-example-react-helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
   "react": "^16.2.0",
   "react-dom": "^16.2.0"
  },
  "devDependencies": {
-   "babel-preset-react": "^6.24.1"
+   "babel-preset-react": "^6.24.1",
+   "parcel-bundler": "^1.0.3"  
  }
 }

6. 新建index.html 檔案

內容

<html>
<body>
  <p id="root"></p>
  <script src="./index.js"></script>
</body>
</html>

#7. 新建index.js 檔案

import React from "react";
import ReactDOM from "react-dom";
const App = () => {
 return <h1>Hello World!</h1>;
};

ReactDOM.render(<App />, document.getElementById("root"));

8. 新增打包指令

 {
  "name": "parcel-example-react-helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
-  "test": "echo \"Error: no test specified\" && exit 1"
+  "start": "parcel index.html"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
   "react": "^16.2.0",
   "react-dom": "^16.2.0"
  },
  "devDependencies": {
    "babel-preset-react": "^6.24.1"
    "babel-preset-react": "^6.24.1",
    "parcel-bundler": "^1.0.3"  
  }
 }

9. 完成

執行

yarn start

npm start

在瀏覽器中開啟http://localhost:1234

打包過程會生產.cache 和dist 兩個目錄,如果是git 工程,可以新建.gitignore 檔案忽略這兩個目錄:

.cache
dist
node_modules

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

在cropper中js透過vue的圖片裁剪上傳功能如何實作

在Vuex中mutations與actions有什麼區別? (詳細教學)

在vue中如何實現剪裁圖片並上傳伺服器功能

如何解決easyui日期時間框ie的相容的實際問題(詳細教學)

詳細為你講解Immutable及React 中實作技巧

在Node.js中使用readline如何實作逐行讀取、寫入檔案內容

以上是使用Parcel如何打包的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn