@aniyajs/rotor
Version:
基于webpack5开发的一款专注于打包、运行的工具
133 lines (109 loc) • 3.59 kB
JavaScript
;
// 确定当前构建环境
process.env.NODE_ENV = "production";
// 抛出所有未被捕获的错误
process.on("unhandledRejection", (error) => {
throw error;
});
const paths = require("../utils/paths");
const information = require("../utils/information");
const escape = require("escape-string-regexp");
const { checkFilesExists, depthDependFilePaths } = require("../utils/common");
const PluginGenerator = require("../utils/pluginGenerator");
// 检查所需的文件是否存在
checkFilesExists([paths.appHtml, paths.appConfigJs]);
information.loading("初始化开始");
// 依赖
const fs = require("fs-extra");
const path = require("path");
const execa = require("execa");
const {
initRequireFileHandle,
initTempHandle,
} = require("../utils/initialize");
// 可忽略的依赖后缀
const dependentSuffixs = [".ts", ".tsx", ".js", ".jsx", ".json"];
// 忽略所有隐藏文件
const hiddenFileRule = /(^|[/\\])\../;
// 匹配环境变量文件
const envFileReg =
process.env.ANIYAJS_ENV &&
new RegExp(`^config.${escape(process.env.ANIYAJS_ENV)}.(ts|js)$`);
const pluginMainSuffixs = [".ts", ".js"];
/**
* 初始化 meta.json,
* `src/.aniya/meta.json`,用于记录比如当前config文件及深度依赖路径等其他信息
*/
initRequireFileHandle()
.then((isInitRequire) => {
if (!isInitRequire) {
return new Error(null);
}
// 获取配置文件路径及深度依赖,用于监听自动重启项目
return depthDependFilePaths(
paths.appConfigJs,
dependentSuffixs,
hiddenFileRule,
);
})
.then(async (dependFiles) => {
const configFiles = [paths.appConfigJs, ...dependFiles];
// 初始化mock、config配置缓存文件,并拿到config缓存文件的文件及config目录
const initSuccessPaths = await initTempHandle(
envFileReg,
configFiles,
false,
true,
);
delete require.cache[
require.resolve(initSuccessPaths.appConfigTempIndexJs)
];
const config = require(initSuccessPaths.appConfigTempIndexJs).default;
let pluginInfos = [];
if (config.aniyaPlugins && config.aniyaPlugins.length) {
// 检查插件文件是否存在
checkFilesExists(config.aniyaPlugins, paths.appNodeModules);
config.aniyaPlugins.forEach((pluginRelativePath) => {
const pluginResolvePath = path.join(
paths.appNodeModules,
pluginRelativePath,
"lib",
);
const extension = pluginMainSuffixs.find((extension) =>
fs.existsSync(`${pluginResolvePath}${extension}`),
);
if (extension) {
const pluginGenertor = new PluginGenerator({
pluginIndex: `${pluginResolvePath}${extension}`,
config,
});
pluginGenertor.initialize();
// 确保commonjs可以引入module
// eslint-disable-next-line no-global-assign
require = require("esm")(module);
pluginGenertor.use(
require(`${pluginResolvePath}${extension}`).default,
);
pluginInfos.push(pluginGenertor.describeParams);
pluginGenertor.ending();
}
});
}
if (initSuccessPaths) {
fs.writeJsonSync(
paths.appTempMetaJs,
{
paths: initSuccessPaths,
},
{ spaces: 2 },
);
}
execa("node", [path.resolve(__dirname, "../webpack/webpackBuild.js")], {
cwd: process.cwd(),
stdio: "inherit",
});
})
.catch((error) => {
console.log(error);
process.exit(1);
});