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

inline-require-webpack-plugin

v0.2.2

Published

Optimise generated bundles by inline requiring ES modules, without CommonJS deoptimisations

Downloads

24

Readme

This plugin enables an advanced runtime performance optimisation where evaluation cost of a module dependencies is shifted from the module initialisation phase to where each dependency is consumed.

This technique has been successfully leveraged by other bundlers (eg FB Metro) and proved to be quite effective on large applications, especially on 2-4 CPUs clients (with TTI improvements up to 400ms on P90).

It is an alternative to feeding Webpack with CommonJS modules and introducing a Babel plugin like @babel/plugin-transform-modules-commonjs. The main advantage is that Webpack is not aware of this optimisation while processing the source code, so all ESM benefits (eg treeshaking) and other plugins optimisations are not affected.

Compatible with Webpack v4.41+ and v5.24+

Usage

After installing it via

npm i -D inline-require-webpack-plugin

Import the plugin and add it to Webpack config plugins array

const { InlineRequireWebpackPlugin } = require('inline-require-webpack-plugin');
// ...
module.exports = {
  // ... webpack config
  plugins: [
    // ... all other plugins
    new InlineRequireWebpackPlugin()
  ];
}

Support for ConcatenatedModule plugin

If your configuration has optimization.concatenateModules enabled (defaults to true on prod builds), then you need to use patch-package to patch Webpack ConcatenatedModulePlugin in order to safely replace variables that map to imported modules.

You can find Webpack patches in /patches, grabbing the version relevant to your Webpack version (v4 or v5).

Documentation

From ESM top level requires to CommonJS-like inline requires

When Inline Require Plugin gets added to the Webpack config, it transforms such output before it get passed to the minification phase, manipulating it so that such top level requires are moved to their usage location. As an example, this how Webpack outputs ES modules normally:

var React = __webpack_require__('react')['default'];
var DragDropContext = __webpack_require__('react-beautiful-dnd')['DragDropContext'];
var MyComponent = __webpack_require__('./my-component')['default'];
var useOnDragEnd = __webpack_require__('./my-hooks')['onDragEnd'];

const MyApp = () => {
  const onDragEnd = useOnDragEnd();
  return React.createElement(DragDropContext, { onDragEnd }, React.createElement(MyComponent));
};
__webpack_exports__['MyApp'] = MyApp;

After adding InlineRequireWebpackPlugin the output will be:

var React = __webpack_require__('react')['default'];
// import 'react-beautiful-dnd'
// import './my-component'
// import './my-hooks'

const MyApp = () => {
  const onDragEnd = __webpack_require__('./my-hooks')['onDragEnd']();
  return React.createElement(
    __webpack_require__('react-beautiful-dnd')['DragDropContext'],
    { onDragEnd },
    React.createElement(__webpack_require__('./my-component')['default'])
  );
};
__webpack_exports__['MyApp'] = MyApp;

Quirks of inline requires

Such optimisation is not without risks. Indeed, if applied to everything, it does break ESM side effects. Given the output will evaluate imports only when needed, if some module requires a side effect to be triggered, then it might run too late and cause errors. Because of this risk, the plugin only optimises 3rd party dependencies that have explicit sideEffect: false in their package.json, but still aggressively applies it for all project files (as leveraging side effects is a bad pattern that should be avoided anyway).

Development and testing

The test suite is powered by Jest and will run for both Webpack v4 and v5 thanks to npm aliases, and is accessible via

npm run test

Contributions

Contributions to inline-require-webpack-plugin are welcome! Please see CONTRIBUTING.md for details.

Thanks

Big shout-out to @shuhei for his inline-requires-webpack-plugin, which demonstrated a similar plugin was possible.

License

Copyright (c) 2022 Atlassian and others. Apache 2.0 licensed, see LICENSE file.

With ❤️ from Atlassian