如何写webpack loader帮助你升职加薪

简单来说loader是让其他类型的文件转换成webpack能理解的js代码的一段代码(函数)

Out of the box, webpack only understands JavaScript files. Loaders allow webpack to process other types of files and converting them into valid modules that can be consumed by your application and added to the dependency graph.、

在你的应用程序中,有三种使用 loader 的方式:
  • 配置(推荐):在 webpack.config.js 文件中指定 loader。
  • 内联:在每个 import 语句中显式指定 loader。
  • CLI:在 shell 命令中指定它们。

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          { loader: 'style-loader' },
          {
            loader: 'css-loader',
            options: {
              modules: true
            }
          }
        ]
      }
    ]
  }

内联

1
import Styles from 'style-loader!css-loader?modules!./styles.css';
选项可以传递查询参数,例如 ?key=value&foo=bar,或者一个 JSON 对象,例如 ?{"key":"value","foo":"bar"}。

CLI

1
webpack --module-bind jade-loader --module-bind 'css=style-loader!css-loader'
会对 .jade 文件使用 jade-loader,对 .css 文件使用 style-loader 和 css-loader。

 
那么如何编写一个webpack-loader呢?官方指南,下面就带你一起手写一个webpack-loader?
需求如下:
我们要写一个对txt文件中的[name]替换成17,非常简单.如下:
1
2
3
4
5
6
7
8
9
10
//src/loader.js
const {getOptions} = require('loader-utils')
 
module.exports = function (source){
    const options = getOptions(this);
    console.log(source);
    source = source.replace(/\[name\]/g, options.name);
    console.log(source);
    return `export default ${JSON.stringify(source)}`
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//webpack.config.js配置
 
const path = require('path')
 
module.exports = {
    mode:'development',
    context: __dirname,
    entry: `./src/test.txt`,
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.txt'
    },
    module: {
        rules: [
            {
                test: /\.txt$/,
                use: [
                    {
                        loader: path.resolve(__dirname, './src/bar-loader.js'),
                        options: {
                            name: '18hahaha'
                        }
                    },
                    {
                        loader: path.resolve(__dirname, './src/loader.js'),
                        options: {
                            name: '17'
                        }
                    }
                ]
            }
        ]
    }
}

那么如何编写一个loader与现有的loader一起使用呢?

接着写:
1
2
3
4
5
6
7
8
9
10
11
//src/bar-loader.js
 
const { getOptions } = require('loader-utils')
 
module.exports = function (source) {
    const options = getOptions(this);
    console.log(11111,source);
    source = source.replace(/17/g, options.name);
    console.log(11111, source);
    return `export default ${JSON.stringify(source)}`
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//webpack.config.js
rules: [
            {
                test: /\.txt$/,
                use: [
                    {
                        loader: path.resolve(__dirname, './src/bar-loader.js'),
                        options: {
                            name: '18hahaha'
                        }
                    },
                    {
                        loader: path.resolve(__dirname, './src/loader.js'),
                        options: {
                            name: '17'
                        }
                    }
                ]
            }
        ]
还可以使用异步模式(async mode)
调用 this.async()来获取this.callback()方法,然后在异步调用的回调函数中通过callback返回null以及处理结果。

1
2
3
4
5
6
7
8
module.exports = function(content) {
    var callback = this.async();
    if(!callback) return someSyncOperation(content);
    someAsyncOperation(content, function(err, result) {
        if(err) return callback(err);
        callback(null, result);
    });
};
那么如何编写一个loader的单元测试呢?OK.直接上代码
编写一个compiler.js

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import path from 'path'
import webpack from 'webpack'
import memoryfs from 'memory-fs'
 
export default (fixture, options = {}) => {
    const compiler = webpack({
        context: __dirname,
        entry: `./${fixture}`,
        output: {
            path: path.resolve(__dirname),
            filename: 'bundle.js'
        },
        module:{
            rules:[
                {
                    test: /\.txt$/,
                    use: {
                        loader: path.resolve(__dirname, '../loaders/loader.js'),
                        options: {
                            name: '17'
                        }
                    }
                }
            ]
        }
    })
 
    compiler.outputFileSystem = new memoryfs();
    return new Promise((resolve, reject) => {
        compiler.run((err, stats) => {
            if(err) reject(err)
            resolve(stats)
        })
    })
}
这样就可以测试了

1
2
3
4
5
6
7
8
9
10
import compiler from './compiler.js';
 
test('Inserts name and outputs JavaScript', async () => {
    const stats = await compiler('example.txt');
    // console.log(stats.toJson());
 
    const output = stats.toJson().modules[0].source;
 
    expect(output).toBe(`export default "Hey 17!\\n"`);
});

  

好了,demo写完了,剩下就是根据需求编写了.
最后奉上loader API、官网的loaders.

 

如果觉得文章不错,可以给小编发个红包给予鼓励.

  

posted @   地铁程序员  阅读(723)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示