在 bundleconfig.json 中使用 BundlerMinifier 中的相对路径和通配模式(folder/subfolders 中的所有文件)
Using relative paths and globbing patterns (all files in folder/subfolders) in bundleconfig.json with BundlerMinifier
我有一个 ASP.NET 核心网络项目。我正在尝试使用项目根目录下 bundleconfig.json 文件上的 BundlerMinifier package 捆绑一堆静态 .js 文件。我遇到的问题是我要捆绑的 .js 文件位于解决方案的另一个项目中(我将其称为 MainProject),因此我必须使用相对路径来指定它们,如下所示:
"outputFileName": "../MainProject/bundles/libraryscripts.js",
"inputFiles": [
"../MainProject/Scripts/Libraries/Angular/**/*.js",
// more input files
],
"minify": {
"enabled": true
}
当我构建我的项目时,捆绑器没有给出任何错误,并且文件 libraryscripts.js
在指定的文件夹中创建。问题是文件是空的,我认为这是由于通配模式 (**/*.js)。当我枚举所有文件而不是使用此模式时,它工作正常。使这变得更复杂的是,当我不使用相对路径(开始时没有 ../ )时,它似乎在使用 globbing 模式时工作正常。
这使我相信将相对路径与 globbing 模式结合使用是一个问题。谁能证实这一点,有谁知道解决这个问题的方法吗?我不想列举数百个 .js 文件(既不优雅也不可持续)。
注意:以下是我实施的一个 hacky 解决方法 -- 它不是这个问题的官方解决方案,但它可能仍然有用。
我克隆了 BundlerMinifier project so I could debug how it was bundling the files specified in bundleconfig.json
and see what the problem was. The issue was in Bundle.cs
,特别是在 GetAbsoluteInputFiles
方法中。这一行获取存放bundleconfig.json
的文件夹的路径:
string folder = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;
问题是稍后在修剪找到的文件路径的开头时使用了相同的 folder
变量:
var allFiles = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(folder + FileHelpers.PathSeparatorChar, ""));
由于文件在另一个目录中,.Select(f => f.Replace());
部分没有删除文件路径的开头,这意味着后来匹配时比较失败。因此,没有找到同时具有 ../
和通配模式的文件。
我为自己想出了一个 hacky 解决方案,但我认为它不可靠,因此我不会为 Git 项目做出贡献。尽管如此,我还是会把它放在这里以防其他人遇到同样的问题。我创建了 folder
:
的副本
string folder = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;
string alternateFolder = folder;
bool folderModified = false;
然后我创建了一个 inputFile
的副本并检查它是否以 ../
开头。如果是这样,将其删除,同时从 alternateFolder
:
中删除最后一个文件夹名称
string searchDir = new FileInfo(Path.Combine(folder, relative).NormalizePath()).FullName;
string newInput = inputFile;
while (newInput.StartsWith("../"))
{
// I'm sure there's a better way to do this using some class/library.
newInput = newInput.Substring(3);
if (!folderModified)
{
int lastSlash = alternateFolder.LastIndexOf('\');
alternateFolder = alternateFolder.Substring(0, lastSlash);
folderModified = true;
}
}
然后我在以下行中使用 alternateFolder
和 newInput
仅:
var allFiles = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(alternateFolder + FileHelpers.PathSeparatorChar, ""));
var matches = Minimatcher.Filter(allFiles, newInput, options).Select(f => Path.Combine(alternateFolder, f));
其他地方仍然使用 folder
和 inputFile
。另请注意 folderModified
布尔值的使用。重要的是只删除 alternateFolder
上的最后一个文件夹一次,因为它处于 foreach 循环中。
我有一个 ASP.NET 核心网络项目。我正在尝试使用项目根目录下 bundleconfig.json 文件上的 BundlerMinifier package 捆绑一堆静态 .js 文件。我遇到的问题是我要捆绑的 .js 文件位于解决方案的另一个项目中(我将其称为 MainProject),因此我必须使用相对路径来指定它们,如下所示:
"outputFileName": "../MainProject/bundles/libraryscripts.js",
"inputFiles": [
"../MainProject/Scripts/Libraries/Angular/**/*.js",
// more input files
],
"minify": {
"enabled": true
}
当我构建我的项目时,捆绑器没有给出任何错误,并且文件 libraryscripts.js
在指定的文件夹中创建。问题是文件是空的,我认为这是由于通配模式 (**/*.js)。当我枚举所有文件而不是使用此模式时,它工作正常。使这变得更复杂的是,当我不使用相对路径(开始时没有 ../ )时,它似乎在使用 globbing 模式时工作正常。
这使我相信将相对路径与 globbing 模式结合使用是一个问题。谁能证实这一点,有谁知道解决这个问题的方法吗?我不想列举数百个 .js 文件(既不优雅也不可持续)。
注意:以下是我实施的一个 hacky 解决方法 -- 它不是这个问题的官方解决方案,但它可能仍然有用。
我克隆了 BundlerMinifier project so I could debug how it was bundling the files specified in bundleconfig.json
and see what the problem was. The issue was in Bundle.cs
,特别是在 GetAbsoluteInputFiles
方法中。这一行获取存放bundleconfig.json
的文件夹的路径:
string folder = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;
问题是稍后在修剪找到的文件路径的开头时使用了相同的 folder
变量:
var allFiles = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(folder + FileHelpers.PathSeparatorChar, ""));
由于文件在另一个目录中,.Select(f => f.Replace());
部分没有删除文件路径的开头,这意味着后来匹配时比较失败。因此,没有找到同时具有 ../
和通配模式的文件。
我为自己想出了一个 hacky 解决方案,但我认为它不可靠,因此我不会为 Git 项目做出贡献。尽管如此,我还是会把它放在这里以防其他人遇到同样的问题。我创建了 folder
:
string folder = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;
string alternateFolder = folder;
bool folderModified = false;
然后我创建了一个 inputFile
的副本并检查它是否以 ../
开头。如果是这样,将其删除,同时从 alternateFolder
:
string searchDir = new FileInfo(Path.Combine(folder, relative).NormalizePath()).FullName;
string newInput = inputFile;
while (newInput.StartsWith("../"))
{
// I'm sure there's a better way to do this using some class/library.
newInput = newInput.Substring(3);
if (!folderModified)
{
int lastSlash = alternateFolder.LastIndexOf('\');
alternateFolder = alternateFolder.Substring(0, lastSlash);
folderModified = true;
}
}
然后我在以下行中使用 alternateFolder
和 newInput
仅:
var allFiles = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(alternateFolder + FileHelpers.PathSeparatorChar, ""));
var matches = Minimatcher.Filter(allFiles, newInput, options).Select(f => Path.Combine(alternateFolder, f));
其他地方仍然使用 folder
和 inputFile
。另请注意 folderModified
布尔值的使用。重要的是只删除 alternateFolder
上的最后一个文件夹一次,因为它处于 foreach 循环中。