Skip to content

fix(@angular-devkit/build-angular): allow vite to serve JavaScript and TypeScript assets #26643

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

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import { describeServeBuilder } from '../jasmine-helpers';
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';

describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
const javascriptFileContent =
"import {foo} from 'unresolved'; /* a comment */const foo = `bar`;\n\n\n";

describe('Behavior: "browser builder assets"', () => {
it('serves a project JavaScript asset unmodified', async () => {
const javascriptFileContent = '/* a comment */const foo = `bar`;\n\n\n';
await harness.writeFile('src/extra.js', javascriptFileContent);

setupTarget(harness, {
Expand All @@ -33,5 +35,25 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
expect(result?.success).toBeTrue();
expect(await response?.text()).toContain(javascriptFileContent);
});

it('serves a project TypeScript asset unmodified', async () => {
await harness.writeFile('src/extra.ts', javascriptFileContent);

setupTarget(harness, {
assets: ['src/extra.ts'],
optimization: {
scripts: true,
},
});

harness.useTarget('serve', {
...BASE_OPTIONS,
});

const { result, response } = await executeOnceAndFetch(harness, 'extra.ts');

expect(result?.success).toBeTrue();
expect(await response?.text()).toContain(javascriptFileContent);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,11 @@ export async function setupServer(
// Rewrite all build assets to a vite raw fs URL
const assetSourcePath = assets.get(pathname);
if (assetSourcePath !== undefined) {
// Workaround to disable Vite transformer middleware.
// See: https://github.com/vitejs/vite/blob/746a1daab0395f98f0afbdee8f364cb6cf2f3b3f/packages/vite/src/node/server/middlewares/transform.ts#L201 and
// https://github.com/vitejs/vite/blob/746a1daab0395f98f0afbdee8f364cb6cf2f3b3f/packages/vite/src/node/server/transformRequest.ts#L204-L206
req.headers.accept = 'text/html';

// The encoding needs to match what happens in the vite static middleware.
// ref: https://github.com/vitejs/vite/blob/d4f13bd81468961c8c926438e815ab6b1c82735e/packages/vite/src/node/server/middlewares/static.ts#L163
req.url = `${server.config.base}@fs/${encodeURI(assetSourcePath)}`;
Expand Down