Socket
Book a DemoInstallSign in
Socket

babel-plugin-import-noop

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-import-noop

Transform certain types of `import` statements into noops that declare empty objects.

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
2
Created
Source

babel-plugin-import-noop

A babel plugin to turn certain types of import statements into noops that declare empty objects.

Why?

If you're using CSS Modules via a build tool like Webpack's css-loader, then when you build your app bundle webpack will magically transform imports like:

import style from './some-style.scss';

— into JavaScript that injects this style into your pages.

However, if you want to run tests, you'll hit a syntax error if you try to run the JavaScript files as-is, since the CSS they're importing isn't valid JavaScript. You could compile an entire "test bundle" with webpack before running the tests, but that adds a substantial boot time to every run. Instead, you can use the babel require hook, and use this plugin to replace all instances of:

import style from './some-style.scss';

with

const style = {};

— during compilation.

Usage

$ npm install --save-dev babel-plugin-import-noop

Then, in your .babelrc:

{
  "plugins": ["import-noop"]
}

Or, to only load this plugin during tests (i.e. not in your actual bundle):

{
  "env": {
    "test": {
      "plugins": ["import-noop"]
    }
  }
}
NODE_ENV=test mocha ....

To specify a list of file extensions which should be transformed, you can add additional options to the plugin declaration:

{
  "plugins": [
    ["import-noop", {
      "extensions": ["scss", "css"]
    }]
  ]
}

Limitations

Since the newly defined object won't have any properties, anything you're reading from it in your code (e.g. style.className) will resolve to undefined. A pull request to allow defining properties using the plugin configuration would be very useful!

License

MIT

Keywords

webpack

FAQs

Package last updated on 26 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts