[Webpack] to use in resolveLoader

Introduced in several ways loader

 

The first is written by the installation of the package loader npm

  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  },

Write directly loader can find the name of the corresponding loader 

 

If you write your own loader, you can use an absolute path way

  module: {
    rules: [
      {
        test: /\.js$/,
        use: path.resolve(__dirname, 'loaders/a.js'),
        exclude: /node_modules/
      }
    ]
  },

My loader a.js file loaders files in the current directory folder

 

If the above way you are not satisfied, then you can configure the way the alias

  resolveLoader: {
    alias: {
      'a-loader': path.resolve(__dirname, 'loaders/a.js')
    }
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'a-loader',
        exclude: /node_modules/
      }
    ]
  },

 

If you still are not satisfied, then it is defined by finding a way loader, loader by default looks at node_modules, you can specify a different directory to find our custom loader

  resolveLoader: {
    modules: [path.resolve(__dirname, 'node_modules'),path.resolve(__dirname, 'loader')]
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'a',
        exclude: /node_modules/
      }
    ]
  },

 

Guess you like

Origin www.cnblogs.com/wuxianqiang/p/11332544.html