Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack separate loader chains for same filetype

Is is possible to run two separate loader chains for the same extension?

In my case, I want to run one set of loaders to build a static file, and another to write a different set of files (for server side rendering)

  {
    test: /\.p?css$/,
    use: ExtractTextPlugin.extract([ "css-loader", "postcss-loader" ]),
  },
  {
    test: /\.p?css$/,
    use: [  
      {
        loader: "emit-file-loader",
        options: {
          name: "dist/[path][name].pcss",
        },
      },
      {
        loader: "skeleton-loader",
        options: {
          procedure: function (content) {
            return `module.exports = ${content}`
          },
        },
      },
      "postcss-loader",
    ]),
  }

But according to What is the loader order for webpack? it seems to run all loaders in the same chain, even if they're defined in separate rules.

Maybe I'm not understanding loaders fully, but is there a way to have each set of loaders (the use list) run independently?

like image 949
Craig Kochis Avatar asked Jul 14 '17 23:07

Craig Kochis


People also ask

How do Webpacks handle pictures?

Essentially, there is not much in Webpack to include your desired images for your web application. First, put your image files into one folder of your projects application. For instance, your src/ folder may have a folder assets/ which has a folder images/.

Can webpack bundle images?

Developers can customize image bundling using Webpack, which provides asset modules for using files (containing the fonts and icons). This is done without configuring additional loaders like url-loader and file-loader.

How does webpack file loader work?

Webpack goes through all the import ed and require d files in your project, and for all those files which have a . svg extension, it uses as an input to the webpack file loader. For each of these files, the file loader emits the file in the output directory and resolves the correct URL to be referenced.


1 Answers

I found a loader which is best suited for your case multi loader. This loader requires a module multiple times, each time loaded with a different loader. Like in a multi entry point the exports of the last item are exported.

var multi = require("multi-loader");
{
    module: {
        loaders: [
            {
                test: /\.css$/,
                // Add CSS to the DOM
                // and
                // Return the raw content
                loader: multi(
                    "style-loader!css-loader!autoprefixer-loader",
                    "raw-loader"
                )
            }
        ]
    }
}

or you can require same file twice using inline loaders at require statement like this

import Styles from 'style-loader!css-loader?modules!./styles.css';
import Styles from 'xyz-loader!sass-loader?modules!./styles.css';

and you can pass the configration option as query.

Also you can make alias for loader-name and configration and use that insted of writing loader-name and configration every time like this

in config file

resolveLoader: {
  alias: {
    myLoader1: "style-loader!css-loader?modules", // and much more
    myLoader2: "xyz-loader!sass-loader?modules" // and much more
  }
}

at import

import Styles from 'myLoader1!./styles.css';
import Styles from 'myLoader2!./styles.css';
like image 178
Tripurari Shankar Avatar answered Oct 08 '22 21:10

Tripurari Shankar