Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active March 28, 2018 03:42
Show Gist options
  • Save xgqfrms-GitHub/5862f5885acd10a956a72b14ee1c5f96 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/5862f5885acd10a956a72b14ee1c5f96 to your computer and use it in GitHub Desktop.
Setup a React Environment (React + Bable + Webpack 2 + Sass + Gulp + css module + ES6)

Setup a React Environment

(React + Bable + Webpack 2 + Sass + Gulp + css module + ES6)

(React + Bable + Webpack 2 + Sass + Gulp + css module + TypeScript)

https://scotch.io/tutorials/setup-a-react-environment-using-webpack-and-babel

https://github.com/scotch-io/hello-world-react

https://github.com/joykare/react-startpack

设置一个React环境

$ mkdir hello-world-react & cd hello-world-react 
$ yarn init
$ touch webpack.config.js

webpack.config.js

https://webpack.github.io/docs/list-of-loaders.html

const path = require('path');
module.exports = {
  entry: './client/index.js',
  output: {
    path: path.resolve('dist'),
    filename: 'index_bundle.js'
  },
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ }
    ]
  }
}

.babelrc

$ yarn add babel-loader babel-core babel-preset-es2015 babel-preset-react --dev

touch .babelrc
{
    "presets":[
        "es2015", "react"
    ]
}
$ mkdir client & cd client & touch index.js index.html & cd .. 

index.js

/*
    ./client/index.js
    which is the webpack entry file
*/
console.log('Hey guys and ladies!!')
/*
    ./client/index.html
    basic html skeleton
*/
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>React App Setup</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

html-webpack-plugin

$ yarn add html-webpack-plugin

webpack.config.js

const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './client/index.html',
  filename: 'index.html',
  inject: 'body'
})

module.exports = {

// ...

module: {
    loaders: [
        //...
    ]
},
// add this line
plugins: [HtmlWebpackPluginConfig]
}

package.json

{
  "name": "hello-world-react",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",

  // add the scripts key to your package.json

  "scripts": {
    "start": "webpack-dev-server"
  },

  "dependencies": {
  ...
  },
  "devDependencies": {
  ...
  }
}

run

$ yarn start

http://localhost:8080/

$ yarn add react react-dom

$ cd client & mkdir components & cd components & touch App.jsx & cd ../..
*
    ./client/components/App.jsx
*/
import React from 'react';

export default class App extends React.Component {
  render() {
    return (
     <div style={{textAlign: 'center'}}>
        <h1>Hello World</h1>
      </div>);
  }
}
/* 
    ./client/index.js
*/
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';

ReactDOM.render(<App />, document.getElementById('root'));
$ yarn start

Simple React Webpack Babel Starter Kit

https://github.com/alicoding/react-webpack-babel

https://github.com/alicoding/react-webpack-babel/blob/master/package.json

Setting up environment for React, SASS, ES2015, Babel with Webpack

https://medium.com/@srinisoundar/setting-up-environment-for-react-sass-es2015-babel-with-webpack-2f77445129#.n006prfll

How-to setup Webpack on an ES6 React Application with SASS?

https://www.jonathan-petitcolas.com/2015/05/15/howto-setup-webpack-on-es6-react-application-with-sass.html

Adding Sass or Scss to Create-React-App

https://medium.com/@Connorelsea/using-sass-with-create-react-app-7125d6913760#.hd6uu7ivo

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Mar 12, 2017

《 Pro React Book》 - Webpack for React

http://www.pro-react.com/materials/
https://github.com/pro-react/
https://github.com/pro-react/react-app-advanced-boilerplate
https://facebook.github.io/react/blog/2016/07/22/create-apps-with-no-configuration.html
https://blog.risingstack.com/using-react-with-webpack-tutorial/

http://www.pro-react.com/materials/appendixA.pdf
http://www.pro-react.com/materials/appendixB.pdf
http://www.pro-react.com/materials/ch06-alt-redux.pdf

ReactCasts

https://www.youtube.com/watch?v=LTunyI2Oyzw

http://www.pro-react.com/materials/appendixA/

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: __dirname + "/app/main.js",
  output: {
    path: __dirname + "/build",
    filename: "[name]-[hash].js"
  },

  module: {
    loaders: [
      {
        test: /\.json$/,
        loader: "json"
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel'
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style', 'css?modules!postcss')
      }
    ]
  },
  postcss: [
    require('autoprefixer')
  ],

  plugins: [
    new HtmlWebpackPlugin({
      template: __dirname + "/app/index.tmpl.html"
    }),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin(),
    new ExtractTextPlugin("[name]-[hash].css")
  ]
}

@xgqfrms-GitHub
Copy link
Author

TypeScript & React & Webpack

https://www.typescriptlang.org/docs/handbook/react-&-webpack.html

> mkdir proj & cd proj
> mkdir src & cd src & mkdir components & cd ..

$ npm init
$ npm install -g webpack
$ npm install --save react react-dom @types/react @types/react-dom
$ npm install --save-dev typescript awesome-typescript-loader source-map-loader

https://medium.com/@fay_jai/getting-started-with-reactjs-typescript-and-webpack-95dcaa0ed33c#.81yfhgvb0
http://todomvc.com/examples/typescript-react/#/
http://blog.wolksoftware.com/working-with-react-and-typescript
https://jaysoo.ca/2015/09/26/typed-react-and-redux/

@xgqfrms-GitHub
Copy link
Author

Gulp Sass

https://www.imooc.com/video/5692

https://www.youtube.com/watch?v=wNlEK8qrb0M

https://github.com/jikeytang/use-gulp

'use strict';

var gulp = require('gulp');
var sass = require('gulp-sass');

gulp.task('sass', function () {
  return gulp.src('./sass/**/*.scss')
    .pipe(sass.sync().on('error', sass.logError))
    .pipe(gulp.dest('./css'));
});

gulp.task('sass:watch', function () {
  gulp.watch('./sass/**/*.scss', ['sass']);
});
var sourcemaps = require('gulp-sourcemaps');

gulp.task('sass', function () {
 return gulp.src('./sass/**/*.scss')
  .pipe(sourcemaps.init())
  .pipe(sass().on('error', sass.logError))
  .pipe(sourcemaps.write())
  .pipe(gulp.dest('./css'));
});


var sourcemaps = require('gulp-sourcemaps');
gulp.task('sass', function () {
 return gulp.src('./sass/**/*.scss')
  .pipe(sourcemaps.init())
  .pipe(sass().on('error', sass.logError))
  .pipe(sourcemaps.write('./maps'))
  .pipe(gulp.dest('./css'));
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment