Giter VIP home page Giter VIP logo

cwc-udemy's Introduction

Custom web components demo

This is webpack 4 starter testing plain ES6 web components.

NPM installation scripts

Just run npm install and all libs mentioned below will be installed.

  # 1. install webpack modules
  npm i -D webpack webpack-cli webpack-dev-server webpack-bundle-analyzer
  # 2. install other webpack util plugins
  npm i -D html-webpack-plugin url-loader file-loader copy-webpack-plugin uglifyjs-webpack-plugin clean-webpack-plugin
  # 3. install babel loaders - basics v7
  npm i -D babel-loader @babel/core @babel/preset-env @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators
  # 4. ESLint for react and styled components
  npm i -D eslint babel-eslint eslint-plugin-react
  # 5. install prettier for
  npm i -D prettier eslint-plugin-prettier
  # (optional) babel polyfills (basic), fetch polyfil for ie11
  npm i -s @babel/polyfill whatwg-fetch
  # (optional) testing with jest & enzyme incl. eslint plugin
  npm i -D jest eslint-plugin-jest enzyme enzyme-adapter-react-16

NPM scripts

  • npm start: start webpack in watch mode passing dev environment (--env=dev).
  • npm run dev: start webpack-dev-server using dev environment (--env=dev)
  • npm run build:dev: Build development version.
  • npm run build: Build production version to dist folder. Note that previous build will be removed first.

Webpack config scripts

Webpack configuration is stored in webpack folder. Initially 2 separate def files are created. Based on --env parameter passed on init one of the configuration files is loaded as webpack config module (see webpack.config.js)

  • dev: this is development environment setup (webpack/dev.js)
  • prod: this is production build setup (webpack/prod.js).

Note! Configuration is not 'shared' between files. When updating, please adjust/update the properties in both files where needed.

BTW: I have chosen here simpler approach with some code duplication above more complex setup (without code duplication).

Folder Structure

  • assets: all asset files used like original logo and other images etc. Excel template for creating json files is also here.
  • dist: builds are here
  • src: holds all react code to be processed by webpack. All js/css files need to be inside src folder, otherwise Webpack won’t see them.
    • component: holds shared/general react components used by multiple pages
    • layout: holds react components which define global page layout
    • page: holds react components that represent pages (uniek in design/composition)
    • router: holds defined routes, react router setup and main router component. This router component is then integrated into main/global layout component.
    • store: holds all redux files used to setup redux store including implementation of custom middleware functions.
    • styles: holds css (js possible too) files related to defining global styles, overrides, css variables etc.
    • utils: holds uitility function
  • static: static files that will be included in the build
  • webpack: webpack configuration file from react-scipts

Git branches

  • master: complete portions (should be) ready for use
  • dev: development branch

Web components cheetsheet

Custom element lifecycle methods

/**
 * Toolip custom element
 * v0.0.1 Jan 2019
 */
class dv4allTooltip extends HTMLElement {
  constructor() {
    super();
    console.log("Tooltip...constructor");
  }
  /**
   * Lifecycle method fired after custom
   * element is attached to DOM
   */
  connectedCallback() {
    console.log("Tooltip...connectedCallback");
  }
  /**
   * Define which attributes of custom element
   * should be monitored for changes
   */
  static get observedAttributes() {
    return ["list", "of-attributes", "to-monitor"];
  }
  /**
   * Lifecycle method fired on attribute change
   * it requires static get method to define
   * which attribute(s) should be monitored
   */
  attributeChangedCallback(name, oldVal, newVal) {
    console.log("Tooltip...attributeChangedCallback");
  }
  /**
   * Lifcycle method fired after custom
   * element is unmounted from the DOM
   */
  disconnectedCallback() {
    console.log("Tooltip...disconnectedCallback");
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.