Skip to content

Commit

Permalink
chore: Tenanted ready mfe builds (#219)
Browse files Browse the repository at this point in the history
* wip: Tenanted lazy modules

* wip: Tenanted lazy modules
  • Loading branch information
amir-zahedi authored Feb 27, 2024
1 parent fac7d0a commit 0659085
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 67 deletions.
6 changes: 6 additions & 0 deletions .changeset/plenty-parents-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'gdu': patch
'@autoguru/utilities': patch
---

GDU: Added support for building for a tenant
4 changes: 2 additions & 2 deletions packages/gdu/commands/build/buildSPA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { GuruConfig } from '../../lib/config';
import { run } from '../../lib/runWebpack';
import { getHooks } from '../../utils/hooks';

export const buildSPA = async (guruConfig: GuruConfig) => {
export const buildSPA = async (guruConfig: GuruConfig, tenant: string) => {
const hooks = getHooks();

// eslint-disable-next-line unicorn/prefer-prototype-methods
const webpackConfigs: Configuration[] = hooks.webpackConfig.call(
makeWebpackConfig(void 0, false),
makeWebpackConfig(void 0, false, tenant),
);

const compiler = webpack(webpackConfigs);
Expand Down
6 changes: 3 additions & 3 deletions packages/gdu/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import { buildSPA } from './buildSPA';
import { buildSSR } from './buildSSR';
import { buildWebComponents } from './buildWebComponents';

export default async () => {
export default async ({ tenant }) => {
setEnvProd(true);

const guruConfig = getGuruConfig();

banner('Building');
banner(tenant ? `Building for tenant: ${tenant}` : 'Building untenanted');

await buildSupportedBrowsers();

let stats;
switch (guruConfig?.type) {
case 'spa':
stats = await buildSPA(guruConfig);
stats = await buildSPA(guruConfig, tenant);
break;
case 'ssr':
stats = await buildSSR(guruConfig);
Expand Down
1 change: 1 addition & 0 deletions packages/gdu/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default (app: Sade) => {
// === BUILD
app.command('build')
.describe('Builds the target app ready for production')
.option('-t, --tenant', 'Tenant to build the mfe for', '')
.action(deferredAction(async () => import('./build'), IS_NOT_ROOT));

// === FORMAT
Expand Down
3 changes: 1 addition & 2 deletions packages/gdu/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export {Manifest} from "./webpack/plugins/GuruBuildManifest";

export { Manifest } from './webpack/plugins/GuruBuildManifest';
8 changes: 6 additions & 2 deletions packages/gdu/config/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { getBuildEnvs } from '../../utils/configs';

import { baseOptions, makeWebpackConfig } from './webpack.config';

const buildConfigs = (env = process.env.APP_ENV, isDebug): Configuration[] => {
const buildConfigs = (
env = process.env.APP_ENV,
isDebug: boolean,
tenant?: string,
): Configuration[] => {
const buildEnvs = getBuildEnvs(env);
return buildEnvs.map((buildEnv) => ({
...baseOptions(buildEnv, buildEnvs.length > 1, isDebug),
...makeWebpackConfig(buildEnv, buildEnvs.length > 1),
...makeWebpackConfig(buildEnv, buildEnvs.length > 1, tenant),
}));
};

Expand Down
80 changes: 59 additions & 21 deletions packages/gdu/config/webpack/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,29 +388,67 @@ export const baseOptions = (
};
};

const { outputPath } = getGuruConfig();

type BuildEnv = ReturnType<typeof getBuildEnvs>[number];

const getPublicPath = ({
buildEnv,
isTenanted,
tenant,
isDev,
projectFolderName,
}: {
buildEnv: BuildEnv;
isTenanted: boolean;
tenant?: string;
isDev: boolean;
projectFolderName: string;
}): string => {
if (isDev) return '/';

if (buildEnv === 'prod') {
if (isTenanted) {
const prodTenant = tenant || '#{AutoGuru.Tenant}';
const prodAppPath = isTenanted
? `${prodTenant}/${projectFolderName}`
: `${projectFolderName}`;
return `#{PUBLIC_PATH_BASE}/${prodAppPath}/`;
} else {
return `#{PUBLIC_PATH_BASE}/${projectFolderName}/`;
}
}

const folderPath = tenant
? `${tenant}/${projectFolderName}`
: `${projectFolderName}`;

return `https://static-mfe-${buildEnv}.autoguru.io/${folderPath}/`;
};
export const makeWebpackConfig = (
buildEnv: BuildEnv,
isMultiEnv: boolean,
): Configuration => ({
name: buildEnv,
tenant?: string,
): Configuration => {
const { outputPath, isTenanted } = getGuruConfig();
return {
name: buildEnv,

output: {
path: `${outputPath}/${
!isMultiEnv && buildEnv === 'prod' ? '' : buildEnv
}`,
publicPath: isDev
? '/'
: buildEnv === 'prod'
? `#{PUBLIC_PATH_BASE}/${getProjectFolderName()}/`
: `https://static-mfe-${buildEnv}.autoguru.io/${getProjectFolderName()}/`,
filename: `${fileMask}.js`,
chunkFilename: `chunks/${fileMask}.js`,
hashFunction: 'sha256',
crossOriginLoading: 'anonymous',
sourceMapFilename: 'sourceMaps/[file].map',
pathinfo: false,
},
});
output: {
path: `${outputPath}/${
!isMultiEnv && buildEnv === 'prod' ? '' : buildEnv
}`,
publicPath: getPublicPath({
buildEnv,
tenant,
isDev,
projectFolderName: getProjectFolderName(),
isTenanted,
}),
filename: `${fileMask}.js`,
chunkFilename: `chunks/${fileMask}.js`,
hashFunction: 'sha256',
crossOriginLoading: 'anonymous',
sourceMapFilename: 'sourceMaps/[file].map',
pathinfo: false,
},
};
};
1 change: 1 addition & 0 deletions packages/gdu/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface GuruConfig {
mountDOMClass?: string;
octopusPackageId: string;
octopusProjectName: string;
isTenanted?: boolean;
isRoot?: boolean;
port?: number;
basePath?: string;
Expand Down
18 changes: 11 additions & 7 deletions packages/gdu/lib/mfe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ interface Props {
* The name of the MFE
*/
projectName: string;

}

export const getMfeMountPoint = ({
mountDomId,
mountDomClass,
projectName,
}: Props): HTMLElement | null => {
invariant(mountDomId || mountDomClass, 'You must provide a mountDomId or mountDomClass');
mountDomId,
mountDomClass,
projectName,
}: Props): HTMLElement | null => {
invariant(
mountDomId || mountDomClass,
'You must provide a mountDomId or mountDomClass',
);
let point: HTMLElement | null = null;
if (typeof mountDomId === 'string') {
point = document.querySelector('#' + mountDomId);
} else if (typeof mountDomClass === 'string') {
const elements = Array.from(document.querySelectorAll('.' + mountDomClass));
const elements = Array.from(
document.querySelectorAll('.' + mountDomClass),
);
for (const element of elements) {
if (element.childNodes.length === 0) {
point = element as HTMLElement;
Expand Down
60 changes: 30 additions & 30 deletions packages/utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
"name": "@autoguru/utilities",
"version": "1.1.1",
"description": "A collection of smallish things that help AutoGuru write code.",
"homepage": "https://github.com/autoguru-au/octane/tree/master/packages/utilities#readme",
"repository": "https://github.com/autoguru-au/octane/tree/master/packages/utilities",
"license": "MIT",
"files": [
"dist"
],
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/utilities.esm.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsdx build",
"prepublish": "yarn run build"
},
"dependencies": {
"tiny-invariant": "^1.0.6",
"tiny-warning": "^1.0.3"
},
"devDependencies": {
"@autoguru/eslint-plugin": "*",
"@autoguru/jest-preset": "*",
"tsdx": "^0.14.1",
"typescript": "^4.4.3"
},
"publishConfig": {
"access": "public"
}
"name": "@autoguru/utilities",
"version": "1.1.1",
"description": "A collection of smallish things that help AutoGuru write code.",
"homepage": "https://github.com/autoguru-au/octane/tree/master/packages/utilities#readme",
"repository": "https://github.com/autoguru-au/octane/tree/master/packages/utilities",
"license": "MIT",
"files": [
"dist"
],
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/utilities.esm.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsdx build",
"prepublish": "yarn run build"
},
"dependencies": {
"tiny-invariant": "^1.0.6",
"tiny-warning": "^1.0.3"
},
"devDependencies": {
"@autoguru/eslint-plugin": "*",
"@autoguru/jest-preset": "*",
"tsdx": "^0.14.1",
"typescript": "^4.4.3"
},
"publishConfig": {
"access": "public"
}
}

0 comments on commit 0659085

Please sign in to comment.