43

I'm new to webpack and I'm trying to understand loaders as well as its properties such as test, loader, include etc.

Here is a sample snippet of webpack.config.js that I found in google.

module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [
          path.resolve(__dirname, 'index.js'),
          path.resolve(__dirname, 'config.js'),
          path.resolve(__dirname, 'lib'),
          path.resolve(__dirname, 'app'),
          path.resolve(__dirname, 'src')
        ],
        exclude: [
          path.resolve(__dirname, 'test', 'test.build.js')
        ],
        cacheDirectory: true,
        query: {
          presets: ['es2015']
        }
      },
    ]
}
  1. Am I right that test: /.js$/ will be used only for files with extension .js?

  2. The loader: 'babel-loader', is the loader we install using npm

  3. The include: I have many questions on this. Am I right that anything we put inside the array will be transpiled? That means, index.js, config.js, and all *.js files in lib, app and src will be transpiled.

  4. More questions on the include: When files get transpiled, do the *.js files get concatenated into one big file?

  5. I think exclude is self explanatory. It will not get transpiled.

  6. What does query: { presets: ['es2015'] } do?

devwannabe
  • 3,160
  • 8
  • 42
  • 79
  • 7
    1. yes 2. yes 3. no: it will be transpiled only if it's `require`d/`import`ed, not explicitly 4. yes 5. yes 6. https://babeljs.io/docs/plugins/preset-es2015/ – zerkms Jan 05 '16 at 23:43
  • I got lost when you said, "it will only be transpiled if it's required or imported. What do you mean? Does it mean that if index.js contains a code that uses import, the file being referenced inside the index.js will be imported? – devwannabe Jan 05 '16 at 23:47
  • 2
    Those files will be loaded only if you import them in your entry point. If you create the empty `index.js` - only the empty `index.js` will be loaded and included into the bundle. – zerkms Jan 05 '16 at 23:48
  • The code in index.js are import $ from 'jquery'; and import React from 'react'; So those 2 files will be transpiled? I hope I understood your last reply. Thanks! – devwannabe Jan 05 '16 at 23:52
  • 1
    "will be transpiled?" --- if only they match any of `include` paths and don't match all of `exclude` paths. If `jquery` is in the `node_modules` directory - it will not be transpiled. – zerkms Jan 05 '16 at 23:52
  • ah. The thing that confuses me is that I was expecting that include will only contain directories. However, in the snippet I found, it included the 2 filenames, index.js and config.js – devwannabe Jan 05 '16 at 23:58

3 Answers3

25

In webpack config there are multiple things for configuration, the most important ones are

  • entry - can be an array or an object defining the entry point for the asset you want to bundle, can be a js as test here says do it only for /.js$. Your application if has multiple entry points use an array.

  • include - defines the set of path or files where the imported files will be transformed by the loader.

  • exclude - do not transform file from these places.

  • output - the final bundle you want to create. If you specify, for example,

    output: {
      filename: "[name].bundle.js",
      vendor: "react"
    }
    

    Then your application js files will be bundled as main.bundle.js and react in a vendor.js files. It is an error if you do not use both in html page.

Hope it helped

Community
  • 1
  • 1
sandeep
  • 2,098
  • 1
  • 10
  • 13
  • 1
    My question: if "example.js" file is imported by "entry.js" file. but this "example.js" is not included in loader's "include", will "example.js" be included in the final "bundle.js"? – derek Jun 14 '16 at 23:12
  • 1
    @derek will be included in the bundle however without any transformation since that import wasn't "included" as one that the loader should care about – cvsguimaraes Jul 05 '16 at 03:39
9

This documentation helped me understand better. Looks like it is for webpack 1 but still applies.

https://webpack.github.io/docs/configuration.html#module-loaders

Loaders

An array of automatically applied loaders.

Each item can have these properties:

  • test: A condition that must be met
  • exclude: A condition that must not be met
  • include: An array of paths or files where the imported files will be transformed by the loader
  • loader: A string of “!” separated loaders
  • loaders: An array of loaders as string

This example helped me understand what is going on. Looks like you use either include or exclude but not both. The test is a condition applied to all files. So if you include a folder, each file must pass the test condition. I have not verified this, but based on the example provided by the documentation, it look like that is how it works.

    module: {

      rules: [
        {
          // "test" is commonly used to match the file extension
          test: /\.jsx$/,

          // "include" is commonly used to match the directories
          include: [
            path.resolve(__dirname, "app/src"),
            path.resolve(__dirname, "app/test")
          ],
          // "exclude" should be used to exclude exceptions
          // try to prefer "include" when possible

          // the "loader"
          loader: "babel-loader" // or "babel" because webpack adds the '-loader' automatically
        }
      ]

    }
zechdc
  • 3,374
  • 9
  • 40
  • 52
  • this include will help to include the header.html to index.html ? like this –  Apr 30 '18 at 10:41
-2

1) Correct.

2) Correct.

3) Correct.

4) I am unsure. My webpack.config.js file includes an output key, and does bundle it all into one file:

output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js'
}

5) Correct.

6) This tells babel-loader what sort of transpile you want it to perform, as well as other compile options. So, for example, if you want it to transpile jsx as well + cache results for improve performance, you would change it to:

query: {
    presets: ['react', 'es2015'],
    cacheDirectory: true
}
Ethan Clark
  • 126
  • 6
  • I hope you can explain #3 more just like my above conversation with zerkms. Thanks! – devwannabe Jan 06 '16 at 00:00
  • (EDIT: This is totally wrong) The files listed in include are the entrypoints you want webpack to begin compiling from. Code that is not referenced by these entrypoint files will not be included in webpack's output. – Ethan Clark Jan 06 '16 at 00:02
  • "The files listed in include are the entrypoints" --- they are not. – zerkms Jan 06 '16 at 00:02
  • Yup, it's not the entry point. There is a property called entry – devwannabe Jan 06 '16 at 00:04