Skip to content

Commit 48b002a

Browse files
fix build for demos bundles for Angular
1 parent c2f4243 commit 48b002a

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

apps/demos/configs/Angular/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"target": "ES2022",
44
"experimentalDecorators": true,
55
"esModuleInterop": true,
6-
"moduleResolution": "node",
6+
"moduleResolution": "bundler",
77
"skipLibCheck": true,
88
"baseUrl": "./",
99
"paths": {

apps/demos/utils/create-bundles/Angular/bundler.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ interface Bundler {
1919
getBuildOptions: (demo: Demo) => BuildOptions;
2020
buildDemo: (demo: Demo, res) => void;
2121
}
22+
23+
type ChangedFile = {
24+
path: string;
25+
originalContent: string;
26+
};
27+
2228
export default class AngularBundler implements Bundler {
2329
framework: Framework;
2430

@@ -28,12 +34,13 @@ export default class AngularBundler implements Bundler {
2834

2935
getBuildOptions = (): BuildOptions => ({});
3036

31-
private updateAntiForgeryImport = (sourceDemoPath: string) => {
37+
private updateAntiForgeryImport = (sourceDemoPath: string): ChangedFile[] => {
3238
const angularAppPath = join(sourceDemoPath, 'app');
3339
if (!existsSync(angularAppPath)) {
34-
return;
40+
return [];
3541
}
3642

43+
const changedFiles: ChangedFile[] = [];
3744
const oldImport = "import 'anti-forgery';";
3845
const antiForgeryFilePath = join(__dirname, '..', '..', '..', 'shared', 'anti-forgery', 'fetch-override.js');
3946

@@ -59,11 +66,21 @@ export default class AngularBundler implements Bundler {
5966
const relativeImportPath = relative(dirname(entryPath), antiForgeryFilePath).split('\\').join('/');
6067
const normalizedImportPath = relativeImportPath.startsWith('.') ? relativeImportPath : `./${relativeImportPath}`;
6168
const newImport = `import '${normalizedImportPath}';`;
69+
changedFiles.push({ path: entryPath, originalContent: content });
6270
writeFileSync(entryPath, content.split(oldImport).join(newImport), 'utf8');
6371
});
6472
};
6573

6674
replaceRecursively(angularAppPath);
75+
return changedFiles;
76+
};
77+
78+
private restoreChangedFiles = (changedFiles: ChangedFile[]) => {
79+
changedFiles.forEach(({ path, originalContent }) => {
80+
if (existsSync(path)) {
81+
writeFileSync(path, originalContent, 'utf8');
82+
}
83+
});
6784
};
6885

6986
buildDemo = (demo: Demo, res): Promise<void> => {
@@ -88,7 +105,7 @@ export default class AngularBundler implements Bundler {
88105
mkdirSync(indexHtmlPath, { recursive: true });
89106

90107
createDemoLayout(demo, this.framework);
91-
this.updateAntiForgeryImport(sourceDemoPath);
108+
const changedFiles = this.updateAntiForgeryImport(sourceDemoPath);
92109

93110
const ngBuildCommand = `npm run build-angular -- ${getProjectNameByDemo(demo)}`;
94111
const ngBuildProcess = exec(ngBuildCommand);
@@ -99,6 +116,7 @@ export default class AngularBundler implements Bundler {
99116
console.error(`stderr: ${data}`);
100117
});
101118
ngBuildProcess.on('close', (code) => {
119+
this.restoreChangedFiles(changedFiles);
102120
console.log(`child process exited with code ${code}`);
103121
res();
104122
});

0 commit comments

Comments
 (0)