emaphp / underscore-template-loader

A Underscore and Lodash template loader for Webpack
MIT License
104 stars 24 forks source link

Do custom macros still work in webpack 2? #27

Closed jamesehly closed 6 years ago

jamesehly commented 6 years ago

Webpack 2 complains about the macros option being in the configuration where the documentation says it should be

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration has an unknown property 'macros'.

If I place it in the loader options, then the macros don't seem to get loaded:

use: [
  {
    loader: 'underscore-template-loader',
    options: {
      macros: {
        test: function() { return "'test'" }
      },
      parseMacros: true
    }
  }
]

So can someone show me an example that works with webpack 2?

jamesehly commented 6 years ago

I read some more on migrating webpack to 2.0 and it seems that they worked in a workaround for this issue (https://webpack.js.org/guides/migrating/#loaderoptionsplugin-context). You can use the LoaderOptionsPlugin to replicate how the config used to work. Adding this fixed my issue:

plugins: [
        ...
        new webpack.LoaderOptionsPlugin({
            options: {
                macros: {
                    header: function() { return "'...'"},
                    footer:  function() { return "'...'"}
                }
            }
        })
        ...
    ],