feat(angular): add spa option to file-server executor to support reloading client-side routes (#11252)
This commit is contained in:
parent
dc6c5313cc
commit
ea3fe2e8b1
@ -3233,6 +3233,11 @@
|
|||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"description": "Watch for file changes.",
|
"description": "Watch for file changes.",
|
||||||
"default": false
|
"default": false
|
||||||
|
},
|
||||||
|
"spa": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Redirect 404 errors to index.html (useful for SPA's).",
|
||||||
|
"default": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
|
|||||||
@ -1,23 +1,22 @@
|
|||||||
import { execFileSync, fork } from 'child_process';
|
|
||||||
import {
|
import {
|
||||||
ExecutorContext,
|
ExecutorContext,
|
||||||
joinPathFragments,
|
joinPathFragments,
|
||||||
readJsonFile,
|
|
||||||
workspaceLayout,
|
workspaceLayout,
|
||||||
} from '@nrwl/devkit';
|
} from '@nrwl/devkit';
|
||||||
import ignore from 'ignore';
|
import { execFileSync, fork } from 'child_process';
|
||||||
import { readFileSync } from 'fs';
|
|
||||||
import { Schema } from './schema';
|
|
||||||
import { watch } from 'chokidar';
|
import { watch } from 'chokidar';
|
||||||
import { platform } from 'os';
|
import { copyFileSync, readFileSync, unlinkSync } from 'fs';
|
||||||
import { resolve } from 'path';
|
import ignore from 'ignore';
|
||||||
import { readModulePackageJson } from 'nx/src/utils/package-json';
|
import { readModulePackageJson } from 'nx/src/utils/package-json';
|
||||||
|
import { platform } from 'os';
|
||||||
|
import { join, resolve } from 'path';
|
||||||
|
import { Schema } from './schema';
|
||||||
|
|
||||||
// platform specific command name
|
// platform specific command name
|
||||||
const pmCmd = platform() === 'win32' ? `npx.cmd` : 'npx';
|
const pmCmd = platform() === 'win32' ? `npx.cmd` : 'npx';
|
||||||
|
|
||||||
function getHttpServerArgs(options: Schema) {
|
function getHttpServerArgs(options: Schema) {
|
||||||
const args = ['-c-1'];
|
const args = ['-c-1', '--cors'];
|
||||||
if (options.port) {
|
if (options.port) {
|
||||||
args.push(`-p=${options.port}`);
|
args.push(`-p=${options.port}`);
|
||||||
}
|
}
|
||||||
@ -43,7 +42,6 @@ function getHttpServerArgs(options: Schema) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
args.push('--cors');
|
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,6 +147,15 @@ export default async function* fileServerExecutor(
|
|||||||
run();
|
run();
|
||||||
|
|
||||||
const outputPath = getBuildTargetOutputPath(options, context);
|
const outputPath = getBuildTargetOutputPath(options, context);
|
||||||
|
|
||||||
|
if (options.spa) {
|
||||||
|
const src = join(outputPath, 'index.html');
|
||||||
|
const dst = join(outputPath, '404.html');
|
||||||
|
|
||||||
|
// See: https://github.com/http-party/http-server#magic-files
|
||||||
|
copyFileSync(src, dst);
|
||||||
|
}
|
||||||
|
|
||||||
const args = getHttpServerArgs(options);
|
const args = getHttpServerArgs(options);
|
||||||
|
|
||||||
const { path: pathToHttpServerPkgJson, packageJson } =
|
const { path: pathToHttpServerPkgJson, packageJson } =
|
||||||
@ -173,6 +180,10 @@ export default async function* fileServerExecutor(
|
|||||||
if (disposeWatch) {
|
if (disposeWatch) {
|
||||||
disposeWatch();
|
disposeWatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.spa) {
|
||||||
|
unlinkSync(join(outputPath, '404.html'));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
process.on('exit', processExitListener);
|
process.on('exit', processExitListener);
|
||||||
process.on('SIGTERM', processExitListener);
|
process.on('SIGTERM', processExitListener);
|
||||||
|
|||||||
@ -11,4 +11,5 @@ export interface Schema {
|
|||||||
withDeps: boolean;
|
withDeps: boolean;
|
||||||
proxyOptions?: object;
|
proxyOptions?: object;
|
||||||
watch?: boolean;
|
watch?: boolean;
|
||||||
|
spa?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,6 +60,11 @@
|
|||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"description": "Watch for file changes.",
|
"description": "Watch for file changes.",
|
||||||
"default": false
|
"default": false
|
||||||
|
},
|
||||||
|
"spa": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Redirect 404 errors to index.html (useful for SPA's).",
|
||||||
|
"default": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user