面试题答案
一键面试1. 编写Loader函数
- 创建Loader文件:
在项目中创建一个新的JavaScript文件,例如
string-replace-loader.js
。 - 导出Loader函数:
module.exports = function (source) {
// 同步Loader
const replacedSource = source.replace('特定字符串', '替换内容');
return replacedSource;
};
对于异步Loader,假设替换操作是异步的(实际字符串替换通常同步,但为演示异步情况):
module.exports = function (source) {
const callback = this.async();
setTimeout(() => {
const replacedSource = source.replace('特定字符串', '替换内容');
callback(null, replacedSource);
}, 1000);
};
2. 处理异步操作
- 使用
this.async()
: 在Loader函数内部调用this.async()
,它会返回一个回调函数。异步操作完成后,通过这个回调函数传递错误(如果有)和处理后的结果。如上述异步示例中,在setTimeout
中调用callback(null, replacedSource)
,第一个参数为错误(这里设为null
表示无错误),第二个参数为处理后的源代码。 - 处理异步依赖:
如果Loader依赖其他异步操作,比如读取外部文件或调用API,可以使用
Promise
或async/await
结合this.async()
。例如:
module.exports = async function (source) {
const callback = this.async();
try {
const externalData = await someAsyncFunction();
const replacedSource = source.replace('特定字符串', externalData);
callback(null, replacedSource);
} catch (error) {
callback(error);
}
};
3. 进行测试
- 创建测试文件:
在项目的测试目录(如
test
或__tests__
)下创建一个测试文件,例如string-replace-loader.test.js
。 - 使用测试框架:
可以使用
jest
或mocha
等测试框架。以jest
为例:
const path = require('path');
const loaderUtils = require('loader-utils');
const stringReplaceLoader = require('../string-replace-loader');
describe('String Replace Loader', () => {
it('should replace specific string', () => {
const source = '这是特定字符串';
const callback = () => {};
const options = {};
const loaderContext = {
async() {
return callback;
},
getOptions() {
return options;
}
};
const spy = jest.spyOn(loaderContext, 'async');
stringReplaceLoader.call(loaderContext, source);
expect(spy).toHaveBeenCalled();
expect(callback.mock.calls[0][1]).toBe('这是替换内容');
});
});
- 模拟Loader上下文:
在测试中需要模拟Loader的上下文,包括
this.async()
方法和getOptions()
方法等,以便正确调用Loader函数并验证其行为。 - 运行测试:
使用测试框架提供的命令运行测试,如
jest
直接在项目根目录执行npm test
或yarn test
(假设已配置好测试脚本)。