Skip to content

Instantly share code, notes, and snippets.

@danevans
Last active May 22, 2018 01:01
Show Gist options
  • Save danevans/759d8c8c4d340a9a034716a7c9a53fe0 to your computer and use it in GitHub Desktop.
Save danevans/759d8c8c4d340a9a034716a7c9a53fe0 to your computer and use it in GitHub Desktop.
A simple setup for a JavaScript project that doesn't use a web app framework

Getting up and running in JavaScript

Prerequisites

  1. Install yarn
  2. Install node

Steps

  1. yarn init
    • Use yarn init -y to skip all the questions and use defaults.
  2. yarn add webpack webpack-cli webpack-dev-server --dev
    • Webpack is the workhorse that bundles all the assets from source to output.
    • Since version 4 of webpack webpack-cli is required.
    • The dev server is not necessary but is the easiest way to serve and hot reload pages locally.
  3. yarn add "babel-loader@^8.0.0-beta" @babel/core @babel/preset-env --dev
    • Babel loader teaches webpack how to use babel to transform ES6 to ES5.
    • Babel core does the transformations.
    • Preset env tells it what transformations to perform.
  4. Add the webpack.config.js
  5. Update the package.json to include scripts
  6. Create index.html
  7. Create index.js
  8. yarn run server

Optional

  1. yarn add lit-html
  2. yarn add lit-router
  3. https://github.com/FormidableLabs/webpack-dashboard
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Example</title>
<script src="/bundle.js"></script>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
// Import dependencies or other source files like this:
// import { html, render } from 'lit-html';
console.log('hello world');
"scripts": {
"build": "webpack --progress -p",
"watch": "webpack --progress --watch",
"server": "webpack-dev-server --open"
}
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
// This serves the same index from subroutes using the dev server if you plan
// to use window.history as a single page app.
devServer: {
historyApiFallback: true
},
mode: 'development',
};
@danevans
Copy link
Author

If I exclude node_modules from babel then it doesn't run on dependencies which can cause uglify to have an error if they use newer syntax. Removing the exclude line fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment