登录  /  注册
首页 > web前端 > js教程 > 正文

Parcel打包示例详解

小云云
发布: 2018-01-16 13:20:22
原创
1996人浏览过

本文主要介绍了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
登录后复制

GitHub 地址: https://github.com/justjavac/parcel-example/tree/master/react-helloworld

相关推荐:

打包工具parcel零配置vue开发脚手架

Parcel.js和Vue 2.x 极速零配置打包体验示例

详解nodejs实现简单的gulp打包


以上就是Parcel打包示例详解的详细内容,更多请关注php中文网其它相关文章!

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学