- 所有的loader都是在node环境中执行的,所以要遵循CommonJS的规范
- 必须导出一个函数,否则编译的时候可能会出现 must have normal or pitch function 的错误
- loader的输入和输出都是字符串的形式 ```jsx // loaders/tpl-loader/index.js const { tplReplace } = require(‘../utils’) const { getOptions } = require(‘loader-utils’); // 获取options const { validate } = require(‘schema-utils’); // 检验options参数是否合法
// TODO: 所有的loader都是在node环境中执行的,所以要遵循CommonJS的规范
// TODO: 必须导出一个函数,否则编译的时候可能会出现 must have normal or pitch function 的错误
// TODO: loader的输入和输出都是字符串的形式
function tplLoader(source) {
source = source.replace(/\s+/g, ‘’)
return export default (options) => {
${tplReplace.toString()}
return tplReplace('${source}', options);
}
}
module.exports = tplLoader
// loaders/utils function tplReplace(template, templateObj) { return template.replace(/{{(.*?)}}/g, function (node, key) { return templateObj[key] }) }
module.exports = { tplReplace };
**webpack配置文件**- 该loader没有上传到npm,所以本地引用的时候,我们通过resolveLoader这个属性来将我们的loaders引入- 配置我们的tpl-loader,这里的loader名称和我们的文件夹名称是一致的,要不然会找不到对应的loader模块```jsx// 我们该loader会把所有的tpl文件进行一个处理,处理之后的结果再给babel-loader做解析resolveLoader: {modules: ['node_modules', resolve(__dirname, 'loaders')], // TODO: 注意点,将本地loader引入进来},module: {rules: [{test: /\.tpl$/,use: ['babel-loader',{loader: 'tpl-loader',options: {log: true}}]}]},
使用
对于我们代码中的tpl结尾的模块都会被tpl-loader编译。
// src/index.js 入口文件import hello from './hello.tpl'const app = document.querySelector('#app')const config = {name: 'hello',age: 20,}app.innerHTML = hello(config)// src/hello.tpl<div><h1>{{name}}</h1><h1>{{age}}</h1></div>
