Giter Club home page Giter Club logo

webpack-starter-basic's Introduction

Webpack Starter Basic Loo

webpack-starter-basic

forthebadgeforthebadge

dependencies

A simple webpack 4 starter project for your basic web development needs.

Read more on the demo website or continue reading below.

Table of Contents

Motivation

I needed to make a plain ol' "drop your mail to stay updated of ongoing developments" page.

I did not need anything fancy, no frontend framework, no unit testing, simply a starter project that would let me use sass, ES6, load assets, add vendor prefixes, start a dev server, generate sourcemaps and optimize everything for production.

I looked around and all I found were heavily specialized and complicated webpack starter projects (webpack-angular-starter, webpack-react-starter, etc) that are so intertwined with plugins that stripping undesired functionality is almost impossible.

So I did this.

Features

  • Separated development and production webpack settings you can understand
  • Sass
  • ES6
  • Asset loading
  • CSS Vendor prefixing
  • Development server
  • Sourcemaps
  • Favicons generation
  • Production optimizations
  • Mobile browser header color

Requirements

Usage

Substitute PROJECT-NAME for your project name.

Clone the repository

 git clone https://github.com/lifenautjoe/webpack-starter-basic PROJECT-NAME
 cd PROJECT-NAME

Install npm dependencies

 npm install 

Run the kickstart command

npm run kickstart

After the project has been kickstarted

To start the development server

npm start

To build for production

npm run build

To preview the production build

npm run preview

FAQ

When should I use this starter?

You should use this starter if any of the following are true:

  • You want to make a static page. e.g. splash screen, onboarding screen, phaser game, threejs visualization, countdown.
  • You found no good starter kit for whatever you want to do and need a solid place to start from.

Please note: If you are going to use a frontend framework like angular or react, you can of course add the required plugins and configuration but it's normally complicated and quirky enough that it's highly recommended to use one of the existing starter projects such as react-webpack-babel or for angular projects the angular-cli.

Where's the common webpack config?

There is none and that is good thing.

The pattern creates unnecessary confusion over the setup, at the end the config will always be different across environments. People just put booleans everywhere on the common config to switch between these differing configuration options which is just awful to see and confusing for someone who's just starting on webpack.

The only truly shared config between these files are the entry js point and the main html template.

How to load fonts

If you don't support Opera Mini, browsers support the .woff format. Its newer version .woff2, is widely supported by modern browsers and can be a good alternative.

If you decide to use only this format you can load the fonts in a similar manner to images.

In your webpack.dev.js and webpack.prod.js add the following

module.exports = {
    // ..
    module: {
        rules: [
            // ..
            {
                test: /\.woff$/,
                loader: 'url-loader',
                options: {
                    // Limit at 50k. Above that it emits separate files
                    limit: 50000,
                    // url-loader sets mimetype if it's passed.
                    // Without this it derives it from the file extension
                    mimetype: 'application/font-woff',
                    // Output below fonts directory
                    name: './fonts/[name].[ext]',
                },
            }
            // ..
        ]
    }
    // ..
};

And let's say your font is in the folder assets with the name pixel.woff

You can add it and use it in index.scss as

@font-face {
    font-family: "Pixel";
    src: url('./../assets/pixel.woff') format('woff');
}

.body{
    font-family: 'Pixel', sans-serif;
}

If you would like to support all kinds of font types, remove the woff rule we previously added to webpack.dev.js and webpack.prod.js and add the following

module.exports = {
    // ..
    module: {
        rules: [
            // ..
            {
                test: /\.(ttf|eot|woff|woff2)$/,
                loader: 'file-loader',
                options: {
                    name: 'fonts/[name].[ext]',
                },
            }
            // ..
        ]
    }
    // ..
};

And assuming you have your fonts in the directory assets with names pixel.woff, pixel.ttf, pixel.eot , etc.

You can add it and use it in index.scss as

@font-face {
    font-family: 'Pixel';
    src: url('./../assets/pixel.woff2') format('woff2'),
    url('./../assets/pixel.woff') format('woff'),
    url('./../assets/pixel.eot') format('embedded-opentype'),
    url('./../assets/pixel.ttf') format('truetype');
    /* Add other formats as you see fit */
}

How to load images

In JavaScript

You can require an image from JavaScript like

const myImage = require('./assets/icon.png');

If the image size in bytes is smaller than 8192you, myImage will be a string with the encoded image path such as

data:image/svg+xml;base64,bW9kdWxlLmV4cG9ydHMgPSBfX3dlYnBhY2tfcHVibGljX3BhdGhfXyArICJhc3NldHMvaW1hZ2VzL3RpY2stQ3lydkhSdi5zdmciOw==

If the image size is larger than 8192 it will be a string with the url to the image such as

src/assets/icon.png?hash=5b1f36bc41ab31f5b801

This limit is set so images like icons are not loaded through a request but you can force the loader to give you image urls always by doing the following but should not be necessary. The limit works 90% of the time.

const myImage = require('!!url!/assets/icon.png');

In index.html

If you would like to include an image on your index.html file, place the path of the image in a webpack require statement<%= require(imagePath) %>.

  <img class="splash-title__img"
                     src="<%= require('./src/assets/logo-on-dark-bg.png') %>"
                     alt="webpack logo"></a>

How to install Bootstrap 4

After the project has been kickstarted

Install bootstrap

npm install bootstrap@4 --save

Install bootstrap dependencies.

npm install popper.js --save
npm install jquery --save

Replace the project index.scss with

@import "~bootstrap/scss/bootstrap";

And replace the project index.js with

require('./styles/index.scss');

import PopperJs from 'popper.js';
import jquery from 'jquery';


jquery(()=>{
    console.log('Hello jQuery + bootstrap 4!');
});

To see it all come together, replace the index.html body tag with

<body>

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="https://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
                <div class="dropdown-menu" aria-labelledby="dropdown01">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>

<main role="main" class="container">

    <div class="starter-template">
        <h1>Bootstrap starter template</h1>
        <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
    </div>

</main><!-- /.container -->
</body>

Start the development server and voilà.

npm start

To build for production

npm run build

To preview the production build

npm run preview

⚠️ Please remember to remove the Google Analytics tag in the index.html file as soon as you make the template yours.

<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-101423651-2"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-101423651-2');
</script>

Websites using this starter kit on the wild

Have a website online built with this starter kit and would like to add it to the list? Open an issue!


Author Joel Hernandez

webpack-starter-basic's People

Contributors

adekoyejoakinhanmi avatar candidosales avatar christikaes avatar dependabot[bot] avatar flogruber avatar krabbit93 avatar lifenautjoe avatar nandofalcao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webpack-starter-basic's Issues

kickstart throws error 130

It's odd because it seems to work afterwards, but don't know if I'll find more issues later because of this. Here is the output of the 'npm kickstart' command

 npm run kickstart

> [email protected] kickstart /home/glantruan/_programming/personal/all-fred
> node kickstarter

? What's the name of your project? (kebab-cased) (awesome-project) AllFred
? What's the name of your project? (kebab-cased) AllFred
? Who's the author? (John Doe) Glantucan
? Who's the author? Glantucan
? What color would you like the mobile header to be? (https://bit.ly/1LX2mtq) (#ff4970) #ff4970
? What color would you like the mobile header to be? (https://bit.ly/1LX2mtq) #ff4970
Removing /docs directory
Updating package.json name
Updating package.json author
Removing package.json git repository
Removing package.json kickstart dependencies
Removing package.json kickstart script
Writing new package.json
Removing package-lock.json
Setting mobile header color to #ff4970
Setting page title to project name
Writing new index.html
Writing new webpack.prod.js
Removing kickstarter script
Removing .git directory
All done!
npm ERR! code ELIFECYCLE
npm ERR! errno 130
npm ERR! [email protected] kickstart: `node kickstarter`
npm ERR! Exit status 130
npm ERR!
npm ERR! Failed at the [email protected] kickstart script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/glantruan/.npm/_logs/2018-12-06T14_55_21_878Z-debug.log

And here is the log:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'run', 'kickstart' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prekickstart', 'kickstart', 'postkickstart' ]
5 info lifecycle [email protected]~prekickstart: [email protected]
6 info lifecycle [email protected]~kickstart: [email protected]
7 verbose lifecycle [email protected]~kickstart: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~kickstart: PATH: /usr/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/glantruan/_programming/personal/all-fred/node_modules/.bin:/home/glantruan/bin:/home/glantruan/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
9 verbose lifecycle [email protected]~kickstart: CWD: /home/glantruan/_programming/personal/all-fred
10 silly lifecycle [email protected]~kickstart: Args: [ '-c', 'node kickstarter' ]
11 silly lifecycle [email protected]~kickstart: Returned: code: 130  signal: null
12 info lifecycle [email protected]~kickstart: Failed to exec kickstart script
13 verbose stack Error: [email protected] kickstart: `node kickstarter`
13 verbose stack Exit status 130
13 verbose stack     at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:182:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:182:13)
13 verbose stack     at maybeClose (internal/child_process.js:962:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
14 verbose pkgid [email protected]
15 verbose cwd /home/glantruan/_programming/personal/all-fred
16 verbose Linux 4.15.0-38-generic
17 verbose argv "/usr/bin/node" "/usr/bin/npm" "run" "kickstart"
18 verbose node v10.14.1
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 130
22 error [email protected] kickstart: `node kickstarter`
22 error Exit status 130
23 error Failed at the [email protected] kickstart script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 130, true ]

Static assets and fonts

Hello there !

I used your webpack starter this week end for a small project. It's perfect and exactly what i was looking for 😻 However i was wondering if you you had planned to handle static assets (images) and web fonts ?

For exemple, in my small project, i created a small canvas with pixi.js and i use some images inside. What's the best way to use images ?

Thanks 👍

node fs missing?

As per: webpack-contrib/css-loader#447

Need to add

 node: {
      fs: 'empty'
    },

to webpack configs to get around


ERROR in ./node_modules/schema-utils/src/validateOptions.js
Module not found: Error: Can't resolve 'fs' in

service worker generation?

create-react-app has it, google's abandoned web starter kit has it.

i really like this starter kit so adding new features would be pretty neat. i really like how react can turn a basic web app into a PWA without doing anything.

btw i'd be nice to see webpack-dev-server host a server on the local network the same way create-react-app does. i just added my ip to the --host flag to webpack-dev-server in npm start but it would be nice to see this starter kit do it automatically out of the box.

Can't include the one HTML template inside another one.

Hello.

I am on the way of learning WebPack and use your example like a start point.
Could you help, please, with the issue?

I have such a file structure:

image

and want to include navigation.html inside header.html

I have the Index.html :

....
<!DOCTYPE html>
<html>
  <%= _.template(require('./../includes/layout/head.html').default)(data) %>
  <body data-spy="scroll" data-target="#subNavigation" data-offset="200">

    <%= _.template(require('./../includes/layout/header.html').default)(data) %>
    <%= _.template(require('./../includes/components/intro.html').default)(data) %>

    <div class="container">Index Page</div>

    <%= _.template(require('./../includes/layout/footer.html').default)(data) %>
    <script src="js/bundle.js"></script>
  </body>
</html>

and the header.html:

<header id="header">
  <img src="img/logo.svg" id="logo" />
  <%= _.template(require('./../includes/components/navigation.html').default)(data) %>
</header>

But as a result, I have an error:

Html Webpack Plugin:
  Error: Cannot find module './../includes/components/navigation.html'
  
  - module.js:11 require
    internal/module.js:11:18
  
  - lodash.templateSources[1]:9 eval
    lodash.templateSources[1]:9:22
  
  - index.html:108 
    D:/Projects/TestProject/markup/src/html/views/index.html:108:132
  
  - index.html:115 ./node_modules/html-webpack-plugin/lib/loader.js!./src/html/views/index.html.module.exports
    D:/Projects/TestProject/markup/src/html/views/index.html:115:3
  
  - index.js:284 Promise.resolve.then
    [markup]/[html-webpack-plugin]/index.js:284:18
  
  - next_tick.js:188 process._tickCallback
    internal/process/next_tick.js:188:7

It would be great if you can push me on the right way.

*-close classes were removed from build

got this weird issue, if a class ends with -close it is removed from the built .css

index.scss

.mfp-close {
    width: 100px;
    height: 100px;
}
.test-close {
    width: 100px;
    height: 100px;
}
.close {
    width: 100px;
    height: 100px;
}
.test {
    width: 100px;
    height: 100px;
}

generated styles.css

.close,.test{width:100px;height:100px}
/*# sourceMappingURL=styles.eae04a2f42f80581e776.css.map */

problem with babel-preset-flow

Following the kickstart instructions results in the below error.

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Couldn't find preset "flow" relative to directory "/Users/skelly"
    at /Users/skelly/dev/webpack-starter-basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (<anonymous>)
    at OptionManager.resolvePresets (/Users/skelly/dev/webpack-starter-basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/skelly/dev/webpack-starter-basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/skelly/dev/webpack-starter-basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/skelly/dev/webpack-starter-basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/skelly/dev/webpack-starter-basic/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/skelly/dev/webpack-starter-basic/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/skelly/dev/webpack-starter-basic/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/skelly/dev/webpack-starter-basic/node_modules/babel-loader/lib/index.js:50:20)
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js main[1]
C

Adding more pages

Hi @lifenautjoe !

Thank you for sharing the kit, it works great! Turns out the project I'm working on needs more than 1 page though. What would be a good way to add more pages to the app using this kit?

Thank you!

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.