Skip to content

Commit

Permalink
fix(ui-devkit): Fix baseHref configuration
Browse files Browse the repository at this point in the history
Fixes #1794. We now directly modify the angular.json file
  • Loading branch information
michaelbromley committed Apr 24, 2023
1 parent 7b407de commit c7836b2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/admin-ui-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export interface AdminUiPluginOptions {
/**
* @description
* The route to the Admin UI.
*
* Note: If you are using the {@link compileUiExtensions} function to compile a custom version of the Admin UI, then
* the route should match the `baseHref` option passed to that function. The default value of `baseHref` is `/admin/`,
* so it only needs to be changed if you set this `route` option to something other than `"admin"`.
*/
route: string;
/**
Expand Down
20 changes: 9 additions & 11 deletions packages/ui-devkit/src/compiler/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as fs from 'fs-extra';
import * as path from 'path';

import { DEFAULT_BASE_HREF, MODULES_OUTPUT_DIR } from './constants';
import { copyGlobalStyleFile, setupScaffold } from './scaffold';
import { copyGlobalStyleFile, setBaseHref, setupScaffold } from './scaffold';
import { getAllTranslationFiles, mergeExtensionTranslations } from './translations';
import {
Extension,
Expand Down Expand Up @@ -72,7 +72,8 @@ function runCompileMode(
const compile = () =>
new Promise<void>(async (resolve, reject) => {
await setupScaffold(outputPath, extensions);
const commandArgs = ['run', 'build', `--base-href=${baseHref}`, ...buildProcessArguments(args)];
await setBaseHref(outputPath, baseHref);
const commandArgs = ['run', 'build', ...buildProcessArguments(args)];
if (!usingYarn) {
// npm requires `--` before any command line args being passed to a script
commandArgs.splice(2, 0, '--');
Expand Down Expand Up @@ -117,20 +118,17 @@ function runWatchMode(
const compile = () =>
new Promise<void>(async (resolve, reject) => {
await setupScaffold(outputPath, extensions);
await setBaseHref(outputPath, baseHref);
const adminUiExtensions = extensions.filter(isAdminUiExtension);
const normalizedExtensions = normalizeExtensions(adminUiExtensions);
const globalStylesExtensions = extensions.filter(isGlobalStylesExtension);
const staticAssetExtensions = extensions.filter(isStaticAssetExtension);
const allTranslationFiles = getAllTranslationFiles(extensions.filter(isTranslationExtension));
buildProcess = spawn(
cmd,
['run', 'start', `--port=${port}`, `--base-href=${baseHref}`, ...buildProcessArguments(args)],
{
cwd: outputPath,
shell: true,
stdio: 'inherit',
},
);
buildProcess = spawn(cmd, ['run', 'start', `--port=${port}`, ...buildProcessArguments(args)], {
cwd: outputPath,
shell: true,
stdio: 'inherit',
});

buildProcess.on('close', code => {
if (code !== 0) {
Expand Down
7 changes: 7 additions & 0 deletions packages/ui-devkit/src/compiler/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ function copyAdminUiSource(outputPath: string, modulePathMapping: Record<string,
fs.copySync(adminUiSrc, outputSrc);
}

export async function setBaseHref(outputPath: string, baseHref: string) {
const angularJsonFilePath = path.join(outputPath, '/angular.json');
const angularJson = await fs.readJSON(angularJsonFilePath, 'utf-8');
angularJson.projects['vendure-admin'].architect.build.options.baseHref = baseHref;
await fs.writeJSON(angularJsonFilePath, angularJson, { spaces: 2 });
}

/**
* Adds module path mapping to the bundled tsconfig.json file if defined as a UI extension.
*/
Expand Down
17 changes: 17 additions & 0 deletions packages/ui-devkit/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,23 @@ export interface UiExtensionCompilerOptions {
* of the app, for example with the default value of `'/admin/'`, the Admin UI app
* will be configured to be served from `http://<host>/admin/`.
*
* Note: if you are using this in conjunction with the {@link AdminUiPlugin} then you should
* also set the `route` option to match this value.
*
* @example
* ```TypeScript
* AdminUiPlugin.init({
* route: 'my-route',
* port: 5001,
* app: compileUiExtensions({
* baseHref: '/my-route/',
* outputPath: path.join(__dirname, './custom-admin-ui'),
* extensions: [],
* devMode: true,
* }),
* }),
* ```
*
* @default '/admin/'
*/
baseHref?: string;
Expand Down

0 comments on commit c7836b2

Please sign in to comment.