undefined
前言
Webpack通过webpack/schema-utils实现Loader参数校验
下载依赖
1
| npm install -D schema-utils
|
定义Schema
schemas/schema.json1 2 3 4 5 6 7 8 9
| { "type": "object", "properties": { "key": { "type": "string", "description": "key的类型应当为string" } } }
|
创建Loader
loaders/loader.js1 2 3 4 5 6 7 8
| const { validate } = require("schema-utils"); const schema = require("../schemas/schema.json");
module.exports = function (source) { const options = this.getOptions(); validate(schema, options); return source; };
|
使用自定义Loader
webpack.config.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| module.exports = { module: { rules: [ { test: /\.js$/, use: [ { loader: "./loaders/loader.js", options: { key: "value" } } ] } ] } };
|
完成