Skip to content

Commit 1aeefa7

Browse files
committed
fix(@angular/build): failed to proxy error for assets
Remove proxy to handle `/` to `/base/` instead update the `AngularAssetsMiddleware` Closes #30639
1 parent 1cde40e commit 1aeefa7

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

packages/angular/build/src/builders/karma/application_builder.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,22 @@ class AngularAssetsMiddleware {
8484
return;
8585
}
8686

87+
// Implementation of serverFile can be found here:
88+
// https://github.com/karma-runner/karma/blob/84f85e7016efc2266fa6b3465f494a3fa151c85c/lib/middleware/common.js#L10
8789
switch (file.origin) {
8890
case 'disk':
8991
this.serveFile(file.inputPath, undefined, res, undefined, undefined, /* doNotCache */ true);
9092
break;
9193
case 'memory':
9294
// Include pathname to help with Content-Type headers.
93-
this.serveFile(`/unused/${url.pathname}`, undefined, res, undefined, file.contents, true);
95+
this.serveFile(
96+
`/unused/${url.pathname}`,
97+
undefined,
98+
res,
99+
undefined,
100+
file.contents,
101+
/* doNotCache */ false,
102+
);
94103
break;
95104
}
96105
}
@@ -508,6 +517,7 @@ async function initializeApplication(
508517
scriptsFiles.push({
509518
pattern: `${outputPath}/${outputName}`,
510519
watched: false,
520+
included: typeof scriptEntry === 'string' ? true : scriptEntry.inject !== false,
511521
type: 'js',
512522
});
513523
}

packages/angular/build/src/builders/karma/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ function getBuiltInKarmaConfig(
107107
'karma-jasmine-html-reporter',
108108
'karma-coverage',
109109
].map((p) => workspaceRootRequire(p)),
110-
proxies: {
111-
'/': '/base/',
112-
},
113110
jasmineHtmlReporter: {
114111
suppressAll: true, // removes the duplicated traces
115112
},
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { execute } from '../../index';
10+
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup';
11+
12+
describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => {
13+
describe('Option: "scripts"', () => {
14+
beforeEach(async () => {
15+
await setupTarget(harness);
16+
});
17+
18+
it(`should be able to access non injected script`, async () => {
19+
await harness.writeFiles({
20+
'src/test.js': `console.log('hello from test script.')`,
21+
'src/app/app.component.ts': `
22+
import { Component } from '@angular/core';
23+
24+
@Component({
25+
selector: 'app-root',
26+
standalone: false,
27+
template: '<p>Hello World</p>'
28+
})
29+
export class AppComponent {
30+
loadScript() {
31+
return new Promise<void>((resolve, reject) => {
32+
const script = document.createElement('script');
33+
script.onload = () => resolve();
34+
script.onerror = reject;
35+
script.src = 'test.js';
36+
document.body.appendChild(script);
37+
});
38+
}
39+
}
40+
`,
41+
'src/app/app.component.spec.ts': `
42+
import { TestBed } from '@angular/core/testing';
43+
import { AppComponent } from './app.component';
44+
45+
describe('AppComponent', () => {
46+
beforeEach(() => TestBed.configureTestingModule({
47+
declarations: [AppComponent]
48+
}));
49+
50+
it('should load script', async () => {
51+
const fixture = TestBed.createComponent(AppComponent);
52+
fixture.detectChanges();
53+
54+
await expectAsync(fixture.componentInstance.loadScript()).toBeResolved();
55+
});
56+
});`,
57+
});
58+
59+
harness.useTarget('test', {
60+
...BASE_OPTIONS,
61+
scripts: [
62+
{
63+
input: 'src/test.js',
64+
bundleName: 'test',
65+
inject: false,
66+
},
67+
],
68+
});
69+
70+
const { result } = await harness.executeOnce();
71+
expect(result?.success).toBeTrue();
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)