Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): ensure all configured assets can …
Browse files Browse the repository at this point in the history
…be served by dev server

The Vite-based development server restricts filesystem access by default. However, assets
configured by the build option `assets` are intended to be accessible by the application at
runtime. To ensure that these files are accessible, the allow list will now explicitly include
all configured assets found during the build.
  • Loading branch information
clydin committed Nov 22, 2023
1 parent cff7663 commit 6473b01
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ export async function* serveWithVite(
}

if (server) {
// Update fs allow list to include any new assets from the build option.
server.config.server.fs.allow = [
...new Set(...server.config.server.fs.allow, ...assetFiles.values()),
];

handleUpdate(normalizePath, generatedFiles, server, serverOptions, context.logger);
} else {
const projectName = context.target?.project;
Expand Down Expand Up @@ -430,10 +435,11 @@ export async function setupServer(
...externalMetadata.explicit,
];

const cacheDir = join(serverOptions.cacheOptions.path, 'vite');
const configuration: InlineConfig = {
configFile: false,
envFile: false,
cacheDir: join(serverOptions.cacheOptions.path, 'vite'),
cacheDir,
root: virtualProjectRoot,
publicDir: false,
esbuild: false,
Expand All @@ -456,6 +462,13 @@ export async function setupServer(
proxy,
// File watching is handled by the build directly. `null` disables file watching for Vite.
watch: null,
fs: {
// Ensure cache directory, node modules, and all assets are accessible by the client.
// The first two are required for Vite to function in prebundling mode (the default) and to load
// the Vite client-side code for browser reloading. These would be available by default but when
// the `allow` option is explicitly configured, they must be included manually.
allow: [cacheDir, join(serverOptions.workspaceRoot, 'node_modules'), ...assets.values()],
},
// This is needed when `externalDependencies` is used to prevent Vite load errors.
// NOTE: If Vite adds direct support for externals, this can be removed.
preTransformRequests: externalMetadata.explicit.length === 0,
Expand Down

0 comments on commit 6473b01

Please sign in to comment.