Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add babel plugin module resolver to composite #1856

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 14 additions & 3 deletions ern-composite-gen/src/createBabelRc.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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`,
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions ern-composite-gen/src/generateComposite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
4 changes: 2 additions & 2 deletions ern-composite-gen/test/compositegen-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

Expand Down Expand Up @@ -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 () => {
Expand Down