Giter Site home page Giter Site logo

webpack_react's Introduction

cd Desktop
mkdir react-webpack
cd react-webpack
code .

#init and install webpack
yarn init -y
yarn add webpack webpack-cli webpack-dev-server -D

touch webpack.config.js

in webpack.config.js

const path = require('path')

module.exports = {
    entry:{
        app:path.join(__dirname, './src/index.js')
    },
    output:{
        filename: 'boundle.js',
        path: path.join(__dirname, 'dist')
    }
}

create src/index.html, src/index.js

in index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root"></div>
</body>
</html>

in src/index.js

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App.js'

ReactDOM.render(<App />, document.getElementById('root'))

yarn add react react-dom -D

create src/app.js

import React from 'react'

export default class App extends React.Component {
    render(){
        return(
            <div>
            <h1>Hello React!!</h1>
        </div>
        )
        
    }
}

add scripts to the package.json

the package.json looks like this

{
  "name": "react-webpack",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "build": "webpack"
  },
  "devDependencies": {
    "webpack": "^5.9.0",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.0"
  }
}

run yarn build throw error, because no loader installed

install loader

yarn add @babel/core @babel/preset-env @babel/preset-react -D

in webpack.config.js add

module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                use: ['babel-loader']
            },
        ]
    }

create src/.babelrc

{
    "presets":["@babel/preset-env","@babel/preset-react"]
}

yarn add babel-loader -D

we got everything we need to run npm run build

should work

to exclude node_modules

module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: path.join(__dirname, 'node_modules'),
                use: ['babel-loader']
            },
        ]
    }
mode: 'development',

faster!

we also want to boundle index.html

yarn add html-webpack-plugin

const HtmlPlugin = require('html-webpack-plugin')
plugin: [
        new HtmlPlugin({
            filename: 'index.html',
            template: path.join(__dirname, 'src/index.html')
        })
    ]

webpack_react's People

Contributors

ziqiangy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.