npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamic-entry-webpack-plugin

v2.0.2

Published

webpack plugin to make entry point as dynamic import

Downloads

9

Readme

dynamic-entry-webpack-plugin

webpack plugin to make entry point as dynamic import

Motivation

  • When creating front-end applications, webpack might generate multiple chunks (based on configurations). In order to load application, certain chunks needs to be present before loading application's main chunk. Example, vendor chunk must be present before main chunk is loaded.

  • Loading of dependent chunks before main chunk is taken by html-webpack-plugin. For example, we might encounter script tags injected in HTML

    <script src="/static/js/bundle.js"></script>
    <script src="/static/js/3.chunk.js"></script>
    <script src="/static/js/main.chunk.js"></script>

    html-webpack-plugin loads dependent chunks in sequence and finally main chunk is loaded at last. This works fine for SPA, where bundles are intended to be loaded in browser.

  • Consider the scenario where you need to load application bundle conditionally.
    For example, loading several micro-frontends bundles at run-time or loading a library.
    Such bundles are hosted on different servers and they are included into parent application at runtime.

  • For scenarios described above, it will be best if we could load single chunk, which in turn loads all the dependent chunks and main chunk for us.

  • This is where dynamic-entry-webpack-plugin comes handy. It will turn all webpack entry points into dynamic import statements and place them in a dynamic in-memory module. The actual entry point configuration is then updated with this new dyanmic module.

  • Webpack internally has all the information about dependent chunks for given chunk. When dynamically loading a module, webpack ensures that all the dependend modules for dynamic import are loaded first then the asked module is loaded.

  • This behavior of webpack is leveraged to provide only single entry chunk, which will load dependent and main chunks.

Usage

Create a object of plugin and pass it to the webpack plugins config.

const DynamicEntry = require('dynamic-entry-webpack-plugin');

module.exports = {
    ...,
    plugins: [
        new DynamicEntry({
            exportable: true,
        }),
    ]
};

Options
exportable - boolean, optional - To make entry module exportable. Default: true

Entry points

webpack supports entry point config in various formats. So accordingly, dynamic-entry-webpack-plugin behaves in following ways:

  • string

    When entry point is string, plugin creates new entry module with following code:

    export default () => import('<actual webpack entry point>');
    
    // OR
    
    import('<actual webpack entry point>'); // if exportable is false
  • array

    According to webpack when multiple configs are provided in array, all the entries are included in bundle with last entry exported. Providing similar behavior, plugin also imports all the entry points and last entry point is returned.

    export default () => (async () => {
        await import('<entry one>');
        await import('<entry two>');
        .
        .
        return import('last entry');
    })();
    
    // when exportable is false, entry module generated will as follows:
    (async () => {
        await import('<entry one>');
        await import('<entry two>');
        .
        .
        return import('last entry');
    })();
  • object

    According to webpack, for object entry point, webpack creates multiple bundles with its own runtime. Similar behavior is provided by this plugin as well.

  • function

    When given function to entry point it is awaited and result is then processed to form entry module. Return value can be string, array or object and it is processed according to above rules.

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue