1webpack簡介
創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)大箐山,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
webpack是目前最流行的打包工具之一,是一個(gè)為前端模塊打包構(gòu)建而生的工具,它既吸取了大量已有的方案優(yōu)點(diǎn)和教訓(xùn),也解決了很多前端開發(fā)過程中已有的痛點(diǎn),如代碼的拆分與異步加載,對(duì)非js資源的支持,強(qiáng)大的loader設(shè)計(jì)使得它更像是一個(gè)構(gòu)建平臺(tái),而不是打包工具。
2webpack,react開發(fā)環(huán)境
1)新建一個(gè)項(xiàng)目文件夾,進(jìn)入此文件夾,創(chuàng)建package.json文件(npm init命令)
2)確認(rèn)已安裝webpack,web pack-dev-server模塊
3)安裝babel(babel不懂的請(qǐng)先看https://www.jb51.net/article/142362.htm)babel的目的有兩個(gè)(將es6轉(zhuǎn)為es5,轉(zhuǎn)換jsx語法),
4) 安裝babel-preset-es2015和babel-preset-react
5)安裝babel-loader和 babel-core
6)babel工作需要.babelrc文件
{ "presets":["react","es2015"] }
7)最重要的webpack.config.js文件的配置
var path = require('path'); var webpack = require('webpack'); var HtmlwebpackPlugin = require('html-webpack-plugin'); var ROOT_PATH = path.resolve(__dirname); var BUILD_PATH = path.resolve(ROOT_PATH, 'build'); var APP_PATH = path.resolve(ROOT_PATH, 'app'); module.exports = { entry: { app: path.resolve(APP_PATH, 'app.js') }, output: { path: BUILD_PATH, filename: '[name].bundle.js' }, resolve:{ extensions:['','.js','.jsx'], root:APP_PATH }, module:{ loaders:[ { test: /\.js?$/, loaders: ['babel-loader'], include: APP_PATH } ] }, plugins:[ new HtmlwebpackPlugin({ title: 'demo', filename:'index.html' }), ] }
這個(gè)應(yīng)該是最簡單的配置了,不懂的可以網(wǎng)上搜,其中用到HtmlwebpackPlugin
,
這個(gè)插件是幫我們自動(dòng)生成html頁面,并自動(dòng)引入打包后的.js文件,其中的參數(shù)意思是,title這個(gè)頁面的title,filename生成的頁面名字
8)最后的項(xiàng)目文件目錄為
app是項(xiàng)目組件文件夾,test是測(cè)試文件
list.js代碼
import React from 'react'; import uuid from 'uuid'; import ListItem from '../listItem/listItem'; class List extends React.Component { constructor(props) { super(props); this.state = { items: [] } } handleclick() { const value = this.refs.addlist.value; const str = { id: uuid.v1(), text: value } let items = this.state.items; items = [...items, str];//合并數(shù)值 this.setState({ items }) } //刪除其中的 deleteItem(id) { this.setState({ items: this.state.items.filter( result => result.id !== id ), }); } render() { const ListIt = this.state.items.map(item => { return (); }); return ( ); } } export default List;{ListIt}
listitem.js
import React from 'react'; class ListItem extends React.Component { render() { const text = this.props.name; return (
app.js文件
import React from 'react'; import ReactDOM from 'react-dom'; import List from './list/list'; const app = document.createElement('div'); document.body.appendChild(app); ReactDOM.render(, app);
9)最后的package.json文件
{ "name": "csdndemo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "dev": "webpack-dev-server --hot" }, "author": "", "license": "ISC", "devDependencies": { "babel-core": "^6.24.0", "babel-loader": "^6.4.1", "babel-preset-es2015": "^6.24.0", "babel-preset-react": "^6.23.0", "html-webpack-plugin": "^2.28.0", "react": "^15.4.2", "react-dom": "^15.4.2", "webpack": "^1.14.0", "webpack-dev-server": "^1.14.1" } }
運(yùn)行項(xiàng)目nam run dev
10)后續(xù)會(huì)有基于這個(gè)demo的測(cè)試enzyme庫的使用,代碼檢測(cè)aslant,flux的使用,redux的使用
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。