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

mock-exports-loader

v1.0.0

Published

Auto create mocks for export for modules

Downloads

1

Readme

Mock Exports Loader

👉中文

MockExportsLoader converts exports of given module to empty functions by analyze module exports and type definition files (some compressed commonjs code may not be easy to extract exports by static analyze, thus we can use it's type definitions to extract exports).

Sometimes we may need to build special package for special purpose, and some module is not truly executed but cannot be shaked due to some side effects; we may use null-loader or { alias: { xx: false } } to shake the module but the code may be broken after the shaking. MockExportsLoader is for such case that we can automatically 'mock' those modules and provide empty function like what we do for test.

Roadmap

✅ Analyze exports by code

✅ Analyze exports by type definition files

🔳 Analyze exports type, distinguish function and const object or value

🔳 Provide type definition files for mock file

How to use

Install

npm i --save-dev mock-exports-loader

Usage

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /target_module/,
        loader: 'mock-exports-loader',
        options: {
          parseFromType: false,
          generateDefault: false,
          createMocks: () => ({}),
        },
      }
    ],
  },
  // ...
}

Loader Options

parseFromType

boolean | undefined, default is false

Whether to extract exports from type defintion file, recommend for compressed non-es6 module with type definitions. Support auto detect @type/xxx for lib xxx, e.g. for lib module-a, it will automatically search @type/module-a if module-a has no types/typing field in package.json.

{
  test: /module-a/,
  loader: 'mock-exports-loader',
  options: {
    parseFromType: true,
  },
}

generateDefault

boolean | undefined, default is false

Whether to merge all named exports to module's default export. e.g. for module fs-extra, we can use either import fs from 'fs-extra' fs.readFileSync or import { readFileSync } from 'fs-extra',but automatic mock will create an empty default export due to the current static code analysis logic.

// ...
module.exports.readFileSync = function () {};
// ...
module.exports.default = function () {};

Set generateDefault to true to make the mock fs object work as expected:

// ...
module.exports.readFileSync = function () {};
// ...
module.exports.default = {
  // ...
  readFileSync: function () {};
  // ...
};

createMocks

(() => object) | undefined, default is undefined

There could be some exports cannot be recognized or some constants mistakenly converted to empty function. We could provides a createMocks function to fix it.

{
  test: /module-a/,
  loader: 'mock-exports-loader',
  options: {
    createMocks: () => ({ CONST_VALUE: 3 }),
  },
}

mock Result:

module.exports.CONST_VALUE = 3;

Known Issues

  1. Due to current logic of static code analysis, it's not easy to distinguish function export and value object export. All exports will be convert to empty function, you must provide a createMocks to provide the correct constant exports.