Git Product home page Git Product logo

webpack-modern-build-plugin's Introduction

Webpack Modern Build Plugin

Plugin that creates module/nomodule scripts.

Install

  npm i --save-dev webpack-modern-build-plugin
  yarn add --dev webpack-modern-build-plugin

Usage

The plugin generates module/nomodule scripts and relies on html-webpack-plugin. This plugin expects to return two configurations in your webpack - legacy and modern. One of those values should be passed as mode option to the plugin in both configurations.

Example of webpack config:

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin')
const WebpackModernBuildPlugin = require('webpack-modern-build-plugin')

module.exports = [
  {
    entry: 'index.js',
    output: {
      path: __dirname + '/dist',
      filename: 'bundle.js',
    },
    plugins: [
      new HtmlWebpackPlugin(),
      new WebpackModernBuildPlugin({
        mode: 'modern',
      }),
    ],
  },
  {
    entry: 'index.js',
    output: {
      path: __dirname + '/dist',
      filename: 'es5.bundle.js',
    },
    plugins: [
      new HtmlWebpackPlugin(),
      new WebpackModernBuildPlugin({
        mode: 'legacy',
      }),
    ],
  },
]

This will generate a file dist/index.html containing the following

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Webpack App</title>
  </head>
  <body>
    <script src="bundle.js" type="module" crossorigin="anonymous"></script>
    <script>
      ;(function () {
        var d = document
        var c = d.createElement('script')
        if (!('noModule' in c) && 'onbeforeload' in c) {
          var s = !1
          d.addEventListener(
            'beforeload',
            function (e) {
              if (e.target === c) {
                s = !0
              } else if (!e.target.hasAttribute('nomodule') || !s) {
                return
              }
              e.preventDefault()
            },
            !0,
          )
          c.type = 'module'
          c.src = '.'
          d.head.appendChild(c)
          c.remove()
        }
      })()
    </script>
    <script src="es5.bundle.js" nomodule></script>
  </body>
</html>

Example

Here you can find the implementation of differential serving using this plugin https://github.com/abereghici/diff_serving

The script in the middle between type="module" and nomodule is for Safari 10.1. This version supports modules, but does not support the nomodule attribute - it will load "script nomodule" anyway. This snippet solve this problem, but only for script tags that load external code.

webpack-modern-build-plugin's People

Contributors

abereghici avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

whitecrownclown

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.