Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

搭建 React 開發環境 #37

Open
futianshen opened this issue Nov 23, 2018 · 0 comments
Open

搭建 React 開發環境 #37

futianshen opened this issue Nov 23, 2018 · 0 comments
Labels

Comments

@futianshen
Copy link
Collaborator

futianshen commented Nov 23, 2018

  1. Git 初始化

    git init
    vim .gitignore

  2. Yarn 初始化

    yarn init

  3. 設定 Webpack

    yarn add webpack webpack-cli html-webpack-plugin webpack-dev-server

    webpack.config.js(webpack 設定檔)
    const path = require('path')
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    
    module.exports = {
      mode: 'development',
      entry: './src/index.js',
      module: {
        rules: [
          {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel-loader'
          }
        ]
      },
      output: {
        filename: 'bundle.[hash].js', // 防止瀏覽器 cache bundle.js
        path: path.resolve(__dirname, 'dist'), // .join 和 .resolve 有什麼差別 ? 'dist' of '/dist'
      },
      plugins: [
        new HtmlWebpackPlugin({
          template: './index.html' // 讓每次自動產生的 bundle.[hash].js 不需要手動重新引入 index.html
        })
      ]
    }

    使用 Webpack 的方法

    1. npx webpack
    2. yarn run start:hot
      package.json
      "scripts": {
        "start:hot":"webpack-dev-server --mode development --open --hot",
      }
    3. yarn run start
      package.json
      "scripts": {
        "start": "webpack --mode development"
      }
    4. yarn run build
      package.json
      "scripts": {
        "build": "webpack --mode production"
      }
  4. 設定 Babel

    yarn add @babel/core @babel/preset-env @babel/preset-react @babel/plugin-proposal-class-properties babel-loader

    .babelrc (告訴 babel 有那些格式要轉換)

    {
      "presets": ["@babel/preset-env", "@babel/preset-react"], /*  */
      "plugins": ["@babel/plugin-proposal-class-properties"]
    }
  5. 設定 React

    yarn add react react-dom react-hot-loader

    src/app.js

    import React from 'react'
    import { hot } from 'react-hot-loader'
    
    class App extends React.Component { 
      constructor (props) { 
        super(props) 
        this.state = {
          // 初始狀態
          hello: 'Hello, World'
        }
      }
    
      render () {
        return (
          <div>
            <h1>{this.state.hello}</h1>
            {/* render */}
          </div>
        )
      }
    } 
    export default hot(module)(App)

    src/index.js

    import React from 'react'
    import ReactDOM from 'react-dom'
    import App from './App.js'
    
    ReactDOM.render( 
      <App />,
      document.getElementById('root')
    )
@futianshen futianshen changed the title 搭建 React 基礎學習環境 搭建 React 開發環境 Dec 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant