Skip to content

Instantly share code, notes, and snippets.

@jugshaurya
Last active July 11, 2020 15:46
Show Gist options
  • Save jugshaurya/0ecf4c069479dd942591b93fcc6500dd to your computer and use it in GitHub Desktop.
Save jugshaurya/0ecf4c069479dd942591b93fcc6500dd to your computer and use it in GitHub Desktop.
Prettier-eslint-babel-webpack

Note: package-lock.json helps in installing exact versions in package json using npm ci Note: package.json installs lastest versions in package json using npm i

webpack

  • Entry

  • Output

    • Tells webpack WHAT (files) and Where to load for the browser; Compliments the Output property.
  • Loaders

    • webpack only understands JavaScript and JSON files. Loaders allow webpack to process other types of files and convert them into valid modules that can be consumed by your application and added to the dependency graph.
    • Tells webpack how to modify files before its added to dependency graph. (ex-ts-loader, convert ts files to js files)
    • Loaders are also javascript modules (functions) that takes the source file, and returns it in a [modified] state.
    • Tells Webpack HOW to interpret and translate files. Transformed on a per-file basis before adding to the dependency graph.
  • plugins

    • While loaders are used to transform certain types of modules, plugins can be leveraged to perform a wider range of tasks like bundle optimization, asset management and injection of environment variables.
  • yarn add -D babel-loader babel-polyfill clean-webpack-plugin compression-webpack-plugin copy-webpack-plugin css-loader dotenv-webpack html-loader html-webpack-plugin image-webpack-loader mini-css-extract-plugin node-sass sass-loader url-loader webpack webpack-cli webpack-dev-server eslint-loader

 
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const Dotenv = require('dotenv-webpack');

const APP_DIR = path.resolve(__dirname, 'app');
const BUILD_DIR = path.resolve(__dirname, 'dist');
const env = process.env.NODE_ENV || 'development';

module.exports = {
  entry: ['babel-polyfill', `${APP_DIR}/index.jsx`],
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js',
  },
  mode: env,
  devtool: env === 'development' ? 'source-map' : null,
  module: {
    rules: [
      {
        enforce: 'pre',
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
      },
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: { minimize: true },
          },
        ],
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
      {
        test: /\.scss$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: [
          {
            loader: 'image-webpack-loader',
            options: {
              bypassOnDebug: true,
            },
          },
          'url-loader?limit=10000',
        ],
      },
      { test: /\.(woff|woff2|eot|ttf)$/, loader: 'url-loader?limit=100000' },
    ],
  },
  performance: {
    hints: process.env.NODE_ENV === 'production' ? 'warning' : false,
  },
  plugins: [
    new Dotenv(),
    new CleanWebpackPlugin([BUILD_DIR]),
    new CopyWebpackPlugin([{ from: 'public', to: './public' }]),
    new HtmlWebPackPlugin({
      template: `${APP_DIR}/index.html`,
      filename: './index.html',
    }),
    new MiniCssExtractPlugin({
      filename: 'bundle.css',
    }),
    new CompressionPlugin({
      algorithm: 'gzip',
      test: /\.jsx?$|\.s?css$/,
      minRatio: 0.8,
    }),
  ],
  devServer: {
    contentBase: BUILD_DIR,
    compress: true,
    port: 3000,
    historyApiFallback: true,
  },
  resolve: {
    extensions: ['.js', '.jsx', '.json'],
  },
};

prettier

  • yarn add -D prettier.
  • ctrl,(vscode) : require config and check it like:
  • Prettier: Require Config
  • touch .prettierrc
{
  "jsxBracketSameLine": true,
  "singleQuote": true,
  "trailingComma": "all",
  "printWidth": 80,
}

Base-eslint for vanilla js

{
  "extends": [
    "eslint:recommended",
    "prettier",
    "prettier/react"
  ],
  "plugins": [],
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true
  }
}

eslint with react as well

  • yarn add -D eslint eslint-config-prettier babel-eslint eslint-plugin-import eslint-plugin-react eslint-plugin-jsx-a11y eslint-plugin-react-hooks
  • touch .eslintrc.json file
{
  "extends": [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:react/recommended",
    "plugin:jsx-a11y/recommended",
    "prettier",
    "prettier/react"
  ],
  "rules": {
    "react/prop-types": 0,
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn",
    "no-console": "warn"
  },
  "parser": "babel-eslint",
  "plugins": ["react", "import", "jsx-a11y", "react-hooks"],
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

babel

  • touch .babelrc
  • yarn add -D @babel/cli @babel/core @babel/plugin-proposal-class-properties @babel/plugin-proposal-object-rest-spread @babel/preset-env @babel/preset-react
{
  "presets": ["@babel/preset-react", "@babel/preset-env"],
  "plugins": ["@babel/plugin-proposal-class-properties", "@babel/plugin-proposal-object-rest-spread"]
}

package.json scripts

"format": "prettier --write \"src/**/*.{js,jsx}\"",
"lint": "eslint \"src/**/*.{js,jsx}\" --quiet",

.gitignore

node_modules/
.DS_Store/
.cache/
dist/
build/
coverage/
.vscode
.env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment