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

rebabel-webpack-plugin

v0.1.2

Published

A webpack plugin that let you re-transpile asset JS files with babel and save to another file

Downloads

3

Readme

Rebabel Webpack Plugin

A webpack plugin that let you re-transpile asset JS files with babel and save to another file.

But why?

Today the norm today is to transpile all your beautiful ES6/7 code into ES 3/5, so that your script work across all browsers and be done with it, however that is quite a waste of time and resources, as many modern browsers already support ALOT of the new features (including IE Edge). A better alternative is to serve the "good" ES 6/7 version of your code to those browsers that supports this and serve the transpiled version to browsers that don't. This minimizes file size overhead and make use of the performance benefits there are in the ES 6/7 version and generally makes your app leaner.

This all very good but it is not directly evident how you create two versions of the exact same code in your bundle with the way Webpack work today.
One solution is to run two simultaneous builds with same entry files but with different Babel setups and you are good to go. This works fine but it increases your build time by a factor of 2 (or more) as you have to run the exact same build twice, which includes transforming asset paths, processing images, compiling css, etc. etc.. All just to have the exact same JS code targeting different ES versions.

This is why I created this Plugin. It takes the compiled code files and runs it through the babel transpiler one more time (hence the name), saves the new code to a new file and update the dynamic chunk reference to this new file.

Result: Multiple versions of the same code but with different ES targets.

Usage

Include in your webpack.config.js:

// CJS
const RebabelPlugin = require('rebabel-webpack-plugin');

// ES modules
import RebabelPlugin from 'rebabel-webpack-plugin';

Add the plugin to your list of plugins:

// In your Webpack setup
{
  // ... other settings ...
  plugins: [
    new RebabelPlugin({ /* options */  })
    // ... Repeat for several versions - eg. ES 7 to ES 6 ...
  ]
}

// ES 5 transpilation example:
{
  // ... other settings ...
  plugins: [
    new RebabelPlugin({
      babel: { presets: ['es2015'] },
      prefix: 'degrated-'
    })
  ]
}

Thats it!

Worth noting

As the ReBabal runs after the initial files have been transpiled the first time, there is no need to indicate the same plugins for the ReBabel babel configuration. You only need to add the subsequent transpilation presets/plugins.

Options

  • babel: The babel setup transferred directly to the babel.transform call.
  • prefix: The prefix to append to the each asset file name (default: es5-).
  • createChunk: Should the re-transpiled assets be exposed as chunks as well.

Caveats

  • As it is right now, you have to pass the minified to the babel options if you are minifying the files, as I have found no reliable way to detect if the file is intentionally minified.

  • To expose the re-transpiled components to the stats (used by plugins like webpack-bundle-analyzer or assets-webpack-plugin) the corresponding chunk needs to be created, which is enabled by createChunk option.

Known issues

  • If you want the re-transpiled assets to appear in webpack-bundle-analyzer you need to set createChunk = true. However if you want to transpile to es5, unfortunately you cannot use the babel-preset-es2015 as it includes babel-plugin-transform-es2015-function-name that messes up the parser in webpack-bundle-analyzer, because it renames anonymous functions created by webpack to _().
    The work around is to include all the transform plugins provided by the babel-preset-es2015 except transform-es2015-function-name.