Code Monkey home page Code Monkey logo

webpack-module-nomodule-plugin's Introduction

Webpack module-nomodule plugin

npm version

Installation

npm install --save-dev webpack-module-nomodule-plugin

or

yarn add -D webpack-module-nomodule-plugin

Why

This automates the generation of module-nomodule scripts.

This project relies on html-webpack-plugin.

module-nomodule explained.

How to use

  1. Create separate build steps for your modern and legacy builds in your webpack config.
  2. Each build step should include at least one use of the html-webpack-plugin with the inject: 'body' option set.
  3. Include this plugin with the name of the configuration in the constructor (e.g. new WebpackModuleNomodulePlugin('legacy'); or new WebpackModuleNomodulePlugin('modern');)

The rest will be handled for you!

Output modes

The second argument to WebpackModuleNomodulePlugin allows you to specify an output mode.

At this point there are two:

  • efficient (default)
  • minimal

These are exported as an object containing OUTPUT_MODES.

Efficient

Ensures IE11 and some Edge versions won't double download the contents of your scripts. This adds some more size to your html file and defers the loading of scripts.

This will look something like this:

efficient mode output

What happens in this picture?

  • We declare the modern javascript files as modulepreload to shave a bit off the load/parse time of these
  • We dynamically inject the entry-modules with the bigger script to avoid injecting needlessly

Minimal

Makes the most minimal output possible, this will be downloaded twice on older Edge and IE11.

This will look something like this:

minimal mode output

The script in the middle between type="module" and nomodule is meant for safari 11 compat.

nomodule will load for browsers who don't support module and the other way around, the problem with this approach is we'll be downloading both bundles on certain Edge versions and IE11.

Example

https://github.com/JoviDeCroock/POC-ModularLegacyBuild

This example uses multiple techniques to guarantee the best size, like using native-url in modern browsers, ...

webpack-module-nomodule-plugin's People

Contributors

andieelmes avatar dependabot[bot] avatar ge11ert avatar hydrophobefireman avatar jovidecroock avatar thecontrarycat 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

Watchers

 avatar  avatar

webpack-module-nomodule-plugin's Issues

Tests

We need this to keep working so some tests would be great

How to build two bundles for module and nomodule?

my folders structure:
...
public
resources
...
webpack.config.js
webpack-prod.config.js

###code inside webpack.config.js:

const path = require("path");
const ROOT = path.resolve(__dirname, "resources");
const templateCache = require("./templateCache");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const DESTINATION = path.resolve( __dirname, 'public/build' );//*

module.exports = {
  mode: "development",
  context: ROOT,
  resolve: {
    extensions: [".js"],
    alias: {
      "@": path.resolve(__dirname, "viewneo-components/components"),
      "~": path.resolve(__dirname, "viewneo-components"),
    }
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"]
          }
        }
      },
      {
        test: /\.vue$/,
        use: 'vue-loader'
      },
      {
        test: /\.(css|sass|scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "postcss-loader",
          { loader: "resolve-url-loader", options: { 
            // engine: "rework",
            // root: '',
            } 
          },
          { loader: "sass-loader", options: { prependData: '@import "~/assets/scss/style.scss";' } }
        ]
      },
      {
        test: /\.(jpg|png|gif|svg)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              esModule: false,
              name: '../[contenthash].[ext]',
            }
          }
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf)$/,
        use: "file-loader?outputPath=fonts/"
      }
    ]
  },
  
  plugins: [
    new MiniCssExtractPlugin({
      filename: "css/app.css"
    }),
    new VueLoaderPlugin(),
    require('autoprefixer')
  ],

  entry: ["./assets/sass/app.scss", "./assets/js/app.js"],
  output: {
      path: DESTINATION,
      filename: 'js/app.js'
  },
};

================================================================
###code inside webpack-prod.config.js:

const path = require('path');
const webpackMerge = require('webpack-merge');
const WebpackModuleNomodulePlugin = require('webpack-module-nomodule-plugin');
const commonConfig = require('./webpack.config.js');
const TerserPlugin = require('terser-webpack-plugin');
const DESTINATION = path.resolve( __dirname, 'public/build' ); 

module.exports = webpackMerge(commonConfig, {
    mode: 'production',
    optimization:{
        minimize: true,
        minimizer: [
          new TerserPlugin({ // TODO optimize bundle size
            extractComments: 'all',
            terserOptions: {
              ecma: undefined,
              warnings: false,
              parse: {},
              compress: {},
              mangle: false,
              module: false,
              output: null,
              toplevel: false,
              nameCache: false,
              ie8: false,
              keep_classnames: false,
              keep_fnames: false,
              safari10: false,
            },
          }),
        ],
    },
    plugins: [
      new WebpackModuleNomodulePlugin('legacy'),
    ]

});

Support HTML-template

Support html template by adding: data-module-nomodule to the script tags injected by us this way we can't accidentely delete/edit/double scripts.

This also goes for potentially deleted css-tags (in all honesty this should work but you never know).

ESM code not included in html when inject is set to head

Hi there,

I just ran into the issue that when using the plugin in efficient mode the DOMContentLoaded function stays empty when the HtmlWebpackPlugin's option inject is set to head like so

new HtmlWebpackPlugin({ inject: 'head' }),

The result only includes regular script imports for the legacy code in this case.

<script src="main.dd065219.js"></script>
<script type="module">self.m=1</script>
<script>addEventListener('DOMContentLoaded', function() {

})
function $l(e,d,c){c=document.createElement("script"),self.m?(e && (c.src=e,c.type="module")):d && (c.src=d),c.src && document.head.appendChild(c)}</script>

When I remove the inject option the $l calls are injected as expected and the modern version loaded:

<link rel="modulepreload" href="runtime.3c7f5b89.esm.js">
<link rel="modulepreload" href="main.1a72c97f.esm.js">
<script type="module">self.m=1</script><script>addEventListener('DOMContentLoaded', function() {
  $l("runtime.3c7f5b89.esm.js", "runtime.fe4dae1f.js")
  $l("main.1a72c97f.esm.js", "main.c4f2db7e.js")
})
function $l(e,d,c){c=document.createElement("script"),self.m?(e && (c.src=e,c.type="module")):d && (c.src=d),c.src && document.head.appendChild(c)}</script>

Thanks for making this plugin ๐Ÿ™‚

Use it with create-react-app?

I saw your PR in create-react-app. It is a pity that it was not merged and this another PR was forgetten over one year. Now I wonder if I can use this plugin in my CRA-based project?

Also, if you want to continue your work for CRA, maybe hacking it with react-app-rewired, if it's unable to be merged to mainline. If possible, I'm glad to test and debug with you with my CRA-based project.

`window.onload` in efficient mode

I was wondering how load event could affect TTI. AFAIK, load fires after all page resources are loaded, including images. Even if scripts are already preloaded with modulepreload, it seems like it might be too late for a script insertion, but I'm not sure...

Could DOMContentLoaded make any difference?

The "constants" file seems to be missing in the 0.3.0 build.

When i run my project, i have this error.

.../node_modules/webpack-cli/bin/cli.js:93
                                throw err;
                                ^

Error: Cannot find module './constants'
Require stack:
- (...)/node_modules/webpack-module-nomodule-plugin/src/index.js
- (...)/webpack.ds.js
- (...)/node_modules/webpack-cli/bin/utils/convert-argv.js

When i check in my node_modules/webpack-module-nomodule-plugin/src package folder, the constants.js is not present.

installed version 0.3.0 with yarn add -D webpack-module-nomodule-plugin

How to use this alongside CspHtmlWebpackPlugin?

I have webpack-module-nomodule-plugin configured and working well for my build, but now we want to add a content security policy and have turned to CspHtmlWebpackPlugin to automatically generate the policy and hashes in the generated HTML file.

The problem is that the tags added by this module don't get hashed. Note in the example below how the stylesheet correctly receives a nonce, but the scripts added by webpack-module-nomodule-plugin aren't hashed:

<head>
	<meta http-equiv="Content-Security-Policy" content="base-uri 'self'; object-src 'none'; script-src 'unsafe-inline' 'self' 'unsafe-eval' 'sha256-nbjaHTCvPZmU6ZiLRgVqNzYz409cIY3Rn9XE5hGHMeI=' 'sha256-nbRAlhO1i8u4QabQ5nggX59s9v3tjLo5sWZX+BTFHtI='; style-src 'unsafe-inline' 'self' 'unsafe-eval' 'nonce-lqH2Zrsqc6O6yUT997okUw=='">
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1">
	<link href="/app.0d82d688390ad51c2cb4.css" rel="stylesheet" nonce="lqH2Zrsqc6O6yUT997okUw==">
	<link rel="modulepreload" href="/modern.vendors.3be4fa4f9fd56490771e.js">
	<link rel="modulepreload" href="/modern.main.a90abc59fba4a2ae5ece.js">
	<script type="module">self.m=1</script>
	<script>addEventListener('DOMContentLoaded', function() {
	$l("/modern.vendors.3be4fa4f9fd56490771e.js", "/legacy.vendors.51840cb0b0964db95818.js")
	$l("/modern.main.a90abc59fba4a2ae5ece.js", "/legacy.main.3ef3fe9d7d57298324e9.js")
	})
	function $l(e,d,c){c=document.createElement("script"),self.m?(e && (c.src=e,c.type="module")):d && (c.src=d),c.src && document.head.appendChild(c)}</script>
</head>

How can we make these two plugins play nicely together?

HtmlWebpackPlugin Issues

I'm getting a peer dep issue on yarn install. Version that's required on the peer dep is pointing to latest and greatest (>=4.0.0-alpha.2). I think the issue is with the >=. Think maybe it should be ^. Also is there a specific reason you are requiring [email protected]?

Also I'm getting:

HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tapAsync(
                        ^
TypeError: HtmlWebpackPlugin.getHooks is not a function

So I tried cloning this down and yarn linking. I updated it to use html-webpack-plugin@^3.2.0 and that doesn't seem to work either.

Thoughts?

Cant make it work with webpack 5

HI.
I just cant make it work whith latest webpack 5.

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { extendDefaultPlugins } = require('svgo');
var HtmlWebpackEsmodulesPlugin  = require('webpack-module-nomodule-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const environment = require('./configuration/environment');

module.exports = [
    {
        name: 'modern',
        stats: 'verbose',
        target: 'browserslist:modern',
        entry: {
            app: path.resolve(environment.paths.source, 'js', 'app.js'),
        },
        output: {
            filename: 'js/[name].modern.js',
            path: environment.paths.output,
            // publicPath: '/dist/',
        },
        module: {
            rules: [
                {
                    test: /\.ejs$/i,
                    use: [
                        {
                            loader: 'html-loader',
                            options: {
                                sources: {
                                    list: [
                                        "...",
                                        {
                                            tag: "div",
                                            attribute: "data-image-src",
                                            type: "src",
                                        },
                                        {
                                            tag: "a",
                                            attribute: "href",
                                            type: "src",
                                            filter: (tag, attribute, attributes, resourcePath) => {
                                                let shouldReturn = false;

                                                attributes.forEach(attribute => {
                                                    if (attribute.name == 'data-add-to-html-loader') {
                                                        shouldReturn = true;
                                                        return;
                                                    }
                                                });

                                                if (shouldReturn) {
                                                    return true;
                                                }
                                            }
                                        },
                                    ]
                                }
                            },
                        },
                        'template-ejs-loader'
                    ],
                },
                {
                    test: /\.((c|sa|sc)ss)$/i,
                    use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'],
                },
                {
                    test: /\.m?js$/,
                    exclude: /node_modules/,
                    use: [
                        {
                            loader: 'babel-loader',
                            options: {
                                cacheDirectory: true,
                                envName: "modern"
                            }
                        }
                    ]
                },
                {
                    test: /\.(png|gif|jpe?g|svg)$/i,
                    type: 'asset',
                    generator: {
                        filename: 'images/[name].[hash:6][ext][query]',
                    },
                    parser: {
                        dataUrlCondition: {
                            maxSize: environment.limits.images,
                        },
                    },
                },
                {
                    test: /\.(eot|ttf|woff|woff2)$/,
                    type: 'asset',
                    generator: {
                        filename: 'fonts/[name].[hash:6][ext][query]',
                    },
                    parser: {
                        dataUrlCondition: {
                            maxSize: environment.limits.fonts,
                        },
                    },
                },
            ],
        },
        optimization: {
            minimizer: [
                "...",
                new ImageMinimizerPlugin({
                    minimizer: {
                        implementation: ImageMinimizerPlugin.imageminMinify,
                        options: {
                            plugins: [
                                ['gifsicle', { interlaced: true }],
                                ['jpegtran', { progressive: true }],
                                ['optipng', { optimizationLevel: 5 }],
                                [
                                    'svgo',
                                    {
                                        plugins: [
                                            {
                                                name: 'preset-default',
                                                params: {
                                                    overrides: {
                                                        removeViewBox: false,
                                                    },
                                                },
                                            },
                                        ],
                                    },
                                ],
                            ],
                        },
                    },
                    generator: [
                        {
                            preset: "webp",
                            implementation: ImageMinimizerPlugin.imageminGenerate,
                            options: {
                                plugins: ["imagemin-webp"],
                            },
                        },
                    ],
                }),
            ],
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: 'css/[name].css',
            }),
            new HtmlWebpackPlugin({
                template: path.resolve(__dirname, './src/views/indexTEST.html'),
                filename: 'index.html',
            }),
            new HtmlWebpackEsmodulesPlugin('modern')
        ]
    },

    {
        name: 'legacy',
        stats: 'verbose',
        target: 'browserslist:legacy',
        entry: {
            app: path.resolve(environment.paths.source, 'js', 'app.js'),
            fetch: 'whatwg-fetch',
        },
        output: {
            filename: 'js/[name].js',
            path: environment.paths.output,
        },
        module: {
            rules: [
                {
                    test: /\.ejs$/i,
                    use: [
                        {
                            loader: 'html-loader',
                            options: {
                                sources: {
                                    list: [
                                        "...",
                                        {
                                            tag: "div",
                                            attribute: "data-image-src",
                                            type: "src",
                                        },
                                        {
                                            tag: "a",
                                            attribute: "href",
                                            type: "src",
                                            filter: (tag, attribute, attributes, resourcePath) => {
                                                let shouldReturn = false;

                                                attributes.forEach(attribute => {
                                                    if (attribute.name == 'data-add-to-html-loader') {
                                                        shouldReturn = true;
                                                        return;
                                                    }
                                                });

                                                if (shouldReturn) {
                                                    return true;
                                                }
                                            }
                                        },
                                    ]
                                }
                            },
                        },
                        'template-ejs-loader'
                    ],
                },
                {
                    test: /\.((c|sa|sc)ss)$/i,
                    use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'],
                },
                {
                    test: /\.m?js$/,
                    exclude: /node_modules/,
                    use: [
                        {
                            loader: 'babel-loader',
                            options: {
                                cacheDirectory: true,
                                envName: "legacy"
                            }
                        }
                    ]
                },
                {
                    test: /\.(png|gif|jpe?g|svg)$/i,
                    type: 'asset',
                    generator: {
                        filename: 'images/[name].[hash:6][ext][query]',
                    },
                    parser: {
                        dataUrlCondition: {
                            maxSize: environment.limits.images,
                        },
                    },
                },
                {
                    test: /\.(eot|ttf|woff|woff2)$/,
                    type: 'asset',
                    generator: {
                        filename: 'fonts/[name].[hash:6][ext][query]',
                    },
                    parser: {
                        dataUrlCondition: {
                            maxSize: environment.limits.fonts,
                        },
                    },
                },
            ],
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: 'css/[name].css',
            }),
            new HtmlWebpackPlugin({
                template: path.resolve(__dirname, './src/views/indexTEST.html'),
                filename: 'index.html',
            }),
            new HtmlWebpackEsmodulesPlugin('legacy'),

        ]
    }
];

It outputs just one script tag, not both modern and legacy

First it outputs this html


<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Modern / Legacy POC</title>
    <script defer="defer" src="js/app.js"></script>
    <script defer="defer" src="js/fetch.js"></script>
    <link href="css/app.css" rel="stylesheet">
    <script>addEventListener('DOMContentLoaded', function () {
            function $l(e, d, c) { c = document.createElement("script"), "noModule" in c ? (e && (c.src = e, c.type = "module", c.crossOrigin = "anonymous")) : d && (c.src = d, c.async = true), c.src && document.head.appendChild(c) }

        })</script>
</head>

<body>
    <div id="root"></div>
</body>

</html>

Then it overwrites this html by this, and creates assets.index.json (that it wont never remove):

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Modern / Legacy POC</title>
    <script defer="defer" src="js/app.modern.js"></script>
    <link href="css/app.css" rel="stylesheet">
</head>

<body>
    <div id="root"></div>
</body>

</html>

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.