Skip to content

Commit 679e038

Browse files
committed
Guarantee miniapps import order in composite
1 parent 6c38aca commit 679e038

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

ern-composite-gen/src/createIndexJs.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
import path from 'path';
22
import fs from 'fs-extra';
3-
import { readPackageJson } from 'ern-core';
3+
import { readPackageJson, PackagePath } from 'ern-core';
44

55
export async function createIndexJs({
66
cwd,
7-
extraImports = [],
7+
miniApps,
88
}: {
99
cwd: string;
10-
extraImports?: string[];
10+
miniApps: PackagePath[];
1111
}) {
1212
let entryIndexJsContent = '';
1313

14-
const dependencies: string[] = [...extraImports];
1514
const compositePackageJson = await readPackageJson(cwd);
16-
for (const dependency of Object.keys(
17-
compositePackageJson.dependencies || [],
18-
)) {
19-
dependencies.push(dependency);
15+
for (const miniApp of miniApps) {
16+
// Add miniapp imports strictly matching miniapps array order
17+
// For git based miniapps we have to rely on some trickery to
18+
// find the package name, as it won't be set in the PackagePath
19+
// We just look in the composite package.json for a match on
20+
// the path, and get the package name from there.
21+
//
22+
// Sample git package in package.json:
23+
// "bar": "git+ssh://github.com/foo/bar.git#master"
24+
const pkgName = miniApp.isGitPath
25+
? Object.entries(compositePackageJson.dependencies).find(
26+
([, v]) => v === miniApp.fullPath,
27+
)![0]
28+
: miniApp.name;
29+
entryIndexJsContent += `import '${pkgName}'\n`;
2030
}
21-
dependencies.forEach((d) => {
22-
entryIndexJsContent += `import '${d}'\n`;
23-
});
2431

2532
await fs.writeFile(path.join(cwd, 'index.js'), entryIndexJsContent);
2633
// Still also generate index.android.js and index.ios.js for backward compatibility with

ern-composite-gen/src/generateComposite.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,6 @@ async function generateFullComposite(
151151
const remoteMiniapps = miniApps.filter((p) => !p.isFilePath);
152152
const localMiniApps = miniApps.filter((p) => p.isFilePath);
153153
const localMiniAppsPaths = localMiniApps.map((m) => m.basePath);
154-
const localMiniAppsPkgNames = [];
155-
for (const x of localMiniApps) {
156-
const pJson = await readPackageJson(x.basePath);
157-
localMiniAppsPkgNames.push(pJson.name);
158-
}
159154
const extraNodeModules: { [pkg: string]: string } = {};
160155

161156
try {
@@ -201,7 +196,10 @@ async function generateFullComposite(
201196

202197
await addRNStartScriptToPjson({ cwd: outDir });
203198

204-
await createIndexJs({ cwd: outDir, extraImports: localMiniAppsPkgNames });
199+
await createIndexJs({
200+
cwd: outDir,
201+
miniApps,
202+
});
205203

206204
await createWatchmanConfig({ cwd: outDir });
207205

0 commit comments

Comments
 (0)