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-webpack-loaders

v0.9.0

Published

babel 6 plugin which allows to use webpack loaders

Downloads

4,338

Readme

Travis CI Appveyor

babel-plugin-webpack-loaders

Important note!!!

Since plugin was published, there were a lot of changes in testing software. Be sure in most(all) cases you DON'T need this plugin for testing. I highly recommend you to use jest for testing, and use moduleNameMapper (identity-obj-proxy, etc) to mock CSS-Modules and other webpack loaders.

Begin

This Babel 6 plugin allows you to use webpack loaders in Babel. It's now easy to run universal apps on the server without additional build steps, to create libraries as usual with babel src --out-dir lib command, to run tests without mocking-prebuilding source code. It just replaces require - import statements with webpack loaders results. Take a look at this Babel build output diff to get the idea.

For now this plugin is of alpha quality and tested on webpack loaders I use in my projects. These loaders are file-loader, url-loader, css-loader, style-loader, sass-loader, postcss-loader. The plugin supports all webpack features like loaders chaining, webpack plugins, and all loaders params. It's easy because this plugin just uses webpack.

Three examples:

Warning

Do not run this plugin as part of a webpack frontend configuration. This plugin is intended only for backend compilation.

How it works

Take a look at this minimal-example

  • You need to create a webpack config

  • You need to add these lines to .babelrc

  • Now you can run example.js

    // example.js
    import css from './example.css';
    console.log('css-modules result:', css);

    with the command BABEL_DISABLE_CACHE=1 NODE_ENV=EXAMPLE ./node_modules/.bin/babel-node ./example.js and you'll get the following console output:

    css-modules result: { main: 'example__main--zYOjd', item: 'example__item--W9XoN' }

Here I placed output diff of this babel library build without and with the plugin. As you can see the plugin just replaces require with loaders results. All loaders and plugins have been applied to generated assets

Install

npm install --save-dev babel-cli babel-plugin-webpack-loaders

Examples

webpack configs, examples, .babelrc example, tests, minimal-example-repo

You can try out the examples by cloning this repo and running the following commands:

npm install
# example above
npm run example-run
# library example - build library with a lot of modules
npm run example-build
# and now you can use your library using just node
node build/myCoolLibrary/myCoolLibrary.js
# test sources are also good examples
npm run test

Why

The source of inspiration for this plugin was babel-plugin-css-modules-transform, but it was missing some features I wanted:

  • I love writing CSS using Sass
  • I like webpack and its loaders (chaining, plugins, settings)
  • I wanted to open source a UI library which heavily used CSS Modules, Sass and other webpack loaders. The library consisted of many small modules and every module needed to be available to users independently such as lodash/blabla/blublu. With this plugin the heavy build file for the library could be replaced with just one command: babel src --out-dir lib.

How the plugin works internally

The plugin tests all require paths with test regexps from the loaders in the webpack config, and then for each successful test:

  1. synchronously executes webpack

  2. parses webpack output using babel-parse

  3. replaces the required ast with the parsed ast output

Caching issues

By default Babel caches compiled files, so any changes in files processed with loaders will not be visible at subsequent builds, you need to run all commands with a BABEL_DISABLE_CACHE=1 prefix.

(More information: #T1186, #36)

Dynamic config path

It's possible to interpolate env vars into the WebPack config path defined in your .babelrc using lodash.template syntax. This is mainly to achieve compatibility with ava.

The ava test runner runs each spec relative to its enclosing folder in a new process which hampers this plugins ability to use a relative path for the WebPack config. An absolute path to the WebPack config will work however, and you can set one in your .babelrc using an env var like this,

{
  "presets": ["es2015"],
  "env": {
    "AVA": {
      "plugins": [
        [
          "babel-plugin-webpack-loaders",
          {
            "config": "${CONFIG}",
            "verbose": false
          }
        ]
      ]
    }
  }
}

And then invoke ava something like this,

CONFIG=$(pwd)/webpack.config.ava.js BABEL_DISABLE_CACHE=1 NODE_ENV=AVA ava --require babel-register src/**/*test.js

(More information: #41)

Thanks to

Felix Kling and his astexplorer

James Kyle and his babel-plugin-handbook

Michal Kvasničák and his babel-plugin-css-modules-transform