question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Process the same module multiple times with separate rules/loaders

See original GitHub issue

Do you want to request a feature or report a bug? Feature

What is the current behavior? If multiple rules match a module, I believe either the loaders are applied serially or only loaders from the first-matched rule processes the file.

What is the expected behavior? I’d like to be able to process the same module multiple times, possibly via separate rules. This doesn’t necessarily need to be asynchronous.

There was a loader that helped with this once, but it no longer appears relevant in webpack 2/3: https://github.com/webpack-contrib/multi-loader; for loaders like ExtractTextPlugin, no file is output.

One implementation of this might be allowing us to configure a rule to only apply for certain entries; e.g.

config = {
  entry: { main: 'index.js', vendor: 'vendor.js' },
  module: { rules: [ { test: (file, entry) => file === 'test.css' && entry === 'main', use: [ ... ] } ] }
} 

Or perhaps we can just specify a rule to apply in “parallel”, for any overlapping rules.

If this is a feature request, what is motivation or use case for changing the behavior? Lots of use cases for this; one might be to process a stylesheet in different ways, and saving them as separate files via ExtractTextPlugin.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:32
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
NickCiscommented, Apr 29, 2018

I’ve sorted a way in order to do this, full explanation is on this gist.

tl;dr;

Duplicating imports (as multi-loader does), inlining loader configuration with ident and adding (in a hackish way) loader configuration to the RuleSet’s references dictionary.

6reactions
epongcommented, Aug 15, 2017

I believe multi-loader simply takes loaders as strings and adds additional requires to modules.export (and doesn’t support loaders specified as objects). So if you have:

import 'file.scss';

And there’s a rule with multi-loader setup, it would transform this into:

require('!!first-loader!file.scss');
require('!!second-loader!file.scss');

This works for many loaders, but it seems to fail for something like ExtractTextPlugin because, I think, the ExtractTextPlugin instance is lost when having to use something like combine-loaders to feed it into multi-loader. This can and probably should be addressed by multi-loader.

But what I’m requesting here is a bit more general. Essentially:

  1. Have webpack support more than 1 matching rule, where each rule processes the module independently
  2. Make a rule’s test more powerful by adding some additional information about the module, like the entry from which it’s sourced

This might not be pertinent to vanilla modules, but it’s useful for importing resources/assets. So, a more complete example might be:

config = {
  entry: {
    main: path.resolve('src/index.js'),
    vendor: path.resolve('src/vendor.js')
  }
  module: {
    rules: [
    ...
      { test: (filename, entry) => filename.match(/\.png$/) !== null && entry == 'main',
        loader: 'file-loader', ... },
      { test: (filename, entry) => filename.match(/\.png$/) !== null && entry == 'vendor',
        loader: 'url-loader', ... },
    ...
      { test: /\/.png$/,
        loader: 'file-loader', { outputPath: 'cdn' } },
    ]
  }
}

In this case, png includes from index.js are copied out as files, whereas those in vendor.js are inlined. This example also shows the same module matching multiple rules, such that png imports will also always create an additional file in ./cdn. And either the first matching rule would provide what’s returned on the original import or an order/preference could be specified by the rule itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loaders - webpack
Loaders are transformations that are applied to the source code of a module. They allow you to pre-process files as you import or...
Read more >
Cannot load the same module, in the same process, multiple ...
I'm trying to import mymodule.py in the constructor of C class in order each new instance of the C class to use its...
Read more >
Rule Types and Configuration Options - ElastAlert
aggregation : This option allows you to aggregate multiple matches together into one alert. Every time a match is found, ElastAlert will wait...
Read more >
for_each to call terraform module multiple times - Terragrunt
Another use case is in achieving compliance with the CIS benchmark for AWS. One of the requirements in the benchmark is that you...
Read more >
Module Sources | Terraform - HashiCorp Developer
The module installer supports installation from a number of different source types. ... For a private registry within Terraform Cloud, use the same...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found