usabilityhub / rails-erb-loader

Embedded Ruby (.erb) webpack loader for Ruby projects.
MIT License
103 stars 28 forks source link

Webpack 5 support #103

Open acallaghan opened 3 years ago

acallaghan commented 3 years ago

Hi there, So i'm testing out Webpacker 6 which uses webpack 5 - I'm having trouble initializing the loader.

module.exports = {
  test: /\.erb$/,
  enforce: "pre",
  exclude: /node_modules/,

  use: [{
    loader: "rails-erb-loader",
    options: {
      runner: (/^win/.test(process.platform) ? "ruby " : "") + "bin/rails runner",
      env: {
        ...process.env,
        DISABLE_SPRING: 1,
      },
    },
  }],
}

Which gives me this error

[webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
 - configuration has an unknown property 'use'. These properties are valid:
   object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
   -> Options object as provided by the user.
   For typos: please correct them.
   For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.
     Loaders should be updated to allow passing options via loader options in module.rules.
     Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:
     plugins: [
       new webpack.LoaderOptionsPlugin({
         // test: /\.xxx$/, // may apply this only for some modules
         options: {
           use: …
         }
       })
     ]

Any ideas how to adapt the loader? Do you need to do work to support the new webpack 5 plugin API?

rhys-vdw commented 3 years ago

Any ideas how to adapt the loader?

First step is to add webpack 5 to the test suite here:

https://github.com/usabilityhub/rails-erb-loader/blob/233db82e5742fa4e8775bdb081391a743b55d902/.travis.yml#L16-L18 https://github.com/usabilityhub/rails-erb-loader/blob/233db82e5742fa4e8775bdb081391a743b55d902/package.json#L15-L17

However, it sounds as though you're simply providing the options to rails-erb-loader is an old way that is not longer valid:

configuration has an unknown property 'use'.

Additionally it sounds like the API for reading options may have changed:

For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration. Loaders should be updated to allow passing options via loader options in module.rules.

Note that in making required changes (if any) we will also need to continue to support older versions of webpack.

Additionally for this change to be complete we will need to update the README to show the current recommended way to configure webpack 5.

Do you need to do work to support the new webpack 5 plugin API?

I myself will not be making this change, but will happily accept a PR that does so, provided the above is included, or explanation is given as to why it was not required.

PikachuEXE commented 3 years ago

Webpacker 6 has already included a default rule for .erb files with rails-erb-loader as loader (When the module rails-erb-loader can be resolved)

acallaghan commented 3 years ago

@PikachuEXE Ah this is correct, and yes it 'just works' in webpacker 6 without any user config at all - thank you very much.

rhys-vdw commented 3 years ago

Reopening until we can confirm compatibility with webpack 5.