-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular/build): support middlewareConfig #32900
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
Open
tianma630
wants to merge
5
commits into
angular:main
Choose a base branch
from
tianma630:feat/middleware
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
875f654
feat(@angular/build): support middlewareConfig
tianma630 e294e55
feat(@angular/build): support middlewareConfig
tianma630 750c6d0
Update packages/angular/build/src/builders/dev-server/vite/server.ts
tianma630 0c06c59
Update packages/angular/build/src/utils/load-middleware-config.ts
tianma630 6a915ca
Update packages/angular_devkit/build_angular/src/builders/dev-server/…
tianma630 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/angular/build/src/utils/load-middleware-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
| import { existsSync } from 'node:fs'; | ||
| import { resolve } from 'node:path'; | ||
| import { pathToFileURL } from 'node:url'; | ||
| import type { Connect } from 'vite'; | ||
| import { assertIsError } from './error'; | ||
| import { loadEsmModule } from './load-esm'; | ||
|
|
||
| export async function loadMiddlewareConfiguration( | ||
| root: string, | ||
| middlewareConfig: string | undefined, | ||
| ): Promise<Connect.NextHandleFunction[]> { | ||
| if (!middlewareConfig) { | ||
| return []; | ||
| } | ||
|
|
||
| const middlewarePath = resolve(root, middlewareConfig); | ||
|
|
||
| if (!existsSync(middlewarePath)) { | ||
| throw new Error(`Middleware configuration file ${middlewarePath} does not exist.`); | ||
| } | ||
|
|
||
| let middlewareConfiguration; | ||
|
|
||
| try { | ||
| middlewareConfiguration = await import(pathToFileURL(middlewarePath).href); | ||
| } catch (e) { | ||
| assertIsError(e); | ||
| if (e.code !== 'ERR_REQUIRE_ASYNC_MODULE') { | ||
| throw e; | ||
| } | ||
|
|
||
| middlewareConfiguration = await loadEsmModule<{ default: unknown }>( | ||
| pathToFileURL(middlewarePath), | ||
| ); | ||
| } | ||
|
|
||
| if ('default' in middlewareConfiguration) { | ||
| middlewareConfiguration = middlewareConfiguration.default; | ||
| } | ||
|
|
||
| return Array.isArray(middlewareConfiguration) | ||
| ? middlewareConfiguration | ||
| : [middlewareConfiguration]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...ular_devkit/build_angular/src/builders/dev-server/tests/options/middleware-config_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { createServer } from 'node:http'; | ||
| import { executeDevServer } from '../../index'; | ||
| import { executeOnceAndFetch } from '../execute-fetch'; | ||
| import { describeServeBuilder } from '../jasmine-helpers'; | ||
| import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; | ||
|
|
||
| describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => { | ||
| describe('option: "middlewareConfig"', () => { | ||
| beforeEach(async () => { | ||
| setupTarget(harness); | ||
|
|
||
| // Application code is not needed for these tests | ||
| await harness.writeFile('src/main.ts', ''); | ||
| }); | ||
|
|
||
| it('middleware configuration export single function (CommonJS)', async () => { | ||
| harness.useTarget('serve', { | ||
| ...BASE_OPTIONS, | ||
| middlewareConfig: 'middleware.config.js', | ||
| }); | ||
| const proxyServer = await createProxyServer(); | ||
| try { | ||
| await harness.writeFiles({ | ||
| 'middleware.config.js': `module.exports = (req, res, next) => { res.end('TEST_MIDDLEWARE'); next();}`, | ||
| }); | ||
|
|
||
| const { result, response } = await executeOnceAndFetch(harness, '/test'); | ||
|
|
||
| expect(result?.success).toBeTrue(); | ||
| expect(await response?.text()).toContain('TEST_MIDDLEWARE'); | ||
| } finally { | ||
| await proxyServer.close(); | ||
| } | ||
| }); | ||
|
|
||
| it('middleware configuration export an array of multiple functions (CommonJS)', async () => { | ||
| harness.useTarget('serve', { | ||
| ...BASE_OPTIONS, | ||
| middlewareConfig: 'middleware.config.js', | ||
| }); | ||
| const proxyServer = await createProxyServer(); | ||
| try { | ||
| await harness.writeFiles({ | ||
| 'middleware.config.js': `module.exports = [(req, res, next) => { next();}, (req, res, next) => { res.end('TEST_MIDDLEWARE'); next();}]`, | ||
| }); | ||
|
|
||
| const { result, response } = await executeOnceAndFetch(harness, '/test'); | ||
|
|
||
| expect(result?.success).toBeTrue(); | ||
| expect(await response?.text()).toContain('TEST_MIDDLEWARE'); | ||
| } finally { | ||
| await proxyServer.close(); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Creates an HTTP Server used for proxy testing that provides a `/test` endpoint | ||
| * that returns a 200 response with a body of `TEST_API_RETURN`. All other requests | ||
| * will return a 404 response. | ||
| */ | ||
| async function createProxyServer() { | ||
| const proxyServer = createServer((request, response) => { | ||
| if (request.url?.endsWith('/test')) { | ||
| response.writeHead(200); | ||
| response.end('TEST_API_RETURN'); | ||
| } else { | ||
| response.writeHead(404); | ||
| response.end(); | ||
| } | ||
| }); | ||
|
|
||
| await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve)); | ||
|
|
||
| return { | ||
| address: proxyServer.address() as import('net').AddressInfo, | ||
| close: () => new Promise<void>((resolve) => proxyServer.close(() => resolve())), | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.