Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see which webpack loaders are used for which files?

Tags:

webpack

Is there a way to make webpack display the exact loader used for each module?

I want to verify that babel-loader is being used for certain modules. How can I validate that my module.rules[].test regular expressions are working as I intend?

The --verbose option does not display loader info.

like image 889
Charles Holbrow Avatar asked Jul 18 '17 16:07

Charles Holbrow


People also ask

Where are webpack files located?

Webpack will generate the files and put them in the /dist folder for you, but it doesn't keep track of which files are actually in use by your project. In general it's good practice to clean the /dist folder before each build, so that only used files will be generated.

What are webpack loaders used for?

They allow you to pre-process files as you import or “load” them. Thus, loaders are kind of like “tasks” in other build tools and provide a powerful way to handle front-end build steps. Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs.

Can webpack process image files?

Using two Webpack plugins, we can import images directly into our JavaScript files, and process those images at build time (compress them) to make our website load faster and have automatic cache busting.


1 Answers

First, run webpack with --profile and --json options and write the output to local disk.

webpack --profile --json > stat.json

Second, open the stat.json file. Search the filename you are interested in. I'll use a scss file for example, following is what I found in stat.json:

    {
      "id": 1,
      "identifier": "D:\\tmp\\webpack-es6-sass-setup\\node_modules\\extract-text-webpack-plugin\\dist\\loade r.js??ref--1-0!D:\\tmp\\webpack-es6-sass-setup\\node_modules\\style-loader\\index.js!D:\\tmp\\webpack-es6-sass-s etup\\node_modules\\css-loader\\index.js!D:\\tmp\\webpack-es6-sass-setup\\node_modules\\postcss-loader\\lib\\ind ex.js!D:\\tmp\\webpack-es6-sass-setup\\node_modules\\sass-loader\\lib\\loader.js!D:\\tmp\\webpack-es6-sass-setup \\src\\main.scss",
      "name": "./src/main.scss",
      "index": 1,
      "index2": 4,
      "size": 41,
      "cacheable": true,
      "built": true,
      "optional": false,
      "prefetched": false,
      "chunks": [
        0
      ],
      "assets": [],
      "issuer": "D:\\tmp\\webpack-es6-sass-setup\\node_modules\\babel-loader\\lib\\index.js!D:\\tmp\\webpack -es6-sass-setup\\src\\main.js",
      "issuerId": 0,
      "issuerName": "./src/main.js",
      "profile": {
        "factory": 11,
        "building": 38
      },
      "failed": false,
      "errors": 0,
      "warnings": 0,
      "reasons": [
        {
          "moduleId": 0,
          "moduleIdentifier": "D:\\tmp\\webpack-es6-sass-setup\\node_modules\\babel-loader\\lib\\index.js!D: \\tmp\\webpack-es6-sass-setup\\src\\main.js",
          "module": "./src/main.js",
          "moduleName": "./src/main.js",
          "type": "cjs require",
          "userRequest": "./main.scss",
          "loc": "3:0-22"
        }
      ],
      "usedExports": true,
      "providedExports": null,
      "optimizationBailout": [],
      "depth": 1,
      "source": "// removed by extract-text-webpack-plugin"
    }

Note the identifier field. Split it by !, we got:

D:\\tmp\\webpack-es6-sass-setup\\node_modules\\extract-text-webpack-plugin\\dist\\loade r.js??ref--1-0
D:\\tmp\\webpack-es6-sass-setup\\node_modules\\style-loader\\index.js
D:\\tmp\\webpack-es6-sass-s etup\\node_modules\\css-loader\\index.js
D:\\tmp\\webpack-es6-sass-setup\\node_modules\\postcss-loader\\lib\\ind ex.js
D:\\tmp\\webpack-es6-sass-setup\\node_modules\\sass-loader\\lib\\loader.js
D:\\tmp\\webpack-es6-sass-setup \\src\\main.scss

and this is all the loaders webpack used to bundle this file.

like image 98
loveky Avatar answered Oct 02 '22 13:10

loveky