From 137ecdfdb8807809200203bd120381891cc377bd Mon Sep 17 00:00:00 2001 From: Zafer AYAN Date: Tue, 8 Feb 2022 01:18:48 +0300 Subject: [PATCH] Add babel plugin module resolver to composite --- ern-composite-gen/src/createBabelRc.ts | 17 ++++++++++++++--- ern-composite-gen/src/generateComposite.ts | 1 + ern-composite-gen/test/compositegen-test.ts | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ern-composite-gen/src/createBabelRc.ts b/ern-composite-gen/src/createBabelRc.ts index 948df5ce5..1b1282e72 100644 --- a/ern-composite-gen/src/createBabelRc.ts +++ b/ern-composite-gen/src/createBabelRc.ts @@ -1,6 +1,6 @@ import fs from 'fs-extra'; import path from 'path'; -import { v4 as uuid } from 'uuid'; +import { v4 as uuid, validate as uuidValidate } from 'uuid'; import semver from 'semver'; import { log, readPackageJson, writePackageJson } from 'ern-core'; import { getNodeModuleVersion } from './getNodeModuleVersion'; @@ -56,7 +56,11 @@ export async function createBabelRc({ // it messing with other module-resolver plugin configurations that could // be defined in the .babelrc config of individual MiniApps // https://babeljs.io/docs/en/options#plugin-preset-merging - babelPlugin.push(uuid()); + // Check if lastItem of babel plugin is not uuid + const lastItem = babelPlugin.slice(-1)[0]; + if (typeof lastItem !== 'string' || !uuidValidate(lastItem)) { + babelPlugin.push(uuid()); + } // Copy over module-resolver plugin & config to top level composite .babelrc log.debug( `Taking care of module-resolver Babel plugin for ${miniAppName} MiniApp`, @@ -68,6 +72,14 @@ export async function createBabelRc({ for (const x of babelPlugin) { if (x instanceof Object && x.alias) { moduleResolverAliases = x.alias; + Object.keys(moduleResolverAliases).map((key) => { + if (!moduleResolverAliases[key].startsWith(miniAppName)) + moduleResolverAliases[ + key + ] = `${miniAppName}/${moduleResolverAliases[ + key + ].replace('./', '')}`; + }); break; } } @@ -105,7 +117,6 @@ export async function createBabelRc({ log.debug( `Removing babel object from ${miniAppName} MiniApp package.json`, ); - delete miniAppPackageJson.babel; await writePackageJson(p, miniAppPackageJson); } } diff --git a/ern-composite-gen/src/generateComposite.ts b/ern-composite-gen/src/generateComposite.ts index af283a6fd..3db7437c9 100644 --- a/ern-composite-gen/src/generateComposite.ts +++ b/ern-composite-gen/src/generateComposite.ts @@ -214,6 +214,7 @@ async function generateFullComposite( cwd: outDir, extraJsDependencies: [ PackagePath.fromString('ern-bundle-store-metro-asset-plugin'), + PackagePath.fromString('babel-plugin-module-resolver'), PackagePath.fromString('react-native-svg-transformer'), ...extraJsDependencies, ], diff --git a/ern-composite-gen/test/compositegen-test.ts b/ern-composite-gen/test/compositegen-test.ts index a8dbbd22f..cf736d395 100644 --- a/ern-composite-gen/test/compositegen-test.ts +++ b/ern-composite-gen/test/compositegen-test.ts @@ -368,7 +368,7 @@ describe('ern-container-gen utils.js', () => { pathToYarnLock: pathToSampleYarnLock, }); assert(yarnCliStub.install.calledOnce); - sinon.assert.callCount(yarnCliStub.add, 4); + sinon.assert.callCount(yarnCliStub.add, 5); assert(yarnCliStub.install.calledBefore(yarnCliStub.add)); }); @@ -408,7 +408,7 @@ describe('ern-container-gen utils.js', () => { ]; yarnCliStub.init.callsFake(() => fakeYarnInit(tmpOutDir, '0.57.0')); await generateComposite({ miniApps, outDir: tmpOutDir }); - sinon.assert.callCount(yarnCliStub.add, 5); + sinon.assert.callCount(yarnCliStub.add, 6); }); it('should create index.js', async () => {