Open futianshen opened 5 years ago
git init vim .gitignore
git init
vim .gitignore
yarn init
yarn add webpack webpack-cli html-webpack-plugin webpack-dev-server
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 }) ] }
npx webpack
yarn run start:hot
"scripts": { "start:hot":"webpack-dev-server --mode development --open --hot", }
yarn run start
"scripts": { "start": "webpack --mode development" }
yarn run build
"scripts": { "build": "webpack --mode production" }
yarn add @babel/core @babel/preset-env @babel/preset-react @babel/plugin-proposal-class-properties babel-loader
{ "presets": ["@babel/preset-env", "@babel/preset-react"], /* */ "plugins": ["@babel/plugin-proposal-class-properties"] }
yarn add react react-dom react-hot-loader
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)
import React from 'react' import ReactDOM from 'react-dom' import App from './App.js' ReactDOM.render( <App />, document.getElementById('root') )
Git 初始化
git init
vim .gitignore
Yarn 初始化
yarn init
設定 Webpack
yarn add webpack webpack-cli html-webpack-plugin webpack-dev-server
webpack.config.js(webpack 設定檔)
使用 Webpack 的方法
npx webpack
yarn run start:hot
package.json
yarn run start
package.json
yarn run build
package.json
設定 Babel
yarn add @babel/core @babel/preset-env @babel/preset-react @babel/plugin-proposal-class-properties babel-loader
.babelrc (告訴 babel 有那些格式要轉換)
設定 React
yarn add react react-dom react-hot-loader
src/app.js
src/index.js