前言

ejs在默认情况下只是一个呈现工具,只是负责依据传入的参数进行模板渲染。然而,在使用过程中(hexo-renderer-ejs),我希望在模板中使用require引入外部模块,使用__dirname__filename变量,即使这个用法是不推荐的

做法

通过修改ejs的源码来实现支持,我使用的版本是ejs@^2.6.1。
打开node_modules/ejs/lib/ejs.js文件,在开头添加:

47 var fs = require('fs');
48 var path = require('path');
49 var utils = require('./utils');
50 + var { Module, createRequireFromPath } = require('module')

找到Template.prototype中的compile函数,修改其中的returnedFn

    675 var returnedFn = opts.client ? fn : function anonymous(data) {
      - var include = function (path, includeData) {
      + var include = function (_path, includeData) {
        var d = utils.shallowCopy({}, data);
        if (includeData) {
          d = utils.shallowCopy(d, includeData);
        }
        + const customModule = new Module()
        + d.__filename = getIncludePath(_path, opts)
        + customModule.id = d.__dirname = path.dirname(d.__filename)
        + d.module = customModule
        + d.require = createRequireFromPath(d.__filename)
        - return includeFile(path, opts)(d);
        + return includeFile(_path, opts)(d);
      };
      + const customModule = new Module()
      + customModule.id = customModule.path = data.__dirname = path.dirname(opts.filename)
      + data.module = customModule
      + data.__filename = opts.filename
      + data.require = createRequireFromPath(data.__filename)
      return fn.apply(opts.context, [data || {}, escapeFn, include, rethrow]);
695 };
如果node的版本是 v12.2.0或以上,将createRequireFromPath改为createRequire 参考文档

小结点
154 声望3 粉丝