Code Monkey home page Code Monkey logo

lab-error-clean-up4's Introduction

Webpack Intro

See Live Demo of this Template

Webpack is a task runner and a module bundler. It originally started as a module bundler. This means that it takes all of your separate Javascript modules and bundles them together into a single file. Webpack also automates some of the tasks that we have to run every time we change the code. It will automate these tasks so that we are not typing in the same commands every single time.

Topics


Get Started

Use Template

1. To get started, click the GREEN "Use this Template" button at the top of the repo

Use this Template

2. Make sure YOUR github account is selected in the dropdown and name your project

Create Project

  1. Clone your new repo to your local machine
  2. Go to the NEXT section

Starting the Project

  1. Open the package.json file and change the name property to the name of your application.
  2. Rename the .sample.env file to .env file. The final file name should be .env
  3. From your command line, be in the root directory and run npm install OR npm i for short.
  4. To start your application, run npm start

NOTES:

  • Changes you make to the project will make the browser reload on save...no more hard refresh unless something goes wrong.
  • You will no longer be using the hs -o command. To start your server, you will run npm start

Other Important Tidbits

Console messages

From this time forward, you will be expected to have a clean console in order for your assignments to be approved. This means that the use of console.log is acceptable (debugger is WAY better though) while developing, but will throw an error in your console like the image below, but all logs will have to be removed. You may use console.error and console.warn in your code however for messages. These need to all be removed before pushing to production unless they contain vital info for the user/developer.

not acceptable

Including Images with Webpack

If you have a folder of local images that you want to load into your code things get a little strange with webpack. Remember the only way webpack knows about assets is if they are imported into your javascript files. Even our CSS is not added until those files are imported into our javascript files. Below is some sample code for how to load a local image file into your project

import cat from './assets/cat.jpg';

let domString = `<img src=${cat} alt="picture of a cat"/>`;

document.getElementById('cat').innerHTMl = domString;

Importing CSS/SCSS

NOTE: We will be using SCSS files. They are used the same way your CSS files work, with some added benefits that we will discuss later.

Since Webpack is making the connection to the JS and CSS for us and we are no longer manually adding links or script tags to our HTML, we have to get our styles to the application some way...

Here is how we add our styles using webpack:

import '../styles/main.scss';

const init = () => {
  document.querySelector('#app').innerHTML = '<h1>HELLO! You are up and running!</h1>');
  console.log('YOU ARE UP AND RUNNING!');
};

init();

Using Axios

For every file you will need to make an XHR request in, you will need to require Axios

import axios from 'axios';

const examplePromise = () => {
  axios.get('http://localhost:3001/example')
    .then((data) => {
      console.warn(data);
    })
    .catch((error) => {
      console.error(error);
    });
});

Deploying on Netlify

  • Build Command: npm run build

  • Publish directory: dist

  • Add Environmental Variables (NOT REQUIRED for Apps that do not use API Keys, etc)

    • Any Enviromental variables you are using in your .env file should be added to Netlify.
      • Go to Site settings > Build & deploy > Environment > Environment variables and the keys and values there.
  • Update Firebase URL Settings

    • In Firebase under Authentication select sign in methods, scroll to Authorized domains. Add your Netlify URL.

More Info and Resources on Webpack

lab-error-clean-up4's People

Watchers

 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.