Today, I saw a piece of code using the glob module when I was working on a project written by my boss, so I wanted to record his basic usage.

1 – glob is introduced

1.1 the NPM’s official website:

1.2- Description: Node’s glob module allows you to write a glob rule using * and other symbols, just like in the shell, to get the file that matches the rule. It uses the Minimatch library for matching. In general, we can use it to match some files in the directory we want, and it should be useful to do some configuration in large projects, such as when we configure webPack entry files when there are too many entry files.

2- Usage steps

2.1- Download package:

npm i glob

2.2- Introduce in the files used:

var glob =require("glob")

2.3- A section in a path can be represented by the following characters, each of which has its own purpose:

  1. * : matches 0 or more characters in the path segment:

    Glob ("js/*.js",function (er, files) {console.log(files)}) glob("js/*.js",function (er, files) {console.log(files)})
  2. ? : matches 1 arbitrary character in the path segment:

    // Get all js. glob("js/? .js",function (er, files) { console.log(files) })
  3. […]. : matches characters in the specified range in this path segment:

    Glob ("js/a[0-3].js",function (er)) glob("js/a[0-3].js",function (er)) files) { console.log(files) })
  4. * (pattern | pattern | pattern) : match zero or more multiple models in brackets, or any combination, before and after the note | cannot have Spaces

    / / js directory a. s, a1. Js, b.j s, or a, a1, b that the combination of a few characters of js, such as ab. Js glob (" js / * (a | b | a1). Js ", function (er, files) {the console. The log files ()})

    Clause not records one by one, the more specific can consult article:…

3- Use Glob to quickly configure webPack entry files

All of the previous methods are asynchronous, passing in a callback that is executed when a matching file is obtained. Var files = glob.sync(pattern, [options])

This project is the Egg-React-SSR server rendering framework. You can see our several pages as follows, so we need to configure webpack multiple entries for the corresponding pages

Specific steps:

Next we can print the entry object:

Now we just pass our entry object directly into the WebPack configuration

Chinese document

Glob Chinese documents