Code Monkey home page Code Monkey logo

js-pong's Introduction

js-Pong

A pong game with javascript.

Uses ES6 with babel and webpack, to get a cleaner code.

How to install ES6 with babel and webpack

Prerequisites

  • Install nodejs
  • Install npm

File structure

  • Create an 'index.html' file at root
  • Create a 'app' folder at root, where will go your javascript sources
  • Create a 'dist' folder at root, where webpack will bundle your compiled files
  • Create a 'index.js' in the app folder
  • Create a '.babelrc' file at root
  • Create a 'webpack.config.js' file at root
  • add 'node_modules/' to your .gitignore

Install webpack and webpack dev server locally

npm install webpack webpack-dev-server --save-dev

Install babel locally

npm install --save-dev babel-loader babel-core

Install babel preset environment

npm install babel-preset-es2015 --save-dev

Paste this in your package.json, under version

 "scripts": {
   "start": "./node_modules/.bin/webpack -p",
   "dev": "./node_modules/.bin/webpack-dev-server --inline --hot"
 },

Paste this in your .babelrc file

{
  "presets": ["es2015"]
}

Add this in your webpack.config.js file

module.exports = {
  entry: "./app/index.js",
  output: {
    filename: "./dist/bundle.js"
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      }
    ]
  },
}

Usage

Add this in the body of your html file

    <script src="dist/bundle.js"></script>

Declare an exported class in a js file (in the app folder)

export default class Ball {
    // code
}

Import it into an other js file without '.js' (path must be consistent), and use it

import Ball from './ball';

// ...

    ball = new Ball(10, 75, 75);

Run babel and webpack thanks to npm in terminal to compile your js. This will export a minified bundle.js in dist folder, where your html page will use it.

npm run start

If you want to dynamically reload your change while developping, use webpack-dev-server via npm

npm run dev

Your app will run at http://localhost:8080/

js-pong's People

Contributors

gaetz avatar

Watchers

James Cloos avatar  avatar

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.