|
1 | 1 | import path from 'path';
|
2 | 2 | import fs from 'fs-extra';
|
3 |
| -import { readPackageJson } from 'ern-core'; |
| 3 | +import { readPackageJson, PackagePath } from 'ern-core'; |
4 | 4 |
|
5 | 5 | export async function createIndexJs({
|
6 | 6 | cwd,
|
7 |
| - extraImports = [], |
| 7 | + miniApps, |
8 | 8 | }: {
|
9 | 9 | cwd: string;
|
10 |
| - extraImports?: string[]; |
| 10 | + miniApps: PackagePath[]; |
11 | 11 | }) {
|
12 | 12 | let entryIndexJsContent = '';
|
13 | 13 |
|
14 |
| - const dependencies: string[] = [...extraImports]; |
15 | 14 | 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`; |
20 | 30 | }
|
21 |
| - dependencies.forEach((d) => { |
22 |
| - entryIndexJsContent += `import '${d}'\n`; |
23 |
| - }); |
24 | 31 |
|
25 | 32 | await fs.writeFile(path.join(cwd, 'index.js'), entryIndexJsContent);
|
26 | 33 | // Still also generate index.android.js and index.ios.js for backward compatibility with
|
|
0 commit comments