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

@joeyparrish/karma-babel-preprocessor

v8.2.0

Published

Preprocessor to compile ES6 on the fly with babel.

Downloads

8,610

Readme

build status npm version npm downloads

karma-babel-preprocessor

Preprocessor to compile ES6 on the fly with babel.

This fork of the original adds caching. See cachePath below.

babel and karma-babel-preprocessor only convert ES6 modules to CommonJS/AMD/SystemJS/UMD. If you choose CommonJS, you still need to resolve and concatenate CommonJS modules on your own. We recommend karma-browserify + babelify or webpack + babel-loader in such cases.

Installation

With babel 7.x:

npm install karma-babel-preprocessor @babel/core @babel/preset-env --save-dev

With babel 6.x:

npm install karma-babel-preprocessor@7 babel-core babel-preset-env --save-dev

As of Babel 6.0, you need to tell Babel which features to use. @babel/preset-env would be the most common one.

Configuration

See babel options for more details.

Given options properties are passed to babel.

In addition to the options property, you can configure any babel options with function properties. This is useful when you want to give different babel options from file to file.

For example, inline sourcemap configuration would look like the following.

module.exports = function (config) {
  config.set({
    preprocessors: {
      'src/**/*.js': ['babel'],
      'test/**/*.js': ['babel']
    },
    babelPreprocessor: {
      options: {
        presets: ['@babel/preset-env'],
        sourceMap: 'inline'
      },
      filename: function (file) {
        return file.originalPath.replace(/\.js$/, '.es5.js');
      },
      sourceFileName: function (file) {
        return file.originalPath;
      }
    }
  });
};

Caching

Adding the field cachePath will enable caching. Cached output from babel will be stored into that file, along with md5 hashes of the original contents of each file. If an original file hasn't changed, the cached babel-processed results will be reused.

module.exports = function (config) {
  config.set({
    preprocessors: {
      'src/**/*.js': ['babel'],
      'test/**/*.js': ['babel']
    },
    babelPreprocessor: {
      cachePath: '.babel-cache',
      options: {
        presets: ['@babel/preset-env'],
        sourceMap: 'inline'
      },
    }
  });
};

Don't preprocess third-party libraries

Third-party libraries may not work properly if you apply karma-babel-preprocessor to them. It also introduces unnecessary overhead. Make sure to explicitly specify files that you want to preprocess.

OK:

module.exports = function (config) {
  config.set({
    preprocessors: {
      'src/**/*.js': ['babel'],
      'test/**/*.js': ['babel']
    },
    // ...
  });
};

NG:

module.exports = function (config) {
  config.set({
    preprocessors: {
      './**/*.js': ['babel']
    },
    // ...
  });
};

Because it preprocesses files in node_modules and may break third-party libraries like jasmine #18.

Polyfill

If you need polyfill, make sure to include it in files.

npm install @babel/polyfill --save-dev
module.exports = function (config) {
  config.set({
    files: [
      'node_modules/@babel/polyfill/dist/polyfill.js',
      // ...
    ],
    // ...
  });
});

Karma's plugins option

In most cases, you don't need to explicitly specify plugins option. By default, Karma loads all sibling NPM modules which have a name starting with karma-*. If need to do so for some reason, make sure to include 'karma-babel-preprocessor' in it.

module.exports = function (config) {
  config.set({
    plugins: [
     'karma-jasmine',
     'karma-chrome-launcher',
     'karma-babel-preprocessor'
    ],
    // ...
  });
};

Custom preprocessor

karma-babel-preprocessor supports custom preprocessor. Set base: 'babel' in addition to normal preprocessor config.

module.exports = function (config) {
  config.set({
    preprocessors: {
      'src/**/*.js': ['babelSourceMap'],
      'test/**/*.js': ['babelSourceMap']
    },
    customPreprocessors: {
      babelSourceMap: {
        base: 'babel',
        options: {
          presets: ['@babel/preset-env'],
          sourceMap: 'inline'
        },
        filename: function (file) {
          return file.originalPath.replace(/\.js$/, '.es5.js');
        },
        sourceFileName: function (file) {
          return file.originalPath;
        }
      },
      // Other custom preprocessors...
    }
  });
};