Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ yarn serve --local-bricks=abc
exports.standaloneAppsConfig = [
{
// 将相关参数替换成调试目标上对应的数据。
// 该应用使用 v2 配置,当前默认为 v2。
// 在 v2 版本中,框架和构件包被拎了出来,这样不同的微应用之间,如果使用了相同版本的框架或构件包,
// 那么可以访问同一路径的资源,以提升缓存命中率。
standaloneVersion: 2,
appDir: "message-subscribe/",
appRoot: "/sa-static/message-subscribe/versions/2.4.16/webroot/-/",
bootstrapHash: "4c732a2e3488e65d",
// v2 相对于 v1 多了下面两个配置项:
publicPrefix: "/sa-static/-/",
coreVersion: "2.76.7",
},
{
// 该应用使用 v1 配置。
standaloneVersion: 1,
appDir: "auth/",
appRoot: "/next/auth/",
bootstrapHash: "9467de8737bcfadf",
},
{
standaloneVersion: 1,
appDir: "agile-task/",
appRoot: "/sa-static/agile-task/versions/1.0.6/webroot/",
bootstrapHash: "8d14a6be80273699",
Expand Down
5 changes: 5 additions & 0 deletions packages/brick-container/serve/getEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ module.exports = (runtimeFlags) => {
(!_standalone && devConfig && devConfig.nextRepoDir) || rootDir;
const standaloneAppsConfig =
(devConfig && devConfig.standaloneAppsConfig) || [];
for (const standaloneConfig of standaloneAppsConfig) {
if (!standaloneConfig.standaloneVersion) {
standaloneConfig.standaloneVersion = 2;
}
}
const appConfig = (devConfig && devConfig.appConfig) || {};

const { usePublicScope, standalone: confStandalone } =
Expand Down
64 changes: 43 additions & 21 deletions packages/brick-container/serve/getProxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,58 @@ module.exports = (env) => {
};
if (useRemote) {
for (const standaloneConfig of allAppsConfig) {
const assetRoot = standaloneConfig ? `${standaloneConfig.appRoot}-/` : "";
const assetRoot = standaloneConfig
? standaloneConfig.standaloneVersion === 2
? standaloneConfig.publicPrefix
: `${standaloneConfig.appRoot}-/`
: "";
if (standaloneConfig) {
// 在「独立应用」模式中,静态资源路径在 `your-app/-/` 目录下。
proxyPaths.push(assetRoot);
proxyPaths.push(`${standaloneConfig.appRoot}conf.yaml`);
if (standaloneConfig.standaloneVersion === 2) {
proxyPaths.push(`${standaloneConfig.appRoot}`);
}
} else {
const assetPaths = ["bricks", "micro-apps", "templates"];
proxyPaths.push(...assetPaths.map((p) => `${assetRoot}${p}`));
}

const assetPaths = ["bricks", "micro-apps", "templates"];
proxyPaths.push(...assetPaths.map((p) => `${assetRoot}${p}`));
}

apiProxyOptions.onProxyRes = (proxyRes, req, res) => {
if (env.asCdn) {
return;
}
// 设定透传远端请求时,可以指定特定的 brick-packages, micro-apps, templates 使用本地文件。
const isStandaloneBootstrap =
hasStandaloneApps &&
new RegExp(
`^${escapeRegExp(
// 匹配 `/next/your-app/-/bootstrap.[hash].json`
`${standaloneAppsConfig
.map((standaloneConfig) => escapeRegExp(standaloneConfig.appRoot))
.join("|")}-/bootstrap.`
)}[^.]+\\.json$`
).test(req.path);
if (

let reqIsBootstrap =
req.path === "/next/api/auth/bootstrap" ||
req.path === "/next/api/auth/v2/bootstrap" ||
isStandaloneBootstrap
) {
req.path === "/next/api/auth/v2/bootstrap";
let matchedStandaloneConfig;
if (!reqIsBootstrap && hasStandaloneApps) {
for (const standaloneConfig of standaloneAppsConfig) {
const regex = new RegExp(
`^${escapeRegExp(
`${standaloneConfig.appRoot}${
standaloneConfig.standaloneVersion === 2 ? "" : "-/"
}`
)}bootstrap\\.[^.]+\\.json$`
);
if (regex.test(req.path)) {
reqIsBootstrap = true;
matchedStandaloneConfig = standaloneConfig;
}
}
}

if (reqIsBootstrap) {
console.log("Modified bootstrap:", req.path);
modifyResponse(res, proxyRes, (raw) => {
if (res.statusCode !== 200) {
return raw;
}
const result = JSON.parse(raw);
const data = isStandaloneBootstrap ? result : result.data;
const data = matchedStandaloneConfig ? result : result.data;
if (localMicroApps.length > 0 || mockedMicroApps.length > 0) {
data.storyboards = mockedMicroApps
.map((id) =>
Expand Down Expand Up @@ -134,7 +149,14 @@ module.exports = (env) => {
);
if (combinedLocalBrickPackages.length > 0) {
data.brickPackages = combinedLocalBrickPackages
.map((id) => getSingleBrickPackage(env, id, data.brickPackages))
.map((id) =>
getSingleBrickPackage(
env,
id,
data.brickPackages,
matchedStandaloneConfig
)
)
.filter(Boolean)
.concat(
data.brickPackages.filter(
Expand Down Expand Up @@ -379,7 +401,7 @@ module.exports = (env) => {
secure: false,
changeOrigin: true,
pathRewrite: pathRewriteFactory(seg),
...(seg === "api" || seg.endsWith("/-")
...(seg === "api" || seg.endsWith("/-/")
? apiProxyOptions
: seg === ""
? rootProxyOptions
Expand Down
44 changes: 27 additions & 17 deletions packages/brick-container/serve/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,17 @@ module.exports = function serve(runtimeFlags) {
),
env.baseHref
)
.replace(
new RegExp(
escapeRegExp("<!--# echo var='app_root' default='' -->"),
"g"
),
standaloneConfig ? standaloneConfig.appRoot : ""
)
.replace(
new RegExp(
escapeRegExp("<!--# echo var='core_root' default='' -->"),
"g"
),
`${env.publicCdn ?? ""}${
standaloneConfig ? `${standaloneConfig.appRoot}-/core/` : ""
standaloneConfig
? standaloneConfig.standaloneVersion === 2
? `${standaloneConfig.appRoot}core/${standaloneConfig.coreVersion}/`
: `${standaloneConfig.appRoot}-/core/`
: ""
}`
)
.replace(
Expand All @@ -78,14 +75,23 @@ module.exports = function serve(runtimeFlags) {
[
"<script>",
"((w)=>{",
[
"w.STANDALONE_MICRO_APPS=!0",
`var a=w.APP_ROOT=${JSON.stringify(standaloneConfig.appRoot)}`,
`var d=a+"-/"`,
'var p=w.PUBLIC_ROOT=(w.PUBLIC_CDN||"")+d',
'w.CORE_ROOT=p+"core/"',
`w.BOOTSTRAP_FILE=d+"bootstrap.${standaloneConfig.bootstrapHash}.json"`,
]
"w.STANDALONE_MICRO_APPS=!0;",
`var a=w.APP_ROOT=${JSON.stringify(standaloneConfig.appRoot)};`,
(standaloneConfig.standaloneVersion === 2
? [
"w.PUBLIC_ROOT_WITH_VERSION=!0",
`var d=${JSON.stringify(standaloneConfig.publicPrefix)}`,
'var p=w.PUBLIC_ROOT=(w.PUBLIC_CDN||"")+d',
`w.CORE_ROOT=p+"core/${standaloneConfig.coreVersion}/"`,
`w.BOOTSTRAP_FILE=a+"bootstrap.${standaloneConfig.bootstrapHash}.json"`,
]
: [
'var d=a+"-/"',
'var p=w.PUBLIC_ROOT=(w.PUBLIC_CDN||"")+d',
'w.CORE_ROOT=p+"core/"',
`w.BOOTSTRAP_FILE=d+"bootstrap.${standaloneConfig.bootstrapHash}.json"`,
]
)
.filter(Boolean)
.join(";"),
"})(window)",
Expand Down Expand Up @@ -149,7 +155,11 @@ module.exports = function serve(runtimeFlags) {

// Serve static files.
const staticRoot = standaloneConfig
? `${standaloneConfig.appRoot || serveRoot}-/core/`
? standaloneConfig.standaloneVersion === 2
? `${standaloneConfig.appRoot || serveRoot}core/${
standaloneConfig.coreVersion
}/`
: `${standaloneConfig.appRoot || serveRoot}-/core/`
: serveRoot;
app.use(staticRoot, express.static(distDir));
}
Expand Down
8 changes: 5 additions & 3 deletions packages/brick-container/serve/serveLocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ module.exports = (env, app) => {

for (const standaloneConfig of allAppsConfig) {
const publicRoot = standaloneConfig
? `${standaloneConfig.appRoot}-/`
? standaloneConfig.standaloneVersion === 2
? standaloneConfig.publicPrefix
: `${standaloneConfig.appRoot}-/`
: baseHref;

// 开发时默认拦截 bootstrap 请求。
Expand Down Expand Up @@ -73,7 +75,7 @@ module.exports = (env, app) => {
new RegExp(
`^${escapeRegExp(
`${publicRoot}bricks/${pkgId}/`
)}(?!dist\\/editors\\/)(.+)`
)}(?:\\d+(?:\\.\\d+)*/)?(?!dist/editors/)(.+)`
),
(req, res) => {
tryServeFiles(
Expand Down Expand Up @@ -159,7 +161,7 @@ module.exports = (env, app) => {
brief: req.query.brief === "true",
})
),
brickPackages: getBrickPackages(env),
brickPackages: getBrickPackages(env, standaloneConfig),
templatePackages: getTemplatePackages(env),
});
});
Expand Down
26 changes: 20 additions & 6 deletions packages/brick-container/serve/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ function getSingleStoryboard(env, microAppName, mocked, options = {}) {
return storyboard;
}

function getBrickPackages(env) {
function getBrickPackages(env, standaloneConfig) {
return getNamesOfBrickPackages(env)
.map((name) => getSingleBrickPackage(env, name))
.map((name) =>
getSingleBrickPackage(env, name, undefined, standaloneConfig)
)
.filter(Boolean);
}

Expand All @@ -91,7 +93,12 @@ function getNamesOfBrickPackages(env) {
);
}

function getSingleBrickPackage(env, brickPackageName, remoteBrickPackages) {
function getSingleBrickPackage(
env,
brickPackageName,
remoteBrickPackages,
standaloneConfig
) {
const {
brickPackagesDir,
alternativeBrickPackagesDir,
Expand Down Expand Up @@ -121,10 +128,17 @@ function getSingleBrickPackage(env, brickPackageName, remoteBrickPackages) {
]);
if (fs.existsSync(distDir)) {
if (!remoteBrickPackages || localBrickPackages.includes(brickPackageName)) {
let versionPart = "";
if (standaloneConfig && standaloneConfig.standaloneVersion === 2) {
const packageJson = JSON.parse(
fs.readFileSync(path.resolve(distDir, "../package.json"))
);
versionPart = `${packageJson.version}/`;
}
let filePath, bricksJson;
for (const file of fs.readdirSync(distDir)) {
if (file.endsWith(".js")) {
filePath = `bricks/${brickPackageName}/dist/${file}`;
filePath = `bricks/${brickPackageName}/${versionPart}dist/${file}`;
} else if (file === "bricks.json") {
bricksJson = JSON.parse(
fs.readFileSync(path.join(distDir, "bricks.json"), "utf8")
Expand All @@ -140,8 +154,8 @@ function getSingleBrickPackage(env, brickPackageName, remoteBrickPackages) {
filePath,
},
!remoteBrickPackages || localEditorPackages.includes(brickPackageName)
? bricksJson
: omit(bricksJson, ["editors", "editorsJsFilePath"])
? omit(bricksJson, ["filePath"])
: omit(bricksJson, ["filePath", "editors", "editorsJsFilePath"])
);
}
if (
Expand Down