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

webpack-recompilation-simulator

v3.2.0

Published

Helper to test and measure webpack recompilations

Downloads

326

Readme

Webpack Recompilation Simulator npm version Dependency Status Build Status Windows build status

If you are writting a webpack loader or a webpack plugin you might want to test the efficiency of your caching solutions.

The Webpack Recompilation Simulator allows you to run the webpack compilation multiple times and also to simulate file changes on the real file system just like a user would do.

How does it work?

The simulator hooks into webpack using the beforeResolve hook of the normalModuleFactory.
Inside the beforeResolve hook it will map all files you want to modify during your tests to a temporary file on the disk.

The temporary files are stored using temp-fs and can be altered using the simulateFileChange method provided by the Webpack Recompilation Simulator.

Api

Creating a new instance

To create an instance of the Webpack Recompilation Simulator you have to pass a webpack compiler:

const compiler = webpack(config);
const webpackSimulator = new WebpackRecompilationSimulator(compiler);

Add all files you might want to modify during your test

To make sure that webpack will add the correct file dependencies we have to add the test files before we run any tests:

webpackSimulator.addTestFile(__dirname + '/src/main.js');

Start a compilation

The webpack simulator will start a new compiler run:

webpackSimulator
  .run()  
  .then(function(stats) {
    console.log(stats);
  });

Simulate a file change

You can prepend a banner, append a footer or replace the entire content of the file.
Make sure that you added it using addTestFile.

    webpackSimulator.simulateFileChange(testFilePath, {
      banner: '/* Add a header */'
    });
    webpackSimulator.simulateFileChange(testFilePath, {
      content: '/* Overwrite the entire file */'
    });
    webpackSimulator.simulateFileChange(testFilePath, {
      footer: '/* Add a footer */'
    });

Simulate a file change using the node api

It is also possible to directly modify files using the fs api

``js const tempMainFile = webpackSimulator.addTestFile(__dirname + '/src/main.js');

fs.writeFileSync(tempMainFile, '// Hello world'); ``

Examples

Watch mode

  const compiler = webpack(config);
  const webpackSimulator = new WebpackRecompilationHelper(compiler);
  
  const tempMainFile = webpackSimulator.addTestFile(__dirname + '/src/main.js');

  // Start watching (this will also trigger an initial build)
  webpackSimulator.startWatching()  
    .then((stats) => {
      console.log('Initial compilation result:', stats);
      // Simulate a change to the main.js file 
      fs.writeFileSync(tempMainFile, 'require("./demo.js");');
      // Wait until webpack detects the file change and compiles again
      return webpackSimulator.waitForWatchRunComplete();
    })
    .then((stats) => {
      console.log('Partial compilation result:', stats);
    })
    // Stop watching
    .then(() => {
      return webpackSimulator.stopWatching();
    });

Direct compile runs

  const compiler = webpack(config);
  const webpackSimulator = new WebpackRecompilationHelper(compiler);
  
  const tempMainFile = webpackSimulator.addTestFile(__dirname + '/src/main.js');

  webpackSimulator.run()  
    .then(function(stats) {
      console.log('Initial compilation result:', stats);
      // Simulate a change to the main.js file
      fs.writeFileSync(tempMainFile, 'require("./demo.js");');
      // Compile it again:
      return webpackSimulator.run();
    })
    .then(function(stats) {
      console.log('Partial compilation result:', stats);
    });

Options

  • tmpDirectory: In order to simulate file changes this util needs a temporary directory. Default: process.cwd() + '/tmp/'

Motiviation

The Webpack Recompilation Simulator is used to test the https://github.com/jantimon/html-webpack-plugin

Requirements

  • Webpack 4
  • Node 6+

Known Issues