Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): allow vite to serve JavaScript an…
Browse files Browse the repository at this point in the history
…d TypeScript assets

This commit fixes an issue which caused vite to transform JavaScript and TypeScript assets.

Closes #26641

(cherry picked from commit 72bd0ab)
  • Loading branch information
alan-agius4 committed Dec 12, 2023
1 parent 385eb77 commit ef11781
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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()).toBe(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 @@ -578,6 +578,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

0 comments on commit ef11781

Please sign in to comment.