admin管理员组文章数量:1279113
If you use typescript you can init a default tsconfig.json, and that json will have javascript // and /* */ ments in it. I've encountered a situation with ts-jest where I need to require my tsconfig and parse it, but jest is not parsing it because json doesn't allow ments. see this I'm not sure how typescript handles it, but it seems to deviate from the rule.
// this fails if tsconfig has ments
const tsconfig = require('./tsconfig')
I want to keep the ments because they are really helpful to understand and maintain my tsconfig.json, and I want to require my config to be able to avoid duplicating code and make things more dynamic.
Is there a way to require a json file with ments using nodejs?
If you use typescript you can init a default tsconfig.json, and that json will have javascript // and /* */ ments in it. I've encountered a situation with ts-jest where I need to require my tsconfig and parse it, but jest is not parsing it because json doesn't allow ments. see this I'm not sure how typescript handles it, but it seems to deviate from the rule.
// this fails if tsconfig has ments
const tsconfig = require('./tsconfig')
I want to keep the ments because they are really helpful to understand and maintain my tsconfig.json, and I want to require my config to be able to avoid duplicating code and make things more dynamic.
Is there a way to require a json file with ments using nodejs?
Share Improve this question edited Nov 18, 2020 at 13:17 Tom Hale 47k40 gold badges201 silver badges265 bronze badges asked May 25, 2020 at 5:17 ThomThom 5511 gold badge6 silver badges12 bronze badges 03 Answers
Reset to default 9json5 can do the trick. Install JSON5 and then:
const JSON5 = require('json5');
const tsconfig = JSON5.parse(jsontxt);
Note that removing ments by yourself is possible but has several pitfalls, and that's why it's not remended. You should look for:
- Strings may contain sequences that look like ments, only they are not.
- Comments may contain sequences that look like string beginning, only they are not.
- Strings may contain
"
that don't mark string end such as"foo\"bar"
. It's more confusing with"foo\\"
, where the second " does end the string. - A
//
ment may contain/*
, but that does not start a multi line ment. - A
//
in a multi line ment does not start a single-line ment, such that removing ments in/* // */ "hi"
results with"hi"
. - Does a
\
continue a single-line to the next line? Does a\*/
end a multi line ment, or does the\
make the*
act differently? (Neither are supported in JavaScript). - are multi line strings supported (they are in JavaScript)? How are they declared/dealt with.
require-json5
is purpose-built for this:
const requireJSON5 = require('require-json5');
const tsconfig = requireJSON5('./tsconfig.json');
Or even more simply:
require('require-json5').replace();
var tsconfig = require("./tsconfig");
I managed to do it by pre-processing the json to strip the ments. The following code is doing the job for me:
const fs = require('fs')
const stripForwardSlashForwardSlashComment = new RegExp('//(.*)', 'g')
const stripForwardSlashStarComment = new RegExp('[/][*](.*)[*][/]', 'gm')
let jsontxt = fs.readFileSync('./tsconfig.json', 'utf8')
jsontxt = jsontxt.replace(stripForwardSlashForwardSlashComment,'').replace(stripForwardSlashStarComment, '')
const tsconfig = JSON.parse(jsontxt)
Self answer note: Posting to keep this public and get some feedback
本文标签: javascriptRequiring a JSON with comments in nodejsStack Overflow
版权声明:本文标题:javascript - Requiring a JSON with comments in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741248763a2365396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论