aui / art-template

High performance JavaScript templating engine
https://aui.github.io/art-template/
MIT License
9.86k stars 2.68k forks source link

请问可以在nestJS中配置art-template吗? #647

Open 123lujiaqiang opened 2 years ago

123lujiaqiang commented 2 years ago
houkunlin commented 1 year ago

我最近尝试用 nestJs 来写一个小服务(群晖NAS短信通知、WebHooks中转服务),主要就是用来简单的解析一些模板内容,因此引入了 art-template ,但是并没有使用 art-template 来渲染HTML页面内容。

在我打包的计划发布到生产环境的时候,先是解决了 nestJs 包含依赖的打包问题后,运行的时候遇到了 art-template 的引入问题,以下是我的记录内容:

npm run build

打包成功后会生成一个 ./dist/main.js 文件,此时可使用命令 node ./dist/main.js 运行服务,不出意外的话会出现一个异常信息

/home/user/http-server-node/dist/main.js:229652
(void 0)[template.defaults.extname] = extension;
                                    ^

TypeError: Cannot set properties of undefined (setting '.art')
    at Object.<anonymous> (/home/user/http-server-node/dist/main.js:229652:37)
    at __webpack_require__ (/home/user/http-server-node/dist/main.js:271956:42)
    at Object.<anonymous> (/home/user/http-server-node/dist/main.js:57762:21)
    at __webpack_require__ (/home/user/http-server-node/dist/main.js:271956:42)
    at Object.<anonymous> (/home/user/http-server-node/dist/main.js:57689:26)
    at __webpack_require__ (/home/user/http-server-node/dist/main.js:271956:42)
    at Object.<anonymous> (/home/user/http-server-node/dist/main.js:19:22)
    at __webpack_require__ (/home/user/http-server-node/dist/main.js:271956:42)
    at /home/user/http-server-node/dist/main.js:272020:37
    at Object.<anonymous> (/home/user/http-server-node/dist/main.js:272022:12)

这个异常是 art-template 模板引擎导致的,art-template 中有一行代码打包后无法在 node 下执行,此时需要魔改 art-template 代码,改动文件 node_modules/art-template/index.js ,原代码如下:

const template = require('./lib/index');
const extension = require('./lib/extension');

template.extension = extension;
require.extensions[template.defaults.extname] = extension;

module.exports = template;

改动后的代码

const template = require('./lib/index');
const extension = require('./lib/extension');

template.extension = extension;
// require.extensions[template.defaults.extname] = extension;// 只需要注释掉这行代码就行了

module.exports = template;