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

babel-plugin-dynamic-import-override

v1.1.2

Published

Babel-Plugin to override every dynamic import and attach error/success handler

Downloads

129

Readme

babel-plugin-dynamic-import-override

Babel plugin to override every dynamic import in application by attaching success and error handler.

Goals

To override all lazy-loaded chunks in an application from a single file, babel.config.js (or .babelrc), eliminating the need to manually override every chunk.

Installation

npm install -D babel-plugin-dynamic-import-override
yarn add -D babel-plugin-dynamic-import-override

Options

  • errorHandler - error object is accesible in err.

  • successHandler - result is accesible in res.

NOTE: successHandler and errorHandler should be wrapped as string. Dynamic Import that has failed/succeeded can be accessed via following variables: [Refer: example]

  • importNode - Contains full code of dynamic import. Example: import('./Home.js')
  • importName - Contains file name of dynamic import. Example: ./Home.js

Skip Overriding Dynamic Import

To skip overriding some dynamic imports, use following magic comment in the dynamic imports

/* skipImportOverride: true */

Usage

Via babel.config.js (Recommended)

babel.config.js

module.exports = function (api) {
  api.cache(true);

  const plugins = [
    ["babel-plugin-dynamic-import-override", {
      "successHandler": `console.log('inside success handler, perform success handling here, result is available in res');
        console.log(res);`,
      "errorHandler": `console.log('inside error handler, perform error handling here, error is available in err');
        console.log(err);`
    }]
  ];

  return {
    presets,
    plugins
  };
}

.babelrc

{
  "plugins": [
    ["babel-plugin-dynamic-import-override", {
      "successHandler": "console.log('inside success handler, perform success handling here, result is available in res', res);",
      "errorHandler": "console.log('inside error handler, perform error handling here, error is available in err', err);"
    }]
  ]
}

Example

Example 1:

import('./Home.js');

will be converted to,

Promise.resolve(import('./Home.js'))
.then(res => {
  // --- successhandler code will come here ----
  return res;
})
.catch(err => {
  // --- errorhandler code will come here ----
  throw err;
});

Any .catch and .then attached to dynamic import will stay as it is and will not be replaced.

import('./Home.js')
.then(data => console.log(data))
.catch(err => {throw err;});

will be converted to,

Promise.resolve(import('./Home.js')
.then(data => console.log(data)) // Original then handler
.then(res => {
  // --- successhandler code will come here ----
  return res;
})
.catch(err => {throw err;})) // Original catch handler
.catch(err => {
  // --- errorhandler code will come here ----
  throw err;
});

Example 2: Skip Overriding Dynamic Imports

To skip overriding following dynamic import,

import('./Home.js');

Change it to following,

import(/* skipImportOverride: true */ './Home.js');

Contributing

Contributions are welcomed! Read the Contributing Guide for more information.

Licensing

This project is licensed under the MIT License. See LICENSE for more information.