要求 config.js 文件以 VSCode 扩展名和绝对路径(例如 "C:\...")不起作用
Requiring config.js file in VSCode extension with absolute path (e.g. "C:\...") does not work
我正在开发 Argdown VSCode extension。 Argdown 解析器可以使用 argdown.config.json
文件或 argdown.config.js
文件导出配置对象进行配置。使用 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 扩展的安全限制,不允许要求具有绝对文件路径的模块?还是有其他原因导致这不起作用?
这与 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 捆绑所有内容之后也是如此。
我正在开发 Argdown VSCode extension。 Argdown 解析器可以使用 argdown.config.json
文件或 argdown.config.js
文件导出配置对象进行配置。使用 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 扩展的安全限制,不允许要求具有绝对文件路径的模块?还是有其他原因导致这不起作用?
这与 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 捆绑所有内容之后也是如此。