我在monorepo的多个软件包中使用create-react-app。每个应用程序的“公共”文件夹中都有大量重复的代码和文件,因为它们都具有相同的图标,描述,字体等。
有没有一种方法可以将“ public”文件夹中的一些或全部文件移动到它们自己的包中,通过handlebars.js之类的工具运行它们,最后将它们与create-react-app捆绑在一起,而不会弹出?
我在monorepo的多个软件包中使用create-react-app。每个应用程序的“公共”文件夹中都有大量重复的代码和文件,因为它们都具有相同的图标,描述,字体等。
有没有一种方法可以将“ public”文件夹中的一些或全部文件移动到它们自己的包中,通过handlebars.js之类的工具运行它们,最后将它们与create-react-app捆绑在一起,而不会弹出?
我为此找不到任何开源工具,所以我写了自己的脚本:
prepare_cra_files.sh
#!/usr/bin/env bash
for app in apps/*/ ; do
# Flush the files if they already exist
if [ -d "$app"public ]; then
rm -r "$app"public
fi
# Copy over the template files
cp -r template "$app/public"
done
node templatify.js
templatify.js
const Handlebars = require("handlebars");
const fs = require("fs-extra");
const path = require("path");
const APPS_PATH = path.join(__dirname,"..","apps");
const INDEX_HTML_TEMPLATE_PATH = path.join(__dirname,"template","index.handlebars");
(async () => {
const dirs = await fs.readdir(APPS_PATH);
const indexHtmlTemplate = Handlebars.compile(await fs.readFile(INDEX_HTML_TEMPLATE_PATH,"utf-8"));
dirs.forEach(async appName => {
const indexHtmlContextPath = path.join(APPS_PATH,appName,"/handlebars/index.json");
if (!fs.existsSync(indexHtmlContextPath)) {
throw new Error(`Please provide the index.html context for the ${appName} appName`);
}
const indexHtmlContext = JSON.parse(await fs.readFile(indexHtmlContextPath,"utf-8"));
const indexHtml = indexHtmlTemplate(indexHtmlContext);
await fs.writeFile(path.join(APPS_PATH,"public","index.html"),indexHtml);
await fs.remove(path.join(APPS_PATH,"index.handlebars"));
});
})();