shakacode / shakapacker

Use Webpack to manage app-like JavaScript modules in Rails
MIT License
400 stars 89 forks source link

Add a Regex Equality Test Function #453

Closed Judahmeek closed 3 months ago

Judahmeek commented 3 months ago

In recent client work, I've been using this function (which I borrowed from StackOverflow here) to isolate rules for a specific module type (like CSS or SVG files) by using it like so:

webpackConfig.module.rules.forEach((rule) => {
  if (regexAreTheSame(rule.test, /\.(scss|sass)(\.erb)?$/i)) {
    const sassDefaultLoaders = rule.use
    // resolve-url-loader needs sourcemaps from sass-loader
    rule.use.forEach((item) => {
      if (item.loader?.includes('sass-loader')) {
        item.options = {
          sourceMap: true,
        }
      }
      if (item.loader?.includes('/css-loader')) {
        item.options = {
          sourceMap: true,
          importLoaders: 2,
          modules: true
        }
      }
    })
  }
})

While creating a function to find & apply options to a specific loader would be possible, it has proven complicated, would require several parameters, & wouldn't resolve use cases such as users seeking to remove or add loaders.

That said, I think that the regex equality test function itself would be useful to enough users to make it worthwhile to include into Shakapacker's utils (I've used it for multiple clients).

G-Rath commented 3 months ago

Is this something really worth shipping to everyone that uses Shakapacker? I think it's worth documenting but all the other utils are used somewhere from what I can tell...

Keeping in mind once shipped it'll be something we have to maintain and do a new major to remove.

Judahmeek commented 3 months ago

@G-Rath your concern is the reason I asked for a review of this PR instead of just merging it.

Actually, rule.test.test('.svg') from https://github.com/shakacode/shakapacker#webpack-configuration (All the way at the bottom of that section) proved to be nearly as effective, so I'm going to close this PR.

Thank you both for your input.