Giter VIP home page Giter VIP logo

webpack-on-demand's Introduction

Webpack on Demand

Motivation

This is an experimental webpack plugin that enables you to only compile modules once they are requested by the browser. This may be useful for the development server on large projects, as it is likely developers are only working on a small section of the application at any time.

IMPORTANT: This is an experimental and somewhat hacky plugin. Please do not expect it to work without issues

Installation

Install the package

yarn add --dev webpack-on-demand
OR
npm install --save-dev webpack-on-demand

In webpack.config.js:

  • Create an instance of the plugin

    const WebpackOnDemand = require('webpack-on-demand').default;
    
    const wodPluginInstance = new WebpackOnDemand();
    
  • Enable Hot Module Reloading for your dev server

  • Add the plugin to the webpack resolve config

    resolve: {
      plugins: [
        wodPluginInstance
      ],
    }
    
  • Pass the plugin server bootstrap to the before option of devServer

    devServer: {
       ...
       before: wodPluginInstance.serverBootstrap
    },
    

Demo

See: https://github.com/flut1/webpack-on-demand-demo

Usage instructions

Add the ?on-demand query string to an import statement. This will wrap the import in a callback that returns a Promise.

// before
import HomePage from './components/HomePage';

const HomePage = new HomePage();

// after
import getHomePage from './components/HomePage?on-demand';

getHomePage().then(HomePage => {
  const HomePage = new HomePage();
});

The module will only be compiled once the callback function (in the above example getHomePage()) is called. If you're not using the development server but a regular build, the import will compile normally but it will still be wrapped in a callback for consistency.

Note: if the same module is imported elsewhere without the ?on-demand query string, it will still compile immediately.

How does it work?

The on-demand compilation is triggered by (ab)using the HMR functionality in webpack. Here is a short rundown:

  • Requests with an ?on-demand query are picked up by the plugin
  • The plugin resolves this request to a {hash}.js file that is created on-the-fly inside this node module
  • The {hash}.js file:
    • requires another file, {hash}-mod.js, which is also created on-the-fly
    • accepts HMR updates for {hash}-mod.js
    • exports a function that returns a promise
  • Initially, {hash}-mod.js simply exports null
  • Once you call the function exported from {hash}.js, a call goes out to webpack-dev-server
  • The dev server responds to this call by overwriting the contents of {hash}-mod.js, with a require statement to the module you are trying to load
  • This triggers an incremental webpack compile
  • {hash}.js accepts the HMR update from {hash}-mod.js, and resolves the promise with the new module

webpack-on-demand's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

webpack-on-demand's Issues

good idea

Hello, I looked at your warehouse. It’s a good idea, but it will also create a lot of small files. If the project is large, a lot of files will be created. This kind of cures the symptoms and not the root cause. Although the created files can be placed in the worker,but...

Support webpack 5?

This plugin was recommended here tatethurston/webpack-inject-entry-plugin#1 (comment)

However when trying to use this with webpack 5.x it does not look compatible.

[18:13:04] TypeError: callback is not a function
    at /Users/mark/code/web/node_modules/enhanced-resolve/lib/Resolver.js:409:24
    at eval (eval at create (/Users/mark/code/web/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:13:1)
    at /Users/mark/code/web/node_modules/enhanced-resolve/lib/Resolver.js:409:24
    at eval (eval at create (/Users/mark/code/web/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:27:1)
    at /Users/mark/code/web/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:88:10
    at /Users/mark/code/web/node_modules/enhanced-resolve/lib/Resolver.js:409:24
    at eval (eval at create (/Users/mark/code/web/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:12:1)
    at /Users/mark/code/web/node_modules/enhanced-resolve/lib/Resolver.js:409:24
    at eval (eval at create (/Users/mark/code/web/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:58:1)
    at /Users/mark/code/web/node_modules/enhanced-resolve/lib/ConditionalPlugin.js:54:9

It's possible I'm doing something else wrong but I'm assuming it's webpack 5 compat.

Any chance this would be updated? It looks like a really neat plugin to optimize dev webpack experience. Kind of like Vite. Nice. Thanks!

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.