webpack-contrib / transform-loader

transform loader for webpack
MIT License
110 stars 23 forks source link

What does the configured LoaderOptionsPlugin do in the webpack2 configuration? #20

Closed nicholascloud closed 7 years ago

nicholascloud commented 7 years ago

What does this actually do, and why do I need it in my configuration?

new webpack.LoaderOptionsPlugin({
      options: {
        transforms: [
          function(file) {
            return through(function(buf) {
              this.queue(buf.split("").map(function(s) {
                return String.fromCharCode(127-s.charCodeAt(0));
              }).join(""));
            }, function() { this.queue(null); });
          }
        ]
      }
    })
michael-ciniawsky commented 7 years ago

@nicholascloud The LoaderOptionsPlugin was mainly necessary in the webpack 1 => webpack 2 transition phase, bc since webpack 2 it's disallowed to have custom props on the webpack.config.js {Object} directly, in this case transforms. I can't give you a guarantee for this 😛, but normally this should live in module.rules[i].options now.

webpack.config.js

{
   loader: 'transform-loader',
   options: {
      transforms: [
          (file) => {
            return through((buffer) => {
               return this.queue(
                 buffer
                    .split('')
                    .map((chunk) => String.fromCharCode(127-chunk.charCodeAt(0))))
                    .join('')
            }), 
            () => this.queue(null)
         } 
      ]
   }
}
nicholascloud commented 7 years ago

@michael-ciniawsky Awesome, thanks for the clarification.

michael-ciniawsky commented 7 years ago

@nicholascloud if you are using this loader, could you please verify 😁 😛 ?

nicholascloud commented 7 years ago

@michael-ciniawsky Here is my configuration, and everything works great:

// Transform Node.js fs lib calls to be inline strings.
// @see: https://github.com/webpack-contrib/transform-loader
{
    test: /\.js$/,
    exclude: /node_modules|vendor/,
    loader: 'transform-loader/cacheable',
    enforce: 'post',
    options: {
        brfs: true,
        // @see: https://github.com/webpack-contrib/transform-loader#webpack-2x-config-example
        // @see: https://github.com/webpack-contrib/transform-loader/issues/20
        transforms: [
            function (/*file*/) {
                return through((buffer) => {
                    return this.queue(
                        buffer.split('')
                            .map((chunk) => String.fromCharCode(127-chunk.charCodeAt(0))))
                            .join('')
                }, () => this.queue(null))
            }
        ]
    }
}
michael-ciniawsky commented 7 years ago

@nicholascloud Thx 👍 Mind sending a quick PR with docs update :D ? Otherwise I do it later 🙃

nicholascloud commented 7 years ago

@michael-ciniawsky For the README file? Yeah, I'll set up a PR.

michael-ciniawsky commented 7 years ago

Yep, 💯 thx in advance 👍

nicholascloud commented 7 years ago

Related PR: https://github.com/webpack-contrib/transform-loader/pull/21