I learned webpack for a while and finally got some results. Although I looked up a lot of things, I also went through a lot of pits. Personally, REactJS + ES6 projects are suitable for Webpack and multi-page mobile projects; I’m not going to go over the concept here, but you can find a lot of things online

File directory

  • The root directory
    • App // Source file
    • css
    • js
    • index.html
    • Index.js // entry file
    • package.json
    • Webpack.config.js // configuration file
    • Readme.md // Documentation

Installation package dependency

{" name ":" webpack - HTML 5 ", "version" : "1.0.0", "description" : ""," scripts ": {" watch" : "Webpack --progress --colors --watch" NPM run watch}, "author": "azq", "license": "ISC", "devDependencies": {"autoprefixer-loader": "^3.2.0",// "^ 6.14.0", / / compile js "Babel - loader" : "^" 6.2.5, / / compile js "Babel - preset - es2015" : "^ 6.14.0", / / compile es6 to es5 "Babel - preset - react" : "^ 6.11.1", / / compile JSX "clean - webpack - plugin" : "^ 0.1.10", / / "CSS - loader" empty file: "^0.23.1",// compress CSS "extract-text-webpack-plugin": "^1.0.1",// This should be the "file-loader" that does not pack CSS: "^" 0.8.5, / / handle file "HTML - loader" : "^ 0.4.3", / / handle HTML "HTML - webpack - plugin" : "^ 2.9.0", / / HTML templates "jquery" : "^ 3.1.1 track", "less" : / / jquery module "^ server", / / less core "less - loader" : "^ 2.2.2", / / compile less "postcss - px2rem" : "^ 0.3.0", / / px conversion rem "react" : "^ 15.3.2", / / the react core "react - dom" : "^ 15.3.2", / / the react - dom core "style - loader" : "^ 0.13.0", "url - loader" : / / loader "^ 0.5.7", / / a similar file - loader, you can set the data file size conversion url "webpack" : "^ 1.12.13", "webpack - dev - server" : "^ 1.16.1}}"Copy the code

Webpack configuration

const path = require('path'); const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const CleanPlugin = require('clean-webpack-plugin'); const ROOT_PATH = path.resolve(__dirname); // root directory const APP_PATH = path.resolve(ROOT_PATH, 'app'); // Source file directory const BUILD_PATH = path.resolve(ROOT_PATH, 'build'); Const ExtractTextPlugin = require("extract-text-webpack-plugin"); const px2rem = require('postcss-px2rem'); Module. exports = {entry: {// entry, default index.js app:APP_PATH, vendor: ['jquery','react','react-dom']// output: {// output configuration path: BUILD_PATH, publicPath: "./", filename: 'bundle.js'}, plugins: [new CleanPlugin (' build '), / / empty bulid new webpack. Optimize the UglifyJsPlugin ({compressor: {warnings: false,}, except: ['$super', 'Copy the code

, ‘exports’,’ require ‘] / / remove keyword}), new webpack. Optimize the OccurenceOrderPlugin (), new HtmlWebpackPlugin ({template: APP_PATH+’\\index.html’, inject: ‘true’ }), new ExtractTextPlugin(“styles.css”), new webpack.DefinePlugin({ ‘process.env’:{ ‘NODE_ENV’: Json.stringify (‘production’)}}), new webpack.providePlugin ({// provide global $: “jquery”, jquery: “jquery”, “window.jQuery”:”jquery”, React:”react”, ReactDom:”react-dom” }), New webpack.optimize.Com monsChunkPlugin (‘ vendor ‘, ‘vendor. Bundle. Js), / / vendor packaging] from the entrance, postcss: () => { return [px2rem({remUnit: 64})]; Px2rem}, module: {loaders: [{// compress and px2rem test: /\.css$/, loader: ExtractTextPlugin. Extract (‘ style! CSS! Postcss ‘)}, {/ / less compile and add prefix test: / \. Less /, loader: ExtractTextPlugin. Extract (‘ CSS! Autoprefixer! Less ‘)}, {/ / JSX compile test: / \. JSX? $/, exclude: /(node_modules|bower_components)/, loader: ‘babel’, query: { presets: [‘react’, ‘es2015′]},}, {attrs=img: SRC;} Attrs =img: SRC img:data-src SRC = test: /\.html$/, loader: “HTML?-minimize&attrs=img: SRC img:data-src”}, {// /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: // fonts/[name].[ext]’}, { Will be less than the image of xxxbyte into base64 code test: / \. (PNG | JPG | GIF) $/, loader: ‘url – loader? Limit = 8192 & name =. / img / [name] [ext]’}, {test: /\.json$/, loader: ‘file-loader?name=./[name].[ext]’ }] } }

Ps: Notes may be inaccurate, I hope to inquire about it

Entrance to the file

Es6 syntax is used here, as well as in the test demo. If you do not use ES6, you can remove the relevant ES6 compiled code, and then how to use the code in the require reference

import './css/base.less'; import './css/index.less'; import './js/comments.json'; Import './js/ productbox.jsx ';Copy the code

Other documents

Productbox.jsx // This is a code I saw while learning ReactJS online and made some changes

class Comment extends React.Component {
    render() {
        return (
            
      
{this.props.children}
- {this.props.author}
); } } class CommentForm extends React.Component { handleSubmit(e) { e.preventDefault(); const author = this.refs.author.value.trim(); const body = this.refs.body.value.trim(); const form = this.refs.form; this.props.onSubmit({author: author, body: body}); form.reset(); } render() { return ( {this.handleSubmit(e)}}> ); } } class CommentList extends React.Component { render() { var commentsNode = this.props.comments.map((comment, index) => { return {comment.body} }); return (
{commentsNode}
); } } class CommentBox extends React.Component { constructor(props) { super(); this.state = { comments: props.comments || [] }; } loadDataFromServer() { $.ajax({ url: this.props.url, dataType: "json", success: comments => { this.setState({comments: comments}); }, error: (xhr, status, err) => { console.log(err.toString()); }}); } componentDidMount() { this.loadDataFromServer(); } handleNewComment(comment) { const comments = this.state.comments; const newComments = comments.concat([comment]); this.setState({comments: newComments}); setTimeout(() => { $.ajax({ url: this.props.url, dataType: "json", type: "POST", data: comment, success: comments => { this.setState({comments: comments}); }, error: (xhr, status, err) => { console.log(err.toString()); this.setState({comments: comments}); }}); }, 2000); } render() { return (

Comments

this.handleNewComment(comment)}/>
); }} let box = ReactDom. Render (// ReactDom = ReactDom) Document.getelementbyid ('content'));Copy the code

Point in time

demand

  • [x] CSS pX2REM, and compressed CSS
  • [x] JS compression obfuscation, modular
  • [x] Automatically adds js, CSS references to HTML
  • [x] Base64 for automatic conversion less than 8K

9.6 requirements

  • [] Image data is packaged into a JS file
  • [x] Replace image path in JS CSS HTML

9.7 requirements

  • [] Generate more than 8K image data with audio list combined with preloading

9.20

  • [x] CSS attributes are automatically prefixed

9.20

  • [x] Modify code to compile automaticallynpm run watch

9.28

  • [x] es6 compilation
  • [x] react to compile
  • [X] Third-party libraries packaged together

This. Refs. Author. Getdomnode () fails, the React v0.14 can directly this. Refs. The author obtain the dom

Some did not realize, they did not carefully think, the first online can be found in front of the implementation, the follow-up will continue to update

Other associated

The public,

Our home page