为 HTML/JS/CSS 编写一个小文件打包器
Coding a small file bundler for HTML/JS/CSS
所以我的项目有很多小的 "widget" 组件,它们有自己的 CSS 和 JS。
我需要的是一种自动 "bundle" 将所有组件的 css 和 js 整合到一个文件中,只需按一下按钮(或 cmd 表达式)。
像这样:bundleMeFiles.exe -header -footer -widget1
或者如果全部:bundleMeFiles.exe -all
.
我知道有很多工具和很多东西可以大规模地做到这一点,但我也是一个 DIY 人,所以我喜欢自己做东西,因为它可以让我进步。
我的第一个猜测是 Powershell 或什至 Java 来构建一个奇特的 UI 但欢迎任何提示。
您可以在 java 中使用 commons-io library
public static void main(String[] args) throws IOException {
File result = new File("C:/temp/result.txt");
OutputStream os = new FileOutputStream(result);
Collection<File> files = FileUtils.listFiles(
new File("C:/temp/dir"),
new RegexFileFilter("^(.*?)"),
DirectoryFileFilter.DIRECTORY
);
for(File f: files){
//path to file surrounded by ===
IOUtils.writeLines(
Arrays.asList("===", f.getAbsolutePath(), "==="),
"\r\n",
os,
Charset.defaultCharset());
//file contents
IOUtils.copy(new FileInputStream(f), os);
}
}
所以我的项目有很多小的 "widget" 组件,它们有自己的 CSS 和 JS。 我需要的是一种自动 "bundle" 将所有组件的 css 和 js 整合到一个文件中,只需按一下按钮(或 cmd 表达式)。
像这样:bundleMeFiles.exe -header -footer -widget1
或者如果全部:bundleMeFiles.exe -all
.
我知道有很多工具和很多东西可以大规模地做到这一点,但我也是一个 DIY 人,所以我喜欢自己做东西,因为它可以让我进步。
我的第一个猜测是 Powershell 或什至 Java 来构建一个奇特的 UI 但欢迎任何提示。
您可以在 java 中使用 commons-io library
public static void main(String[] args) throws IOException {
File result = new File("C:/temp/result.txt");
OutputStream os = new FileOutputStream(result);
Collection<File> files = FileUtils.listFiles(
new File("C:/temp/dir"),
new RegexFileFilter("^(.*?)"),
DirectoryFileFilter.DIRECTORY
);
for(File f: files){
//path to file surrounded by ===
IOUtils.writeLines(
Arrays.asList("===", f.getAbsolutePath(), "==="),
"\r\n",
os,
Charset.defaultCharset());
//file contents
IOUtils.copy(new FileInputStream(f), os);
}
}