forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-replacements_spec.ts
More file actions
86 lines (72 loc) · 3.21 KB
/
file-replacements_spec.ts
File metadata and controls
86 lines (72 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* @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 { buildApplication } from '../../index';
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
describe('Option: "fileReplacements"', () => {
it('should replace JSON files', async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
fileReplacements: [{ replace: './src/one.json', with: './src/two.json' }],
});
await harness.modifyFile('tsconfig.json', (content) => {
const tsconfig = JSON.parse(content);
tsconfig.compilerOptions.resolveJsonModule = true;
return JSON.stringify(tsconfig);
});
await harness.writeFile('./src/one.json', '{ "x": 12345 }');
await harness.writeFile('./src/two.json', '{ "x": 67890 }');
await harness.writeFile('src/main.ts', 'import { x } from "./one.json";\n console.log(x);');
const { result } = await harness.executeOnce();
expect(result?.success).toBe(true);
harness.expectFile('dist/browser/main.js').content.not.toContain('12345');
harness.expectFile('dist/browser/main.js').content.toContain('67890');
});
it('should apply file replacements inside web workers', async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
fileReplacements: [{ replace: './src/app/env.ts', with: './src/app/env.prod.ts' }],
});
await harness.writeFile('src/app/env.ts', `export const value = 'development';`);
await harness.writeFile('src/app/env.prod.ts', `export const value = 'production';`);
await harness.writeFile(
'src/app/worker.ts',
`import { value } from './env';\nself.postMessage(value);`,
);
await harness.writeFile(
'src/app/app.component.ts',
`
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: false,
template: '<h1>Worker Test</h1>',
})
export class AppComponent {
worker = new Worker(new URL('./worker', import.meta.url), { type: 'module' });
}
`,
);
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
// Verify the worker output file exists
expect(harness.hasFileMatch('dist/browser', /^worker-[A-Z0-9]{8}\.js$/)).toBeTrue();
// Find the worker filename from the main bundle and read its content
const mainContent = harness.readFile('dist/browser/main.js');
const workerMatch = mainContent.match(/worker-([A-Z0-9]{8})\.js/);
expect(workerMatch).not.toBeNull();
if (workerMatch) {
const workerFilename = `dist/browser/${workerMatch[0]}`;
// The worker bundle should contain the replaced (production) value
harness.expectFile(workerFilename).content.toContain('production');
// The worker bundle should NOT contain the original (development) value
harness.expectFile(workerFilename).content.not.toContain('development');
}
});
});
});