我正在开发Argdown VSCode extension。可以使用argdown.config.json
文件或导出配置对象的argdown.config.js
文件来配置Argdown解析器。使用Javascript文件是允许用户向Argdown解析器添加自定义插件的最简单方法。
如果用户告诉解析器使用Javascript文件,则使用import-fresh加载该文件(它使用节点的require,但删除缓存的版本。
使用Argdown命令行工具(@argdown/cli)可以很好地工作,但在VSCode扩展中找不到配置文件的模块。该扩展使用绝对文件路径来要求配置模块(例如"C:\Users\my-username\projects\my-argdown-project\argdown.config.js").这些路径在VScode扩展之外使用import-fresh。
VSCode扩展是否有安全限制,不允许使用具有绝对文件路径的模块?或者,有没有其他原因导致这种方法不起作用?
发布于 2021-02-24 16:41:45
这与VSCode无关。这个问题是由于import-fresh
与webpack捆绑在一起造成的。我以为webpack会忽略动态导入,但它没有。
我很幸运:从上个月开始,webpack支持"magic comments" for require (不仅仅是导入)。所以我可以使用:
require(/* webpackIgnore: true */ file);
你必须在你的webpack配置中激活魔术评论支持:
module.exports = {
parser: {
javascript: {
commonjsMagicComments: true,
},
},
}
现在下一个问题是如何将神奇的注释添加到import-fresh
包中。为此,我使用了string-replace-loader:
module.exports = {
module: {
rules: {
{
enforce: "pre",
test: /import-fresh[\/\\]index\.js/,
loader: "string-replace-loader",
options: {
search:
"return parent === undefined ? require(filePath) : parent.require(filePath);",
replace:
"return parent === undefined ? require(/* webpackIgnore: true */ filePath) : parent.require(/* webpackIgnore: true */ filePath);",
},
},
}
}
}
在此之后,我可以再次加载argdown.config.js
文件,即使在与webpack捆绑所有内容之后也是如此。
https://stackoverflow.com/questions/66350516
复制相似问题