Skip to content

Instantly share code, notes, and snippets.

@Venryx
Last active September 14, 2016 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Venryx/1386c6141a8602aca770c1a515817256 to your computer and use it in GitHub Desktop.
Save Venryx/1386c6141a8602aca770c1a515817256 to your computer and use it in GitHub Desktop.
Small webpack config change to have the ES6 for-of loop converted to an ES5 for loop

The following is a small webpack config change to have the ES6:
for (let item of collection)
Converted to the ES5:
for (let i = 0, item; i < collection.length | (item = collection[i]); i++)

Instructions

  1. Run npm install --save string-replace-webpack-plugin

  2. Add the following to your webpack.config.js file: (below the babel-loader, so that ours runs before it (yes, last runs first))

var StringReplacePlugin = require("string-replace-webpack-plugin");

//module.exports = {
//  module: {
//      loaders: [
            {
                  test: /(\.js|\.jsx)$/,
                  loader: StringReplacePlugin.replace({replacements: [
                    {
                        pattern: /for \((\w+) (\w+) of ([A-Za-z0-9_.]+)\)/g,
                        replacement: function(match, varType, varName, listName, offset, string) {
                            return `for (let i = 0, ${varName}; i < ${listName}.length | (${varName} = ${listName}[i]); i++)`;
                        }
                    }
                ]})
            }
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment