Modules

HTML Scripts


html-script
├── index.html
└── js
    ├── lib.js
    └── main.js

html-script/js/lib.js:


function sum(x, y) {
  return x + y
}

function minus(x, y) {
  return x - y
}

html-script/js/main.js:

console.log(sum(10, 5))
console.log(minus(10, 5))

html-script/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script src="js/lib.js"></script>
  <script src="js/main.js"></script>
</body>
</html>

HTML ESM


html-esm
├── index.html
└── js
    ├── lib.mjs
    └── main.mjs

html-esm/js/lib.mjs:


export function sum(x, y) {
  return x + y
}

export function minus(x, y) {
  return x - y
}

html-esm/js/main.mjs:

import {sum, minus} from './lib.mjs'

console.log(sum(10, 5))
console.log(minus(10, 5))

html-esm/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script type="module" src="js/main.mjs"></script>
</body>
</html>

HTML ESM FAIL


html-esm-fail
├── index.html
└── js
    ├── date_fns.js
    ├── lib.mjs
    └── main.mjs

html-esm-fail/js/lib.mjs:


export function sum(x, y) {
  return x + y
}

export function minus(x, y) {
  return x - y
}

html-esm-fail/js/main.mjs:

import {sum, minus} from './lib.mjs'
import * as date from './date_fns.js'                         // Error

console.log(sum(10, 5))
console.log(minus(10, 5))
console.log(date.format(new Date(2014, 1, 11), 'MM/DD/YYYY')) // Error

html-esm-fail/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script type="module" src="js/main.mjs"></script>
</body>
</html>

HTML ESM - Parcel Bundler


html-parcel-esm
├── index.html
├── js
│   ├── lib.mjs
│   └── main.mjs
└── package.json

html-parcel-esm/js/lib.mjs:


export function sum(x, y) {
  return x + y
}

export function minus(x, y) {
  return x - y
}

html-parcel-esm/js/main.mjs:

import {sum, minus} from './lib.mjs'
import {format} from 'date-fns'

console.log(sum(10, 5))
console.log(minus(10, 5))
console.log(format(new Date(2014, 1, 11), 'MM/DD/YYYY'))

html-parcel-esm/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script src="js/main.mjs"></script>
</body>
</html>

html-parcel-esm/package.json:

$ npm init -y
$ npm i date-fns -s
$ npm i parcel-bundler -D
{
  "name": "html-parcel-esm",
  "version": "1.0.0",
  "description": "",
  "main": "js/main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "date-fns": "^1.29.0"
  },
  "devDependencies": {
    "parcel-bundler": "^1.9.7"
  }
}

$ npm install
$ npm list | grep date-fns
└── date-fns@1.29.0
$ npx parcel index.html
$ tree .
html-parcel-esm
├── dist
│   ├── index.html
│   ├── main.b0ab085c.js
│   └── main.b0ab085c.map
├── js
│   ├── lib.js
│   └── main.js
├── node_modues
│   └── ...
├── index.html
└── package-lock.json
└── package.json
$ chrome http://localhost:1234

HTML ESM - Webpack Bundler


html-webpack-esm
├── index.html
├── js
│   ├── lib.mjs
│   └── main.mjs
└── package.json

html-webpack-esm/js/lib.mjs:


export function sum(x, y) {
  return x + y
}

export function minus(x, y) {
  return x - y
}

html-webpack-esm/js/main.mjs:

import {sum, minus} from './lib.mjs'
import date from 'date-fns'

console.log(sum(10, 5))
console.log(minus(10, 5))
console.log(date.format(new Date(2014, 1, 11), 'MM/DD/YYYY'))

html-webpack-esm/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script src="dist/main.js"></script>
</body>
</html>

html-webpack-esm/package.json:

$ npm init -y
$ npm i date-fns -s
$ npm i webpack webpack-cli -D
{
  "name": "html-webpack-esm",
  "version": "1.0.0",
  "description": "",
  "main": "js/main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build-dev": "npx webpack js/main.mjs --mode development"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "date-fns": "^1.29.0"
  },
  "devDependencies": {
    "webpack": "^4.16.2",
    "webpack-cli": "^3.1.0"
  }
}

$ npx webpack js/main.mjs --mode development
Hash: 0efd707d06341ed4a9fe
Version: webpack 4.16.2
Time: 633ms
Built at: 07/24/2018 10:38:19 AM
  Asset     Size  Chunks             Chunk Names
main.js  271 KiB    main  [emitted]  main
Entrypoint main = main.js
[./js/lib.js] 94 bytes {main} [built]
[./js/main.js] 175 bytes {main} [built]
    + 159 hidden modules
$ tree .
html-webpack-esm
├── dist
│   └── main.js
├── js
│   ├── lib.mjs
│   └── main.mjs
├── node_modues
│   └── ...
├── index.html
└── package-lock.json
└── package.json
$ chrome index.html
$ npm run build-dev

> html-webpack-esm@1.0.0 build-dev /home/user/html-webpack-esm
> npx webpack js/main.js --mode development

Hash: 0efd707d06341ed4a9fe
Version: webpack 4.16.2
Time: 587ms
Built at: 07/24/2018 10:37:33 AM
  Asset     Size  Chunks             Chunk Names
main.js  271 KiB    main  [emitted]  main
Entrypoint main = main.js
[./js/lib.js] 94 bytes {main} [built]
[./js/main.js] 175 bytes {main} [built]
    + 159 hidden modules

HTML ESM - Webpack Bundler - Config


html-webpack-esm-config
├── index.html
├── js
│   ├── lib.mjs
│   └── main.mjs
├── package.json
└── webpack.config.js

html-webpack-esm-config/js/lib.mjs:


export function sum(x, y) {
  return x + y
}

export function minus(x, y) {
  return x - y
}

html-webpack-esm-config/js/main.mjs:

import {sum, minus} from './lib.mjs'
import date from 'date-fns'

console.log(sum(10, 5))
console.log(minus(10, 5))
console.log(date.format(new Date(2014, 1, 11), 'MM/DD/YYYY'))

html-webpack-esm-config/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script src="dist/bundle.js"></script>
</body>
</html>

html-webpack-esm-config/package.json:

$ npm init -y
$ npm i date-fns -s
$ npm i webpack webpack-cli -D
{
  "name": "html-webpack-esm",
  "version": "1.0.0",
  "description": "",
  "main": "js/main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build-dev": "npx webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "date-fns": "^1.29.0"
  },
  "devDependencies": {
    "webpack": "^4.16.2",
    "webpack-cli": "^3.1.0"
  }
}

html-webpack-esm-config/webpack.config.js:

const path = require('path');

module.exports = {
  mode: 'development',
  
  entry: './js/main.mjs',
  
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};
$ npm run build-dev

> html-webpack-esm@1.0.0 build-dev /home/user/html-webpack-esm-config
> npx webpack --mode development

Hash: 381cb32daa0a9b43ac50
Version: webpack 4.16.2
Time: 692ms
Built at: 07/24/2018 12:21:38 PM
    Asset     Size  Chunks             Chunk Names
bundle.js  271 KiB    main  [emitted]  main
Entrypoint main = bundle.js
[./js/lib.js] 94 bytes {main} [built]
[./js/main.js] 175 bytes {main} [built]
    + 159 hidden modules
$ chrome index.html

HTML ESM - Webpack Bundler - Babel


html-webpack-esm-babel
├── index.html
└── js
    ├── lib.js
    └── main.js

html-webpack-esm-babel/js/lib.mjs:


export function sum(x, y) {
  return x + y
}

export function minus(x, y) {
  return x - y
}

export function pow(x, y) {
  return x ** y
}

html-webpack-esm-babel/js/main.mjs:

import {sum, minus, pow} from './lib.mjs'
import date from 'date-fns'

console.log(sum(10, 5))
console.log(minus(10, 5))
console.log(pow(10, 5))
console.log(date.format(new Date(2014, 1, 11), 'MM/DD/YYYY'))

html-webpack-esm-babel/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script src="dist/bundle.js"></script>
</body>
</html>

html-webpack-esm-babel/package.json:

$ npm init -y
$ npm i date-fns -s
$ npm i webpack webpack-cli @webpack-cli/init -D
{
  "name": "html-webpack-esm-babel",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build-dev": "npx webpack --config webpack.dev.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "date-fns": "^1.29.0"
  },
  "devDependencies": {
    "@webpack-cli/init": "^0.1.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "babel-preset-env": "^1.7.0",
    "uglifyjs-webpack-plugin": "^1.2.7",
    "webpack": "^4.16.2",
    "webpack-cli": "^3.1.0"
  }
}

html-webpack-esm-babel/webpack.dev.js:

$ npx webpack init
? Will your application have multiple bundles? No
? Which module will be the first to enter the application? [default: ./src/index] ./js/main.mjs
? Which folder will your generated bundles be in? [default: dist]:
? Will you be using ES2015? Yes
? Will you use one of the below CSS solutions? No
...
Congratulations! Your new webpack configuration file has been created!
const webpack = require('webpack')
const path = require('path')




/*
 * SplitChunksPlugin is enabled by default and replaced
 * deprecated CommonsChunkPlugin. It automatically identifies modules which
 * should be splitted of chunk by heuristics using module duplication count and
 * module category (i. e. node_modules). And splits the chunks…
 *
 * It is safe to remove "splitChunks" from the generated configuration
 * and was added as an educational example.
 *
 * https://webpack.js.org/plugins/split-chunks-plugin/
 *
 */

/*
 * We've enabled UglifyJSPlugin for you! This minifies your app
 * in order to load faster and run less javascript.
 *
 * https://github.com/webpack-contrib/uglifyjs-webpack-plugin
 *
 */

const UglifyJSPlugin = require('uglifyjs-webpack-plugin');




module.exports = {
  module: {
    rules: [{
      include: [path.resolve(__dirname, 'js')],
      loader: 'babel-loader',

      options: {
        plugins: ['syntax-dynamic-import'],

        presets: [['env', {
          'modules': false
        }]]
      },

      test: /\.js$/
    }]
  },

  entry: './js/main.mjs',

  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },

  mode: 'development',

  optimization: {
    splitChunks: {
      cacheGroups: {
        vendors: {
          priority: -10,
          test: /[\\/]node_modules[\\/]/
        }
      },
      chunks: 'async',
      minChunks: 1,
      minSize: 30000,
      name: true
    }
  }
}
$ npx webpack --config webpack.dev.js
Hash: cf61f9a11c1734bbe58f
Version: webpack 4.16.2
Time: 1470ms
Built at: 07/24/2018 6:27:30 PM
    Asset     Size  Chunks             Chunk Names
bundle.js  271 KiB    main  [emitted]  main
Entrypoint main = bundle.js
[./js/lib.js] 151 bytes {main} [built]
[./js/main.js] 214 bytes {main} [built]
    + 159 hidden modules
$ npm run build-dev

> html-webpack-esm-babel@1.0.0 build-dev /home/user/html-webpack-esm-babel
> npx webpack --config webpack.dev.js

Hash: cf61f9a11c1734bbe58f
Version: webpack 4.16.2
Time: 1732ms
Built at: 07/24/2018 6:28:45 PM
    Asset     Size  Chunks             Chunk Names
bundle.js  271 KiB    main  [emitted]  main
Entrypoint main = bundle.js
[./js/lib.js] 151 bytes {main} [built]
[./js/main.js] 214 bytes {main} [built]
    + 159 hidden modules
$ chrome index.html

Node Modules - Webpack Bundler


html-webpack-node-modules
├── index.html
├── js
│   ├── lib.js
│   └── main.js
├── package.json
└── webpack.config.js

html-webpack-node-modules/js/lib.js:


module.exports.sum = function (x, y) {
  return x + y
}

module.exports.minus = function (x, y) {
  return x - y
}

html-webpack-node-modules/js/main.js:

var math = require('./lib.js')
var date = require('date-fns')

console.log(math.sum(10, 5))
console.log(math.minus(10, 5))
console.log(date.format(new Date(2014, 1, 11), 'MM/DD/YYYY'))

html-webpack-node-modules/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script src="dist/bundle.js"></script>
</body>
</html>

html-webpack-node-modules/package.json:

{
  "name": "html-webpapck-node-modules",
  "version": "1.0.0",
  "description": "",
  "main": "js/main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build-js": "npx webpack"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "date-fns": "^1.29.0"
  },
  "devDependencies": {
    "webpack": "^4.16.2",
    "webpack-cli": "^3.1.0"
  }
}

html-webpack-node-modules/webpack.config.js:

const path = require('path');

module.exports = {
  mode: 'development',
  
  entry: './js/main.js',
  
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};
$ npm run build-dev

> html-webpack-esm@1.0.0 build-dev /home/user/html-webpack-esm-config
> npx webpack --mode development

Hash: 381cb32daa0a9b43ac50
Version: webpack 4.16.2
Time: 692ms
Built at: 07/24/2018 12:21:38 PM
    Asset     Size  Chunks             Chunk Names
bundle.js  271 KiB    main  [emitted]  main
Entrypoint main = bundle.js
[./js/lib.js] 94 bytes {main} [built]
[./js/main.js] 175 bytes {main} [built]
    + 159 hidden modules
$ chrome index.html

References