零配置 JS 打包工具 Parcel 初体验

Parcel 是一个新的 JavaScript 打包工具,其特点是零配置、速度快。

今天使用 Parcel 打包了一个 React 的 HelloWorld 应用,记录一下开发过程。

0. 新建目录

  
  
  1. mkdir react-helloworld

  2. cd react-helloworld

1. 初始化 npm

  
  
  1. yarn init -y

  
  
  1. npm init -y

此时会创建要给 package.json 文件,文件内容:

  
  
  1. {

  2.  "name": "parcel-example-react-helloworld",

  3.  "version": "1.0.0",

  4.  "description": "",

  5.  "main": "index.js",

  6.  "scripts": {

  7.    "test": "echo \"Error: no test specified\" && exit 1"

  8.  },

  9.  "keywords": [],

  10.  "author": "",

  11.  "license": "ISC"

  12. }

2. 添加 React

yarn:

  
  
  1. yarn add react react-dom

npm:

  
  
  1. npm install react react-dom --save

package.json 文件内容:

  
  
  1. {

  2.   "name": "parcel-example-react-helloworld",

  3.   "version": "1.0.0",

  4.   "description": "",

  5.   "main": "index.js",

  6.   "scripts": {

  7.     "test": "echo \"Error: no test specified\" && exit 1"

  8.   },

  9.   "keywords": [],

  10.   "author": "",

  11. -  "license": "ISC"

  12. +  "license": "ISC",

  13. +  "dependencies": {

  14. +    "react": "^16.2.0",

  15. +    "react-dom": "^16.2.0"

  16. +  }

  17. }

3. 添加 Babel

新建 .babelrc 文件

  
  
  1. touch .babelrc

输入内容:

  
  
  1. {

  2.  "presets": ["react"]

  3. }

添加 babel-preset-react:

yarn:

  
  
  1. yarn add babel-preset-react -D

npm:

  
  
  1. npm install babel-preset-react --D

此时 package.json 文件内容:

  
  
  1. {

  2.   "name": "parcel-example-react-helloworld",

  3.   "version": "1.0.0",

  4.   "description": "",

  5.   "main": "index.js",

  6.   "scripts": {

  7.     "test": "echo \"Error: no test specified\" && exit 1"

  8.   },

  9.   "keywords": [],

  10.   "author": "",

  11.   "license": "ISC",

  12.   "dependencies": {

  13.     "react": "^16.2.0",

  14.     "react-dom": "^16.2.0"

  15. -   }

  16. +   },

  17. +   "devDependencies": {

  18. +     "babel-preset-react": "^6.24.1"

  19. +   }

  20. }

5. 添加 Parcel

yarn:

  
  
  1. yarn add parcel-bundler -D

npm:

  
  
  1. npm install parcel-bundler --D

此时 package.json 文件内容:

  
  
  1. {

  2.   "name": "parcel-example-react-helloworld",

  3.   "version": "1.0.0",

  4.   "description": "",

  5.   "main": "index.js",

  6.   "scripts": {

  7.     "test": "echo \"Error: no test specified\" && exit 1"

  8.   },

  9.   "keywords": [],

  10.   "author": "",

  11.   "license": "ISC",

  12.   "dependencies": {

  13.     "react": "^16.2.0",

  14.     "react-dom": "^16.2.0"

  15.    },

  16.    "devDependencies": {

  17. -      "babel-preset-react": "^6.24.1"

  18. +      "babel-preset-react": "^6.24.1",

  19. +      "parcel-bundler": "^1.0.3"    

  20.    }

  21. }

6. 新建 index.html 文件

内容

  
  
  1. <html>

  2. <body>

  3.    <div id="root"></div>

  4.    <script src="./index.js"></script>

  5. </body>

  6. </html>

7. 新建 index.js 文件

  
  
  1. import React from "react";

  2. import ReactDOM from "react-dom";

  3. const App = () => {

  4.  return <h1>Hello World!</h1>;

  5. };

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

8. 添加打包命令

  
  
  1. {

  2.   "name": "parcel-example-react-helloworld",

  3.   "version": "1.0.0",

  4.   "description": "",

  5.   "main": "index.js",

  6.   "scripts": {

  7. -    "test": "echo \"Error: no test specified\" && exit 1"

  8. +    "start": "parcel index.html"

  9.   },

  10.   "keywords": [],

  11.   "author": "",

  12.   "license": "ISC",

  13.   "dependencies": {

  14.     "react": "^16.2.0",

  15.     "react-dom": "^16.2.0"

  16.    },

  17.    "devDependencies": {

  18.       "babel-preset-react": "^6.24.1"

  19.       "babel-preset-react": "^6.24.1",

  20.       "parcel-bundler": "^1.0.3"    

  21.    }

  22. }

9. 完成

运行

  
  
  1. yarn start

  
  
  1. npm start

在浏览器中打开 http://localhost:1234

打包过程会生产 .cache 和 dist 两个目录,如果是 git 工程,可以新建 .gitignore 文件忽略这两个目录:

  
  
  1. .cache

  2. dist

  3. node_modules


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值