Parcel 打包示例 - React HelloWorld

使用 Parcel 打包的 React HelloWorld 應用。GitHub 地址: https://github.com/justjavac/...html

0. 新建目錄

mkdir react-helloworld
cd react-helloworld

1. 初始化 npm

yarn init -y

java

npm init -y

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

{
  "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:react

yarn add react react-dom

npm:git

npm install react react-dom --save

package.json 文件內容:github

{
   "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 文件npm

touch .babelrc

輸入內容:json

{
  "presets": ["react"]
}

添加 babel-preset-react:瀏覽器

yarn:babel

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 地址: https://github.com/justjavac/...

相關文章
相關標籤/搜索