Parcel 打包示例(React HelloWorld)
parcel 打包特点
极速打包时间
parcel 使用 worker 进程去启用多核编译。同时有文件系统缓存,即使在重启构建后也能快速再编译。
将你所有的资源打包
parcel 具备开箱即用的对 js, css, html, 文件 及更多的支持,而且不需要插件。
自动转换
如若有需要,babel, postcss, 和posthtml甚至 node_modules 包会被用于自动转换代码.
配置代码分拆
使用动态 import() 语法, parcel 将你的输出文件束(bundles)分拆,因此你只需要在初次加载时加载你所需要的代码。
热模块替换
parcel 无需配置,在开发环境的时候会自动在浏览器内随着你的代码更改而去更新模块。
友好的错误日志
当遇到错误时,parcel 会输出 语法高亮的代码片段,帮助你定位问题。
使用 parcel 打包的 react helloworld 应用。github 地址:
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> <div id="root"></div> <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
github 地址:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。