Skip to content

Instantly share code, notes, and snippets.

@heaversm
Last active October 16, 2019 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heaversm/852674900ce5c1b46d5f3c0e4028e097 to your computer and use it in GitHub Desktop.
Save heaversm/852674900ce5c1b46d5f3c0e4028e097 to your computer and use it in GitHub Desktop.
Common JS Workflows

COMMON JS WORKFLOWS

Use an ES6 Module

With Unpkg

Add type="module" to script tags:

<script type="module">
  //then make sure to use the param module in unpkg:
  import _ from 'https://unpkg.com/lodash-es?module'
  //or...
  import cloneDeep from 'https://unpkg.com/lodash-es/cloneDeep?module'
</script>

with browser-sync

Import with module param:

import { html, LitElement } from 'https://unpkg.com/@polymer/lit-element@latest/lit-element.js?module';

and then use browser-sync:

browser-sync src -f src --cors --no-notify

with webpack

Install webpack and all its garbage: npm install --save-dev webpack webpack-cli copy-webpack-plugin webpack-dev-server

Create a really confusing webpack config (webpack.config.js):

const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
    entry: './src/app.js',
    mode: 'development',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.js'
    },
    devServer: {
        contentBase: './dist'
    },
    plugins: [
        new CopyPlugin([
            { from: 'src/index.html', to: './' },
            { from: 'src/style.css', to: './' },
        ]),
    ],
};

Init npm in your project (npm init) and add this script:

"dev": "webpack-dev-server --open"

Import as normal, no params:

import { html, LitElement } from 'lit-element/lit-element.js';

Run the dev-server with live-reload with npm run dev.

With Pika:

npm install && npx @pika/web import { html, LitElement } from './web_modules/lit-element.js';

Utilize CommonJS (require) npm modules in the browser:

in terminal:

npm install browserify -g
npm install watchify -g
npm init
npm install glslCanvas --save

in html:

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

in main.js: const GlslCanvas = require('GlslCanvas');

back in terminal: watchify main.js -o bundle.js

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