Skip to content

Commit 137ecdf

Browse files
Add babel plugin module resolver to composite
1 parent 743e3b4 commit 137ecdf

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

ern-composite-gen/src/createBabelRc.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs-extra';
22
import path from 'path';
3-
import { v4 as uuid } from 'uuid';
3+
import { v4 as uuid, validate as uuidValidate } from 'uuid';
44
import semver from 'semver';
55
import { log, readPackageJson, writePackageJson } from 'ern-core';
66
import { getNodeModuleVersion } from './getNodeModuleVersion';
@@ -56,7 +56,11 @@ export async function createBabelRc({
5656
// it messing with other module-resolver plugin configurations that could
5757
// be defined in the .babelrc config of individual MiniApps
5858
// https://babeljs.io/docs/en/options#plugin-preset-merging
59-
babelPlugin.push(uuid());
59+
// Check if lastItem of babel plugin is not uuid
60+
const lastItem = babelPlugin.slice(-1)[0];
61+
if (typeof lastItem !== 'string' || !uuidValidate(lastItem)) {
62+
babelPlugin.push(uuid());
63+
}
6064
// Copy over module-resolver plugin & config to top level composite .babelrc
6165
log.debug(
6266
`Taking care of module-resolver Babel plugin for ${miniAppName} MiniApp`,
@@ -68,6 +72,14 @@ export async function createBabelRc({
6872
for (const x of babelPlugin) {
6973
if (x instanceof Object && x.alias) {
7074
moduleResolverAliases = x.alias;
75+
Object.keys(moduleResolverAliases).map((key) => {
76+
if (!moduleResolverAliases[key].startsWith(miniAppName))
77+
moduleResolverAliases[
78+
key
79+
] = `${miniAppName}/${moduleResolverAliases[
80+
key
81+
].replace('./', '')}`;
82+
});
7183
break;
7284
}
7385
}
@@ -105,7 +117,6 @@ export async function createBabelRc({
105117
log.debug(
106118
`Removing babel object from ${miniAppName} MiniApp package.json`,
107119
);
108-
delete miniAppPackageJson.babel;
109120
await writePackageJson(p, miniAppPackageJson);
110121
}
111122
}

ern-composite-gen/src/generateComposite.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ async function generateFullComposite(
214214
cwd: outDir,
215215
extraJsDependencies: [
216216
PackagePath.fromString('ern-bundle-store-metro-asset-plugin'),
217+
PackagePath.fromString('babel-plugin-module-resolver'),
217218
PackagePath.fromString('react-native-svg-transformer'),
218219
...extraJsDependencies,
219220
],

ern-composite-gen/test/compositegen-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ describe('ern-container-gen utils.js', () => {
368368
pathToYarnLock: pathToSampleYarnLock,
369369
});
370370
assert(yarnCliStub.install.calledOnce);
371-
sinon.assert.callCount(yarnCliStub.add, 4);
371+
sinon.assert.callCount(yarnCliStub.add, 5);
372372
assert(yarnCliStub.install.calledBefore(yarnCliStub.add));
373373
});
374374

@@ -408,7 +408,7 @@ describe('ern-container-gen utils.js', () => {
408408
];
409409
yarnCliStub.init.callsFake(() => fakeYarnInit(tmpOutDir, '0.57.0'));
410410
await generateComposite({ miniApps, outDir: tmpOutDir });
411-
sinon.assert.callCount(yarnCliStub.add, 5);
411+
sinon.assert.callCount(yarnCliStub.add, 6);
412412
});
413413

414414
it('should create index.js', async () => {

0 commit comments

Comments
 (0)